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, ...)
62 local function merge(t
, ...)
63 return inner_merge(t
, select('#', ...), ...)
66 -- Function to insert nulls into the JSON stream
71 -- Marker for 'undefined' values
72 local function undefined()
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
87 if #value
== 0 then return false end
92 local function InitArray(array
)
93 setmetatable(array
, ArrayMT
)
99 local function isCall(value
)
100 return CallMT
== getmetatable(value
)
103 local function buildCall(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
)
118 modeOptions
= modeOptions
and modeOptions
[name
]
119 defaultOptions
= defaultOptions
and defaultOptions
[name
]
121 options
[name
] = merge(
130 printValue
= printValue
,
134 undefined
= undefined
,
136 InitArray
= InitArray
,
138 buildCall
= buildCall
,
139 decodeCall
= decodeCall
,
140 doOptionMerge
= doOptionMerge