Commit ffb089a9f5

Marc Tiehuis <marctiehuis@gmail.com>
2018-06-08 07:43:13
Fix json parser comma after empty object case
1 parent f0b6dac
Changed files (2)
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;
                     },
                 }
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" {