# HG changeset patch # User Matthew Wild # Date 1667564803 0 # Node ID d63190a7a714a7d07d4f4180de465d2da4cbfb4d # Parent 4f69423603f265ded2495c846ace320b99a6ef25# Parent 997f3ca90628c7e0d57ade24589c5cec3453e577 Merge 0.12->trunk diff -r 4f69423603f2 -r d63190a7a714 spec/json/pass4.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spec/json/pass4.json Fri Nov 04 12:26:43 2022 +0000 @@ -0,0 +1,8 @@ +{ + "one": [ + + ], + "two": [], + "three": [ ], + "four": [ ] +} diff -r 4f69423603f2 -r d63190a7a714 util/json.lua --- a/util/json.lua Mon Oct 31 14:32:26 2022 +0000 +++ b/util/json.lua Fri Nov 04 12:26:43 2022 +0000 @@ -217,12 +217,19 @@ end local function _readarray(json, index) local a = {}; - local oindex = index; while true do - local val; - val, index = _readvalue(json, index + 1); + local val, terminated; + val, index, terminated = _readvalue(json, index + 1, 0x5d); if val == nil then - if json:byte(oindex + 1) == 0x5d then return setmetatable(a, array_mt), oindex + 2; end -- "]" + if terminated then -- "]" found instead of value + if #a ~= 0 then + -- A non-empty array here means we processed a comma, + -- but it wasn't followed by a value. JSON doesn't allow + -- trailing commas. + return nil, "value expected"; + end + val, index = setmetatable(a, array_mt), index+1; + end return val, index; end t_insert(a, val); @@ -294,7 +301,7 @@ end return nil, "false parse failed"; end -function _readvalue(json, index) +function _readvalue(json, index, terminator) index = _skip_whitespace(json, index); local b = json:byte(index); -- TODO try table lookup instead of if-else? @@ -312,6 +319,8 @@ return _readtrue(json, index); elseif b == 0x66 then -- "f" return _readfalse(json, index); + elseif b == terminator then + return nil, index, true; else return nil, "value expected"; end