2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local table = require("table")
6 local table_concat
= table.concat
9 local getmetatable
, setmetatable
= getmetatable
, setmetatable
12 local jsonutil
= require("json.util")
14 local isCall
, decodeCall
= jsonutil
.isCall
, jsonutil
.decodeCall
18 local defaultOptions
= {
21 -- No real default-option handling needed...
22 local modeOptions
= {}
24 local function mergeOptions(options
, mode
)
25 jsonutil
.doOptionMerge(options
, false, 'calls', defaultOptions
, mode
and modeOptions
[mode
])
30 Encodes 'value' as a function call
31 Must have parameters in the 'callData' field of the metatable
32 name == name of the function call
33 parameters == array of parameters to encode
35 local function getEncoder(options
)
36 options
= options
and jsonutil
.merge({}, defaultOptions
, options
) or defaultOptions
37 local function encodeCall(value
, state
)
38 if not isCall(value
) then
41 local encode
= state
.encode
42 local name
, params
= decodeCall(value
)
43 local compositeEncoder
= state
.outputEncoder
.composite
44 local valueEncoder
= [[
45 for i = 1, (composite.n or #composite) do
46 local val = composite[i]
48 val = encode(val, state)
55 return compositeEncoder(valueEncoder
, name
.. '(', ')', ',', params
, encode
, state
)
59 ['function'] = encodeCall
64 mergeOptions
= mergeOptions
,
65 getEncoder
= getEncoder