1 local jsonencode
= require("json.encode")
7 local table_concat
= table.concat
8 local math_floor
, math_modf
= math
.floor, math
.modf
10 module("json.encode.array")
12 function isArray(val
, options
)
13 local externalIsArray
= options
and options
.array
and options
.array
.isArray
14 local isEncodable
= jsonencode
.isEncodable
16 if externalIsArray
then
17 local ret
= externalIsArray(val
)
18 if ret
== true or ret
== false then
22 -- Use the 'n' element if it's a number
23 if type(val
.n
) == 'number' and math_floor(val
.n
) == val
.n
and val
.n
>= 1 then
27 for k
,v
in pairs(val
) do
28 if type(k
) ~= 'number' then
31 local _
, decim
= math_modf(k
)
32 if not (decim
== 0 and 1<=k
) then
35 assert(isEncodable(v
, options
), "Invalid array element type:" .. type(v
))
36 if k
> len
then -- Use Lua's length as absolute determiner
44 function encode(tab
, options
)
45 local encodeValue
= jsonencode
.encodeValue
47 for i
= 1,(tab
.n
or #tab
) do
48 retVal
[#retVal
+ 1] = encodeValue(tab
[i
], options
)
50 return '[' .. table_concat(retVal
, ',') .. ']'