tests: adds positive test case with nothrow to enhance test coverage
[luajson.git] / lua / json / decode / others.lua
blob9fab7a83e91d965d177d516bc130e05ae4f48365
1 --[[
2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
4 ]]
5 local lpeg = require("lpeg")
6 local jsonutil = require("json.util")
7 local merge = jsonutil.merge
8 local util = require("json.decode.util")
10 -- Container module for other JavaScript types (bool, null, undefined)
12 local _ENV = nil
14 -- For null and undefined, use the util.null value to preserve null-ness
15 local booleanCapture =
16 lpeg.P("true") * lpeg.Cc(true)
17 + lpeg.P("false") * lpeg.Cc(false)
19 local nullCapture = lpeg.P("null")
20 local undefinedCapture = lpeg.P("undefined")
22 local defaultOptions = {
23 allowUndefined = true,
24 null = jsonutil.null,
25 undefined = jsonutil.undefined
28 local modeOptions = {}
30 modeOptions.simple = {
31 null = false, -- Mapped to nil
32 undefined = false -- Mapped to nil
34 modeOptions.strict = {
35 allowUndefined = false
38 local function mergeOptions(options, mode)
39 jsonutil.doOptionMerge(options, false, 'others', defaultOptions, mode and modeOptions[mode])
40 end
42 local function generateLexer(options)
43 -- The 'or nil' clause allows false to map to a nil value since 'nil' cannot be merged
44 options = options.others
45 local valueCapture = (
46 booleanCapture
47 + nullCapture * lpeg.Cc(options.null or nil)
49 if options.allowUndefined then
50 valueCapture = valueCapture + undefinedCapture * lpeg.Cc(options.undefined or nil)
51 else
52 valueCapture = valueCapture + #undefinedCapture * util.denied("undefined", "others.allowUndefined")
53 end
54 return valueCapture
55 end
57 local others = {
58 mergeOptions = mergeOptions,
59 generateLexer = generateLexer
62 return others