decoder: fixes 5.2-strict mode state object
[luajson.git] / lua / json / util.lua
blobdfdbf3bf32702e3c6155d017993ec654138750c2
1 --[[
2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
4 ]]
5 local type = type
6 local print = print
7 local tostring = tostring
8 local pairs = pairs
9 local getmetatable, setmetatable = getmetatable, setmetatable
10 local select = select
12 _ENV = nil
14 local function foreach(tab, func)
15 for k, v in pairs(tab) do
16 func(k,v)
17 end
18 end
19 local function printValue(tab, name)
20 local parsed = {}
21 local function doPrint(key, value, space)
22 space = space or ''
23 if type(value) == 'table' then
24 if parsed[value] then
25 print(space .. key .. '= <' .. parsed[value] .. '>')
26 else
27 parsed[value] = key
28 print(space .. key .. '= {')
29 space = space .. ' '
30 foreach(value, function(key, value) doPrint(key, value, space) end)
31 end
32 else
33 if type(value) == 'string' then
34 value = '[[' .. tostring(value) .. ']]'
35 end
36 print(space .. key .. '=' .. tostring(value))
37 end
38 end
39 doPrint(name, tab)
40 end
42 local function clone(t)
43 local ret = {}
44 for k,v in pairs(t) do
45 ret[k] = v
46 end
47 return ret
48 end
50 local function inner_merge(t, remaining, from, ...)
51 if remaining == 0 then
52 return t
53 end
54 if from then
55 for k,v in pairs(from) do
56 t[k] = v
57 end
58 end
59 return inner_merge(t, remaining - 1, ...)
60 end
62 local function merge(t, ...)
63 return inner_merge(t, select('#', ...), ...)
64 end
66 -- Function to insert nulls into the JSON stream
67 local function null()
68 return null
69 end
71 -- Marker for 'undefined' values
72 local function undefined()
73 return undefined
74 end
76 local ArrayMT = {}
78 --[[
79 Return's true if the metatable marks it as an array..
80 Or false if it has no array component at all
81 Otherwise nil to get the normal detection component working
83 local function IsArray(value)
84 if type(value) ~= 'table' then return false end
85 local ret = getmetatable(value) == ArrayMT
86 if not ret then
87 if #value == 0 then return false end
88 else
89 return ret
90 end
91 end
92 local function InitArray(array)
93 setmetatable(array, ArrayMT)
94 return array
95 end
97 local CallMT = {}
99 local function isCall(value)
100 return CallMT == getmetatable(value)
103 local function buildCall(name, ...)
104 local callData = {
105 name = name,
106 parameters = {n = select('#', ...), ...}
108 return setmetatable(callData, CallMT)
111 local function decodeCall(callData)
112 if not isCall(callData) then return nil end
113 return callData.name, callData.parameters
116 local function doOptionMerge(options, nested, name, defaultOptions, modeOptions)
117 if nested then
118 modeOptions = modeOptions and modeOptions[name]
119 defaultOptions = defaultOptions and defaultOptions[name]
121 options[name] = merge(
123 defaultOptions,
124 modeOptions,
125 options[name]
129 local json_util = {
130 printValue = printValue,
131 clone = clone,
132 merge = merge,
133 null = null,
134 undefined = undefined,
135 IsArray = IsArray,
136 InitArray = InitArray,
137 isCall = isCall,
138 buildCall = buildCall,
139 decodeCall = decodeCall,
140 doOptionMerge = doOptionMerge
143 return json_util