Skip to content

Commit 3e4ab2e

Browse files
committed
Fixes for null and numeric numbered tables
1 parent 0f9b02c commit 3e4ab2e

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

examples/tests.lua

+23-5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ function testJSON4Lua()
102102
-- NB: This test can fail because of order: need to test further once
103103
-- decoding is supported.
104104
-- assert(r==[[{"age":35,"Name":"Craig","email":"[email protected]"}]])
105+
106+
-- Test encoding tables with numeric (string) indexes
107+
s = {}
108+
s['1']='One'
109+
r = json.encode(s)
110+
-- print("r = ", r)
111+
assert(r=='{"1":"One"}')
112+
113+
s['2']= {One='Uno'}
114+
r = json.encode(s)
115+
assert(compareData(json.decode(r), s))
105116

106117
-- Test decode_scanWhitespace
107118
if nil then
@@ -122,7 +133,6 @@ function testJSON4Lua()
122133
s = [["Test\u00A7\\"]]
123134
r,e = json._decode_scanString(s,1)
124135
assert(r=="Test\xC2\xA7\\" and e==9)
125-
print(s,r)
126136

127137
-- Test decode_scanNumber
128138
s = [[354]]
@@ -165,14 +175,13 @@ function testJSON4Lua()
165175
s = [["Test\u00A7\\\""]]
166176
r,e = json.decode(s)
167177
assert(r=="Test\xC2\xA7\\\"", r)
168-
print(s,r)
169178

170179
-- Test decode_scanObject
171180
s = [[ {"one":1, "two":2, "three":"three", "four":true} ]]
172181
r,e = json.decode(s)
173-
for x,y in pairs(r) do
174-
print(x,y)
175-
end
182+
-- for x,y in pairs(r) do
183+
-- print(x,y)
184+
-- end
176185
assert(compareData(r,{one=1,two=2,three='three',four=true}))
177186
s = [[ { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } ]]
178187
r,e = json.decode(s)
@@ -198,6 +207,15 @@ function testJSON4Lua()
198207
]]
199208
r,e = json.decode(s)
200209
assert(r=='test',"Comment decoding failed")
210+
211+
-- Per error reported by M.Hund, with incorrect decoding of string-numbered tables
212+
s = {}
213+
subt = {a="a",b="b",c="c"}
214+
s['1'] = subt
215+
s['2'] = subt
216+
s['3'] = subt
217+
r = json.decode('{"1":{"a":"a","b":"b","c":"c"},"2":{"a":"a","b":"b","c":"c"},"3":{"a":"a","b":"b","c":"c"}}')
218+
assert(compareData(s, r))
201219
end
202220

203221
testJSON4Lua()

json/json.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function json.encode (v)
105105
end
106106

107107
-- Handle null values
108-
if vtype=='function' and v==null then
108+
if vtype=='function' and v==json.null then
109109
return 'null'
110110
end
111111

@@ -141,16 +141,16 @@ function json.decode(s, startPos)
141141
return decode_scanString(s,startPos)
142142
end
143143
if string.sub(s,startPos,startPos+1)=='/*' then
144-
return decode(s, decode_scanComment(s,startPos))
144+
return json.decode(s, decode_scanComment(s,startPos))
145145
end
146146
-- Otherwise, it must be a constant
147147
return decode_scanConstant(s,startPos)
148148
end
149149

150150
--- The null function allows one to specify a null value in an associative array (which is otherwise
151151
-- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
152-
function null()
153-
return null -- so json.null() will also return null ;-)
152+
function json.null()
153+
return json.null -- so json.null() will also return null ;-)
154154
end
155155
-----------------------------------------------------------------------------
156156
-- Internal, PRIVATE functions.
@@ -418,7 +418,8 @@ end
418418
-- @return boolean True if the object should be JSON encoded, false if it should be ignored.
419419
function isEncodable(o)
420420
local t = type(o)
421-
return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
421+
return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or
422+
(t=='function' and o==json.null)
422423
end
423424

424425
return json

0 commit comments

Comments
 (0)