local function createJson()
local math = require('math')
local string = require("string")
local table = require("table")
local object = nil
-----------------------------------------------------------------------------
-- Module declaration
-----------------------------------------------------------------------------
local json = {} -- Public namespace
local json_private = {} -- Private namespace
-- Public constants
json.EMPTY_ARRAY = {}
json.EMPTY_OBJECT = {}
-- Public functions
-- Private functions
local decode_scanArray
local decode_scanComment
local decode_scanConstant
local decode_scanNumber
local decode_scanObject
local decode_scanString
local decode_scanWhitespace
local encodeString
local isArray
local isEncodable
-----------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
-----------------------------------------------------------------------------
--- Encodes an arbitrary Lua object / variable.
-- @param v The Lua object / variable to be JSON encoded.
-- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
function json.encode(v)
-- Handle nil values
if v == nil then
return "null"
end
local vtype = type(v)
-- Handle strings
if vtype == 'string' then
return "\"" .. json_private.encodeString(v) .. "\"" -- Need to handle encoding in string
end
-- Handle booleans
if vtype == 'number' or vtype == 'boolean' then
return tostring(v)
end
-- Handle tables
if vtype == 'table' then
local rval = {}
-- Consider arrays separately
local bArray, maxCount = isArray(v)
if bArray then
for i = 1, maxCount do
table.insert(rval, json.encode(v[i]))
end
else -- An object, not an array
for i, j in pairs(v) do
if isEncodable(i) and isEncodable(j) then
table.insert(rval, "\"" .. json_private.encodeString(i) .. '\":' .. json.encode(j))
end
end
end
if bArray then
return '[' .. table.concat(rval, ',') .. ']'
else
return '{' .. table.concat(rval, ',') .. '}'
end
end
-- Handle null values
if vtype == 'function' and v == json.null then
return 'null'
end
assert(false, 'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
end
--- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
-- @param s The string to scan.
-- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
-- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
-- and the position of the first character after
-- the scanned JSON object.
function json.decode(s, startPos)
startPos = startPos and startPos or 1
startPos = decode_scanWhitespace(s, startPos)
assert(startPos <= string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
local curChar = string.sub(s, startPos, startPos)
-- Object
if curChar == '{' then
return decode_scanObject(s, startPos)
end
-- Array
if curChar == '[' then
return decode_scanArray(s, startPos)
end
-- Number
if string.find("+-0123456789.e", curChar, 1, true) then
return decode_scanNumber(s, startPos)
end
-- String
if curChar == "\"" or curChar == [[']] then
return decode_sc
Lua table和json转换
最新推荐文章于 2023-02-03 18:14:03 发布