2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
6 local setmetatable
= setmetatable
7 local jsonutil
= require("json.util")
20 function state_ops
.pop(self
)
21 self
.previous_set
= true
22 self
.previous
= self
.active
24 -- Load in this array into the active item
25 self
.active
= self
.stack
[i
]
26 self
.active_state
= self
.state_stack
[i
]
27 self
.active_key
= self
.key_stack
[i
]
29 self
.state_stack
[i
] = nil
30 self
.key_stack
[i
] = nil
35 function state_ops
.push(self
)
39 self
.stack
[i
] = self
.active
40 self
.state_stack
[i
] = self
.active_state
41 self
.key_stack
[i
] = self
.active_key
44 function state_ops
.put_object_value(self
, trailing
)
45 local object_options
= self
.options
.object
46 if trailing
and object_options
.trailingComma
then
47 if not self
.active_key
then
51 assert(self
.active_key
, "Missing key value")
52 object_options
.setObjectKey(self
.active
, self
.active_key
, self
:grab_value())
56 function state_ops
.put_array_value(self
, trailing
)
58 if trailing
and not self
.previous_set
and self
.options
.array
.trailingComma
then
61 local new_index
= self
.active_state
+ 1
62 self
.active_state
= new_index
63 self
.active
[new_index
] = self
:grab_value()
66 function state_ops
.put_value(self
, trailing
)
67 if self
.active_state
== 'object' then
68 self
:put_object_value(trailing
)
70 self
:put_array_value(trailing
)
74 function state_ops
.new_array(self
)
76 if jsonutil
.InitArray
then
77 new_array
= jsonutil
.InitArray(new_array
) or new_array
79 self
.active
= new_array
85 function state_ops
.end_array(self
)
86 if self
.previous_set
or self
.active_state
~= 0 then
90 if self
.active_state
~= #self
.active
then
91 -- Store the length in
92 self
.active
.n
= self
.active_state
96 function state_ops
.new_object(self
)
98 self
.active
= new_object
99 self
.active_state
= 'object'
100 self
.active_key
= nil
104 function state_ops
.end_object(self
)
105 if self
.previous_set
or next(self
.active
) then
106 -- Not an empty object
111 function state_ops
.new_call(self
, name
, func
)
112 -- TODO setup properly
116 self
.active
= new_call
117 self
.active_state
= 0
118 self
.active_key
= nil
122 function state_ops
.end_call(self
)
123 if self
.previous_set
or self
.active_state
~= 0 then
124 -- Not an empty array
127 if self
.active_state
~= #self
.active
then
128 -- Store the length in
129 self
.active
.n
= self
.active_state
131 local func
= self
.active
.func
133 func
= jsonutil
.buildCall
135 self
.active
= func(self
.active
.name
, unpack(self
.active
, 1, self
.active
.n
or #self
.active
))
139 function state_ops
.unset_value(self
)
140 self
.previous_set
= false
144 function state_ops
.grab_value(self
)
145 assert(self
.previous_set
, "Previous value not set")
146 self
.previous_set
= false
150 function state_ops
.set_value(self
, value
)
151 assert(not self
.previous_set
, "Value set when one already in slot")
152 self
.previous_set
= true
153 self
.previous
= value
156 function state_ops
.set_key(self
)
157 assert(self
.active_state
== 'object', "Cannot set key on array")
158 local value
= self
:grab_value()
159 local value_type
= type(value
)
160 if self
.options
.object
.number then
161 assert(value_type
== 'string' or value_type
== 'number', "As configured, a key must be a number or string")
163 assert(value_type
== 'string', "As configured, a key must be a string")
165 self
.active_key
= value
169 local function create(options
)
182 return setmetatable(ret
, state_mt
)