| ... | ... | @@ -375,7 +375,7 @@ pub const StreamingParser = struct { |
| 375 | 375 | '}' => { |
| 376 | 376 | // unlikely |
| 377 | 377 | if (p.stack & 1 != object_bit) { |
| 378 | | return error.UnexpectedClosingBracket; |
| 378 | return error.UnexpectedClosingBrace; |
| 379 | 379 | } |
| 380 | 380 | if (p.stack_used == 0) { |
| 381 | 381 | return error.TooManyClosingItems; |
| ... | ... | @@ -401,7 +401,7 @@ pub const StreamingParser = struct { |
| 401 | 401 | }, |
| 402 | 402 | ']' => { |
| 403 | 403 | if (p.stack & 1 != array_bit) { |
| 404 | | return error.UnexpectedClosingBrace; |
| 404 | return error.UnexpectedClosingBracket; |
| 405 | 405 | } |
| 406 | 406 | if (p.stack_used == 0) { |
| 407 | 407 | return error.TooManyClosingItems; |
| ... | ... | @@ -571,8 +571,11 @@ pub const StreamingParser = struct { |
| 571 | 571 | p.state = .ValueBeginNoClosing; |
| 572 | 572 | }, |
| 573 | 573 | ']' => { |
| 574 | if (p.stack & 1 != array_bit) { |
| 575 | return error.UnexpectedClosingBracket; |
| 576 | } |
| 574 | 577 | if (p.stack_used == 0) { |
| 575 | | return error.UnbalancedBrackets; |
| 578 | return error.TooManyClosingItems; |
| 576 | 579 | } |
| 577 | 580 | |
| 578 | 581 | p.state = .ValueEnd; |
| ... | ... | @@ -589,8 +592,12 @@ pub const StreamingParser = struct { |
| 589 | 592 | token.* = Token.ArrayEnd; |
| 590 | 593 | }, |
| 591 | 594 | '}' => { |
| 595 | // unlikely |
| 596 | if (p.stack & 1 != object_bit) { |
| 597 | return error.UnexpectedClosingBrace; |
| 598 | } |
| 592 | 599 | if (p.stack_used == 0) { |
| 593 | | return error.UnbalancedBraces; |
| 600 | return error.TooManyClosingItems; |
| 594 | 601 | } |
| 595 | 602 | |
| 596 | 603 | p.state = .ValueEnd; |
| ... | ... | @@ -1189,6 +1196,15 @@ test "json.token" { |
| 1189 | 1196 | testing.expect((try p.next()) == null); |
| 1190 | 1197 | } |
| 1191 | 1198 | |
| 1199 | test "json.token mismatched close" { |
| 1200 | var p = TokenStream.init("[102, 111, 111 }"); |
| 1201 | checkNext(&p, .ArrayBegin); |
| 1202 | checkNext(&p, .Number); |
| 1203 | checkNext(&p, .Number); |
| 1204 | checkNext(&p, .Number); |
| 1205 | testing.expectError(error.UnexpectedClosingBrace, p.next()); |
| 1206 | } |
| 1207 | |
| 1192 | 1208 | /// Validate a JSON string. This does not limit number precision so a decoder may not necessarily |
| 1193 | 1209 | /// be able to decode the string even if this returns true. |
| 1194 | 1210 | pub fn validate(s: []const u8) bool { |
| ... | ... | @@ -1207,7 +1223,12 @@ pub fn validate(s: []const u8) bool { |
| 1207 | 1223 | } |
| 1208 | 1224 | |
| 1209 | 1225 | test "json.validate" { |
| 1210 | | testing.expect(validate("{}")); |
| 1226 | testing.expectEqual(true, validate("{}")); |
| 1227 | testing.expectEqual(true, validate("[]")); |
| 1228 | testing.expectEqual(true, validate("[{[[[[{}]]]]}]")); |
| 1229 | testing.expectEqual(false, validate("{]")); |
| 1230 | testing.expectEqual(false, validate("[}")); |
| 1231 | testing.expectEqual(false, validate("{{{{[]}}}]")); |
| 1211 | 1232 | } |
| 1212 | 1233 | |
| 1213 | 1234 | const Allocator = std.mem.Allocator; |