2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
7 local tostring = tostring
9 local getmetatable
, setmetatable
= getmetatable
, setmetatable
14 local function foreach(tab
, func
)
15 for k
, v
in pairs(tab
) do
19 local function printValue(tab
, name
)
21 local function doPrint(key
, value
, space
)
23 if type(value
) == 'table' then
25 print(space
.. key
.. '= <' .. parsed
[value
] .. '>')
28 print(space
.. key
.. '= {')
30 foreach(value
, function(key
, value
) doPrint(key
, value
, space
) end)
33 if type(value
) == 'string' then
34 value
= '[[' .. tostring(value
) .. ']]'
36 print(space
.. key
.. '=' .. tostring(value
))
42 local function clone(t
)
44 for k
,v
in pairs(t
) do
50 local function inner_merge(t
, remaining
, from
, ...)
51 if remaining
== 0 then
55 for k
,v
in pairs(from
) do
59 return inner_merge(t
, remaining
- 1, ...)
63 Shallow-merges tables in order onto the first table.
65 @param t table to merge entries onto
66 @param ... sequence of 0 or more tables to merge onto 't'
68 @returns table 't' from input
70 local function merge(t
, ...)
71 return inner_merge(t
, select('#', ...), ...)
74 -- Function to insert nulls into the JSON stream
79 -- Marker for 'undefined' values
80 local function undefined()
87 Return's true if the metatable marks it as an array..
88 Or false if it has no array component at all
89 Otherwise nil to get the normal detection component working
91 local function IsArray(value
)
92 if type(value
) ~= 'table' then return false end
93 local meta
= getmetatable(value
)
94 local ret
= meta
== ArrayMT
or (meta
~= nil and meta
.__is_luajson_array
)
96 if #value
== 0 then return false end
101 local function InitArray(array
)
102 setmetatable(array
, ArrayMT
)
108 local function isCall(value
)
109 return CallMT
== getmetatable(value
)
112 local function buildCall(name
, ...)
115 parameters
= {n
= select('#', ...), ...}
117 return setmetatable(callData
, CallMT
)
120 local function decodeCall(callData
)
121 if not isCall(callData
) then return nil end
122 return callData
.name
, callData
.parameters
125 local function doOptionMerge(options
, nested
, name
, defaultOptions
, modeOptions
)
127 modeOptions
= modeOptions
and modeOptions
[name
]
128 defaultOptions
= defaultOptions
and defaultOptions
[name
]
130 options
[name
] = merge(
139 printValue
= printValue
,
143 undefined
= undefined
,
145 InitArray
= InitArray
,
147 buildCall
= buildCall
,
148 decodeCall
= decodeCall
,
149 doOptionMerge
= doOptionMerge