2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
9 local tostring = tostring
11 local table_concat
= require("table").concat
12 local jsonutil
= require("json.util")
16 local defaultOptions
= {
19 local modeOptions
= {}
21 local function mergeOptions(options
, mode
)
22 jsonutil
.doOptionMerge(options
, false, 'object', defaultOptions
, mode
and modeOptions
[mode
])
26 Cleanup function to unmark a value as in the encoding process and return
29 local function unmarkAfterEncode(tab
, state
, ...)
30 state
.already_encoded
[tab
] = nil
34 Encode a table as a JSON Object ( keys = strings, values = anything else )
36 local function encodeTable(tab
, options
, state
)
37 -- Make sure this value hasn't been encoded yet
38 state
.check_unique(tab
)
39 local encode
= state
.encode
40 local compositeEncoder
= state
.outputEncoder
.composite
41 local valueEncoder
= [[
43 for k, v in pairs(composite) do
45 assert(ti == 'string' or ti == 'number' or ti == 'boolean', "Invalid object index type: " .. ti)
46 local name = encode(tostring(k), state, true)
53 local val = encode(v, state)
60 return unmarkAfterEncode(tab
, state
, compositeEncoder(valueEncoder
, '{', '}', nil, tab
, encode
, state
))
63 local function getEncoder(options
)
64 options
= options
and jsonutil
.merge({}, defaultOptions
, options
) or defaultOptions
66 table = function(tab
, state
)
67 return encodeTable(tab
, options
, state
)
73 mergeOptions
= mergeOptions
,
74 getEncoder
= getEncoder