@@ -64,32 +64,43 @@ local function encode_table(val, stack)
64
64
if stack [val ] then error (" circular reference" ) end
65
65
66
66
stack [val ] = true
67
+ -- Check whether to treat as a array or object
68
+ local array = true
69
+ local length = 0
70
+ local nLen = 0
71
+ for k ,v in pairs (val ) do
72
+ if (type (k ) ~= " number" or k <= 0 ) and not (k == " n" and type (v ) == " number" ) then
73
+ array = nil
74
+ break -- Treat as object
75
+ else
76
+ if k > length then
77
+ length = k
78
+ end
79
+ if k == " n" and type (v ) == " number" then
80
+ nLen = v
81
+ end
82
+ end
83
+ end
67
84
68
- if rawget (val , 1 ) ~= nil or next (val ) == nil then
69
- -- Treat as array -- check keys are valid and it is not sparse
70
- local n = 0
71
- for k in pairs (val ) do
72
- if type (k ) ~= " number" then
73
- error (" invalid table: mixed or invalid key types" )
74
- end
75
- n = n + 1
76
- end
77
- if n ~= # val then
78
- error (" invalid table: sparse array" )
79
- end
85
+ if array then
86
+ if nLen > length then
87
+ length = nLen
88
+ end
80
89
-- Encode
81
- for i , v in ipairs ( val ) do
82
- table.insert (res , encode (v , stack ))
90
+ for i = 1 , length do
91
+ table.insert (res , encode (val [ i ] , stack ))
83
92
end
84
93
stack [val ] = nil
85
94
return " [" .. table.concat (res , " ," ) .. " ]"
86
95
87
96
else
88
97
-- Treat as an object
89
98
for k , v in pairs (val ) do
99
+ --[[
90
100
if type(k) ~= "string" then
91
101
error("invalid table: mixed or invalid key types")
92
102
end
103
+ ]]
93
104
table.insert (res , encode (k , stack ) .. " :" .. encode (v , stack ))
94
105
end
95
106
stack [val ] = nil
@@ -108,7 +119,7 @@ local function encode_number(val)
108
119
if val ~= val or val <= - math.huge or val >= math.huge then
109
120
error (" unexpected number value '" .. tostring (val ) .. " '" )
110
121
end
111
- return string.format ( " %.14g " , val )
122
+ return tostring ( val )
112
123
end
113
124
114
125
0 commit comments