Lua table和json转换



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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值