From ffb089a9f5fa95fd559a7c88081310d0be73f206 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Fri, 8 Jun 2018 17:43:13 +1200 Subject: [PATCH] Fix json parser comma after empty object case --- std/json.zig | 18 +++++++++++++----- std/json_test.zig | 10 ++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/std/json.zig b/std/json.zig index 6cf83eef1a..03b19a7fa4 100644 --- a/std/json.zig +++ b/std/json.zig @@ -324,7 +324,9 @@ pub const StreamingParser = struct { p.complete = true; p.state = State.TopLevelEnd; }, - else => {}, + else => { + p.state = State.ValueEnd; + }, } token.* = Token.initMarker(Token.Id.ObjectEnd); @@ -348,7 +350,9 @@ pub const StreamingParser = struct { p.complete = true; p.state = State.TopLevelEnd; }, - else => {}, + else => { + p.state = State.ValueEnd; + }, } token.* = Token.initMarker(Token.Id.ArrayEnd); @@ -970,7 +974,7 @@ pub fn validate(s: []const u8) bool { var token1: ?Token = undefined; var token2: ?Token = undefined; - p.feed(c, *token1, *token2) catch |err| { + p.feed(c, &token1, &token2) catch |err| { return false; }; } @@ -978,6 +982,10 @@ pub fn validate(s: []const u8) bool { return p.complete; } +test "json validate" { + debug.assert(validate("{}")); +} + const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const ArrayList = std.ArrayList; @@ -1230,7 +1238,7 @@ pub const Parser = struct { _ = p.stack.pop(); p.state = State.ObjectKey; }, - else => { + Token.Id.ObjectEnd, Token.Id.ArrayEnd => { unreachable; }, } @@ -1270,7 +1278,7 @@ pub const Parser = struct { Token.Id.Null => { try array.append(Value.Null); }, - else => { + Token.Id.ObjectEnd => { unreachable; }, } diff --git a/std/json_test.zig b/std/json_test.zig index cb054d8e4e..8c8862441a 100644 --- a/std/json_test.zig +++ b/std/json_test.zig @@ -17,6 +17,16 @@ fn any(comptime s: []const u8) void { std.debug.assert(true); } +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Additional tests not part of test JSONTestSuite. + +test "y_trailing_comma_after_empty" { + ok( + \\{"1":[],"2":{},"3":"4"} + ); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// test "y_array_arraysWithSpaces" {