Skip to content

Commit ffb089a

Browse files
committedJun 8, 2018
Fix json parser comma after empty object case
·
0.15.20.3.0
1 parent f0b6dac commit ffb089a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed
 

‎std/json.zig‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,14 +974,18 @@ 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
}
977981

978982
return p.complete;
979983
}
980984

985+
test "json validate" {
986+
debug.assert(validate("{}"));
987+
}
988+
981989
const Allocator = std.mem.Allocator;
982990
const ArenaAllocator = std.heap.ArenaAllocator;
983991
const 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‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
24+
test "y_trailing_comma_after_empty" {
25+
ok(
26+
\\{"1":[],"2":{},"3":"4"}
27+
);
28+
}
29+
2030
////////////////////////////////////////////////////////////////////////////////////////////////////
2131

2232
test "y_array_arraysWithSpaces" {

0 commit comments

Comments
 (0)
Please sign in to comment.