2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local tostring = tostring
7 local jsonutil
= require("json.util")
8 local huge
= require("math").huge
12 local defaultOptions
= {
17 local modeOptions
= {}
18 modeOptions
.strict
= {
23 local function mergeOptions(options
, mode
)
24 jsonutil
.doOptionMerge(options
, false, 'number', defaultOptions
, mode
and modeOptions
[mode
])
28 local function encodeNumber(number, options
)
29 if number ~= number then
30 assert(options
.nan
, "Invalid number: NaN not enabled")
33 if number == huge
then
34 assert(options
.inf
, "Invalid number: Infinity not enabled")
37 if number == -huge
then
38 assert(options
.inf
, "Invalid number: Infinity not enabled")
41 return tostring(number)
44 local function getEncoder(options
)
45 options
= options
and jsonutil
.merge({}, defaultOptions
, options
) or defaultOptions
47 number = function(number, state
)
48 return encodeNumber(number, options
)
54 mergeOptions
= mergeOptions
,
55 getEncoder
= getEncoder