decoder: fixes 5.2-strict mode state object
[luajson.git] / lua / json / encode / calls.lua
blob7d0cee23f78e30f81f432fd2b5fdb748ea902c33
1 --[[
2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
4 ]]
5 local table = require("table")
6 local table_concat = table.concat
8 local select = select
9 local getmetatable, setmetatable = getmetatable, setmetatable
10 local assert = assert
12 local jsonutil = require("json.util")
14 local isCall, decodeCall = jsonutil.isCall, jsonutil.decodeCall
16 _ENV = nil
18 local defaultOptions = {
21 -- No real default-option handling needed...
22 local modeOptions = {}
24 local function mergeOptions(options, mode)
25 jsonutil.doOptionMerge(options, false, 'calls', defaultOptions, mode and modeOptions[mode])
26 end
29 --[[
30 Encodes 'value' as a function call
31 Must have parameters in the 'callData' field of the metatable
32 name == name of the function call
33 parameters == array of parameters to encode
35 local function getEncoder(options)
36 options = options and jsonutil.merge({}, defaultOptions, options) or defaultOptions
37 local function encodeCall(value, state)
38 if not isCall(value) then
39 return false
40 end
41 local encode = state.encode
42 local name, params = decodeCall(value)
43 local compositeEncoder = state.outputEncoder.composite
44 local valueEncoder = [[
45 for i = 1, (composite.n or #composite) do
46 local val = composite[i]
47 PUTINNER(i ~= 1)
48 val = encode(val, state)
49 val = val or ''
50 if val then
51 PUTVALUE(val)
52 end
53 end
55 return compositeEncoder(valueEncoder, name .. '(', ')', ',', params, encode, state)
56 end
57 return {
58 table = encodeCall,
59 ['function'] = encodeCall
61 end
63 local calls = {
64 mergeOptions = mergeOptions,
65 getEncoder = getEncoder
68 return calls