authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-06-08 17:43:13+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-06-08 17:43:13+12:00
logffb089a9f5fa95fd559a7c88081310d0be73f206
tree60dbb1ed07fc878205778d2bd20617b24eabbf87
parentf0b6dac1f2d37ea9eff0116bec34e9b2be9f3ce7

Fix json parser comma after empty object case


2 files changed, 23 insertions(+), 5 deletions(-)

std/json.zig+13-5
......@@ -324,7 +324,9 @@ pub const StreamingParser = struct {
324324 p.complete = true;
325325 p.state = State.TopLevelEnd;
326326 },
327 else => {},
327 else => {
328 p.state = State.ValueEnd;
329 },
328330 }
329331
330332 token.* = Token.initMarker(Token.Id.ObjectEnd);
......@@ -348,7 +350,9 @@ pub const StreamingParser = struct {
348350 p.complete = true;
349351 p.state = State.TopLevelEnd;
350352 },
351 else => {},
353 else => {
354 p.state = State.ValueEnd;
355 },
352356 }
353357
354358 token.* = Token.initMarker(Token.Id.ArrayEnd);
......@@ -970,7 +974,7 @@ pub fn validate(s: []const u8) bool {
970974 var token1: ?Token = undefined;
971975 var token2: ?Token = undefined;
972976
973 p.feed(c, *token1, *token2) catch |err| {
977 p.feed(c, &token1, &token2) catch |err| {
974978 return false;
975979 };
976980 }
......@@ -978,6 +982,10 @@ pub fn validate(s: []const u8) bool {
978982 return p.complete;
979983}
980984
985test "json validate" {
986 debug.assert(validate("{}"));
987}
988
981989const Allocator = std.mem.Allocator;
982990const ArenaAllocator = std.heap.ArenaAllocator;
983991const ArrayList = std.ArrayList;
......@@ -1230,7 +1238,7 @@ pub const Parser = struct {
12301238 _ = p.stack.pop();
12311239 p.state = State.ObjectKey;
12321240 },
1233 else => {
1241 Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
12341242 unreachable;
12351243 },
12361244 }
......@@ -1270,7 +1278,7 @@ pub const Parser = struct {
12701278 Token.Id.Null => {
12711279 try array.append(Value.Null);
12721280 },
1273 else => {
1281 Token.Id.ObjectEnd => {
12741282 unreachable;
12751283 },
12761284 }
std/json_test.zig+10
......@@ -17,6 +17,16 @@ fn any(comptime s: []const u8) void {
1717 std.debug.assert(true);
1818}
1919
20////////////////////////////////////////////////////////////////////////////////////////////////////
21//
22// Additional tests not part of test JSONTestSuite.
23
24test "y_trailing_comma_after_empty" {
25 ok(
26 \\{"1":[],"2":{},"3":"4"}
27 );
28}
29
2030////////////////////////////////////////////////////////////////////////////////////////////////////
2131
2232test "y_array_arraysWithSpaces" {