2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local tostring = tostring
7 local util
= require("json.util")
8 local huge
= require("math").huge
10 local is_52
= _VERSION
== "Lua 5.2"
17 local defaultOptions
= {
22 local default
= nil -- Let the buildCapture optimization take place
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 util
.merge({}, defaultOptions
, options
) or defaultOptions
47 number = function(number, state
)
48 return encodeNumber(number, options
)
56 getEncoder
= getEncoder
60 _G
.json
= _G
.json
or {}
61 _G
.json
.encode
= _G
.json
.encode
or {}
62 _G
.json
.encode
.number = number