2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local tostring = tostring
8 local jsonutil
= require("json.util")
9 local util_merge
= require("json.util").merge
12 local is_52
= _VERSION
== "Lua 5.2"
19 -- Shortcut that works
20 local encodeBoolean
= tostring
22 local defaultOptions
= {
23 allowUndefined
= true,
25 undefined
= jsonutil
.undefined
28 local default
= nil -- Let the buildCapture optimization take place
30 allowUndefined
= false
33 local function getEncoder(options
)
34 options
= options
and util_merge({}, defaultOptions
, options
) or defaultOptions
35 local function encodeOthers(value
, state
)
36 if value
== options
.null
then
38 elseif value
== options
.undefined
then
39 assert(options
.allowUndefined
, "Invalid value: Unsupported 'Undefined' parameter")
45 local function encodeBoolean(value
, state
)
46 return value
and 'true' or 'false'
48 local nullType
= type(options
.null
)
49 local undefinedType
= options
.undefined
and type(options
.undefined
)
50 -- Make sure that all of the types handled here are handled
52 boolean
= encodeBoolean
,
53 ['nil'] = function() return 'null' end,
54 [nullType
] = encodeOthers
57 ret
[undefinedType
] = encodeOthers
63 encodeBoolean
= encodeBoolean
,
66 getEncoder
= getEncoder
70 _G
.json
= _G
.json
or {}
71 _G
.json
.encode
= _G
.json
.encode
or {}
72 _G
.json
.encode
.others
= others