local ____exports = __module.env local FALLBACK_NULL = {__json_null__ = true} local FALLBACK_EMPTY_ARRAY = {__json_empty_array__ = true} --- -- @module_data local cachedNative = nil local function TryLoadNativeJson() if cachedNative ~= nil then local ____temp_0 if cachedNative == false then ____temp_0 = nil else ____temp_0 = cachedNative end return ____temp_0 end local ok, mod = pcall(require, "ue_json") if ok and type(mod) == "table" and type(mod.encode) == "function" and type(mod.decode) == "function" then cachedNative = mod return cachedNative end cachedNative = false return nil end local function EscapeJsonString(str) local escaped = string.gsub( str, "[%z-\\\"]", function(ch) if ch == "\"" then return "\\\"" end if ch == "\\" then return "\\\\" end if ch == "\b" then return "\\b" end if ch == "\f" then return "\\f" end if ch == "\n" then return "\\n" end if ch == "\r" then return "\\r" end if ch == "\t" then return "\\t" end return string.format( "\\u%04x", string.byte(ch) ) end ) return ("\"" .. escaped) .. "\"" end local function IsPureArray(tbl) local count = 0 local maxKey = 0 for k in pairs(tbl) do count = count + 1 if type(k) ~= "number" or math.type(k) ~= "integer" or k <= 0 then return false, 0 end if k > maxKey then maxKey = k end end return count > 0 and maxKey == count, maxKey end local function FallbackEncodeValue(val, depth) if depth > 64 then return nil, "JSON encode exceeded maximum depth (64)" end if val == nil or val == FALLBACK_NULL then return "null", nil end if val == FALLBACK_EMPTY_ARRAY then return "[]", nil end local t = type(val) if t == "boolean" then return val and "true" or "false", nil end if t == "number" then local num = val if num ~= num or num == math.huge or num == -math.huge then return nil, "Cannot encode NaN or Infinity to JSON" end if math.type(num) == "integer" then return string.format("%d", num), nil end return string.format("%.17g", num), nil end if t == "string" then return EscapeJsonString(val), nil end if t == "table" then local tbl = val local isArr, len = IsPureArray(tbl) if isArr then local items = {} do local i = 1 while i <= len do local encoded, err = FallbackEncodeValue(tbl[i], depth + 1) if err ~= nil then return nil, err end items[#items + 1] = encoded i = i + 1 end end return ("[" .. table.concat(items, ",")) .. "]", nil end local pairsOut = {} for k, v in pairs(tbl) do local kt = type(k) if kt ~= "string" and kt ~= "number" then return nil, "Unsupported JSON object key type: " .. kt end local encodedVal, err = FallbackEncodeValue(v, depth + 1) if err ~= nil then return nil, err end pairsOut[#pairsOut + 1] = (EscapeJsonString(tostring(k)) .. ":") .. encodedVal end return ("{" .. table.concat(pairsOut, ",")) .. "}", nil end return nil, "Unsupported Lua type for JSON encode: " .. t end local function SkipWs(st) while st.pos <= st.len do local b = string.byte(st.src, st.pos) if b == 32 or b == 9 or b == 10 or b == 13 then st.pos = st.pos + 1 else break end end end local function ParseString(st) st.pos = st.pos + 1 local out = {} while st.pos <= st.len do local ch = string.sub(st.src, st.pos, st.pos) if ch == "\"" then st.pos = st.pos + 1 return table.concat(out), nil end if ch == "\\" then st.pos = st.pos + 1 if st.pos > st.len then return nil, "Unterminated escape in JSON string" end local esc = string.sub(st.src, st.pos, st.pos) st.pos = st.pos + 1 if esc == "\"" or esc == "\\" or esc == "/" then out[#out + 1] = esc elseif esc == "b" then out[#out + 1] = "\b" elseif esc == "f" then out[#out + 1] = "\f" elseif esc == "n" then out[#out + 1] = "\n" elseif esc == "r" then out[#out + 1] = "\r" elseif esc == "t" then out[#out + 1] = "\t" elseif esc == "u" then local hex = string.sub(st.src, st.pos, st.pos + 3) local code = tonumber(hex, 16) if code == nil or #hex < 4 then return nil, "Invalid \\u escape" end st.pos = st.pos + 4 out[#out + 1] = utf8.char(code) else return nil, "Invalid escape char: \\" .. esc end else out[#out + 1] = ch st.pos = st.pos + 1 end end return nil, "Unterminated JSON string" end local function ParseValue(st, depth) if depth > 64 then return nil, "JSON decode exceeded maximum depth (64)" end SkipWs(st) if st.pos > st.len then return nil, "Unexpected EOF" end local ch = string.sub(st.src, st.pos, st.pos) if ch == "\"" then return ParseString(st) end if ch == "{" then st.pos = st.pos + 1 local obj = {} SkipWs(st) if string.sub(st.src, st.pos, st.pos) == "}" then st.pos = st.pos + 1 return obj, nil end while true do SkipWs(st) if string.sub(st.src, st.pos, st.pos) ~= "\"" then return nil, "Expected string key in JSON object" end local key, keyErr = ParseString(st) if keyErr ~= nil then return nil, keyErr end SkipWs(st) if string.sub(st.src, st.pos, st.pos) ~= ":" then return nil, "Expected ':' in JSON object" end st.pos = st.pos + 1 local val, valErr = ParseValue(st, depth + 1) if valErr ~= nil then return nil, valErr end obj[key] = val SkipWs(st) local sep = string.sub(st.src, st.pos, st.pos) if sep == "}" then st.pos = st.pos + 1 return obj, nil end if sep ~= "," then return nil, "Expected ',' or '}' in JSON object" end st.pos = st.pos + 1 end end if ch == "[" then st.pos = st.pos + 1 local arr = {} SkipWs(st) if string.sub(st.src, st.pos, st.pos) == "]" then st.pos = st.pos + 1 return arr, nil end local idx = 1 while true do local val, valErr = ParseValue(st, depth + 1) if valErr ~= nil then return nil, valErr end arr[idx] = val idx = idx + 1 SkipWs(st) local sep = string.sub(st.src, st.pos, st.pos) if sep == "]" then st.pos = st.pos + 1 return arr, nil end if sep ~= "," then return nil, "Expected ',' or ']' in JSON array" end st.pos = st.pos + 1 end end if string.sub(st.src, st.pos, st.pos + 3) == "true" then st.pos = st.pos + 4 return true, nil end if string.sub(st.src, st.pos, st.pos + 4) == "false" then st.pos = st.pos + 5 return false, nil end if string.sub(st.src, st.pos, st.pos + 3) == "null" then st.pos = st.pos + 4 return st.keepNull and FALLBACK_NULL or nil, nil end local numStr = string.match(st.src, "^%-?%d+%.?%d*[eE]?[%+%-]?%d*", st.pos) if numStr ~= nil and numStr ~= "" then local parsed = tonumber(numStr) if parsed ~= nil then st.pos = st.pos + #numStr return parsed, nil end end return nil, "Unexpected token at pos " .. tostring(st.pos) end local function FallbackDecode(raw, keepNull) local st = {src = raw, len = #raw, pos = 1, keepNull = keepNull} local val, err = ParseValue(st, 0) if err ~= nil then return nil, err end SkipWs(st) if st.pos <= st.len then return nil, "Trailing characters after JSON value" end return val, nil end function ____exports.IsNativeBackend() return TryLoadNativeJson() ~= nil end function ____exports.GetNull() local native = TryLoadNativeJson() local ____temp_1 if native ~= nil then ____temp_1 = native.null else ____temp_1 = FALLBACK_NULL end return ____temp_1 end function ____exports.GetEmptyArray() local native = TryLoadNativeJson() local ____temp_2 if native ~= nil then ____temp_2 = native.empty_array else ____temp_2 = FALLBACK_EMPTY_ARRAY end return ____temp_2 end function ____exports.Encode(value) local native = TryLoadNativeJson() if native ~= nil then return native.encode(value) end return FallbackEncodeValue(value, 0) end function ____exports.Decode(raw, keepNull) if keepNull == nil then keepNull = false end if type(raw) ~= "string" or raw == "" then return nil, "Empty JSON input" end local native = TryLoadNativeJson() if native ~= nil then return native.decode(raw, keepNull) end return FallbackDecode(raw, keepNull) end function __module.StashData(env, to) local ____has = to.__has__ if not ____has then ____has = {} to.__has__ = ____has end to.cachedNative = cachedNative ____has.cachedNative = true end function __module.RestoreData(env, from) local ____has = from.__has__ if not ____has then return end if ____has.cachedNative then cachedNative = from.cachedNative end end function __module.UpdateImports() end