Skip to content

Commit

Permalink
Fix json parser comma after empty object case
Browse files Browse the repository at this point in the history
  • Loading branch information
tiehuis committed Jun 8, 2018
1 parent f0b6dac commit ffb089a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions std/json.zig
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -970,14 +974,18 @@ 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;
};
}

return p.complete;
}

test "json validate" {
debug.assert(validate("{}"));
}

const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const ArrayList = std.ArrayList;
Expand Down Expand Up @@ -1230,7 +1238,7 @@ pub const Parser = struct {
_ = p.stack.pop();
p.state = State.ObjectKey;
},
else => {
Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
unreachable;
},
}
Expand Down Expand Up @@ -1270,7 +1278,7 @@ pub const Parser = struct {
Token.Id.Null => {
try array.append(Value.Null);
},
else => {
Token.Id.ObjectEnd => {
unreachable;
},
}
Expand Down
10 changes: 10 additions & 0 deletions std/json_test.zig
Expand Up @@ -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" {
Expand Down

0 comments on commit ffb089a

Please sign in to comment.