Skip to content

Commit e8a3ac5

Browse files
committed
rollup merge of #17276 : treeman/json-comma
2 parents 27af691 + fb299bd commit e8a3ac5

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libserialize/json.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub enum ErrorCode {
234234
KeyMustBeAString,
235235
ExpectedColon,
236236
TrailingCharacters,
237+
TrailingComma,
237238
InvalidEscape,
238239
InvalidUnicodeCodePoint,
239240
LoneLeadingSurrogateInHexEscape,
@@ -274,6 +275,7 @@ pub fn error_str(error: ErrorCode) -> &'static str {
274275
KeyMustBeAString => "key must be a string",
275276
ExpectedColon => "expected `:`",
276277
TrailingCharacters => "trailing characters",
278+
TrailingComma => "trailing comma",
277279
InvalidEscape => "invalid escape",
278280
UnrecognizedHex => "invalid \\u escape (unrecognized hex)",
279281
NotFourDigit => "invalid \\u escape (not four digits)",
@@ -1681,7 +1683,11 @@ impl<T: Iterator<char>> Parser<T> {
16811683
fn parse_object(&mut self, first: bool) -> JsonEvent {
16821684
if self.ch_is('}') {
16831685
if !first {
1684-
self.stack.pop();
1686+
if self.stack.is_empty() {
1687+
return self.error_event(TrailingComma);
1688+
} else {
1689+
self.stack.pop();
1690+
}
16851691
}
16861692
if self.stack.is_empty() {
16871693
self.state = ParseBeforeFinish;
@@ -2377,7 +2383,7 @@ mod tests {
23772383
F64Value, StringValue, NullValue, SyntaxError, Key, Index, Stack,
23782384
InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingList,
23792385
EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon,
2380-
TrailingCharacters};
2386+
TrailingCharacters, TrailingComma};
23812387
use std::{i64, u64, f32, f64, io};
23822388
use std::collections::TreeMap;
23832389

@@ -3379,6 +3385,7 @@ mod tests {
33793385
}
33803386
}
33813387
}
3388+
33823389
#[test]
33833390
#[ignore(cfg(target_word_size = "32"))] // FIXME(#14064)
33843391
fn test_read_object_streaming() {
@@ -3393,6 +3400,7 @@ mod tests {
33933400
assert_eq!(last_event("{\"a\":1"), Error(SyntaxError(EOFWhileParsingObject, 1, 7)));
33943401
assert_eq!(last_event("{\"a\":1 1"), Error(SyntaxError(InvalidSyntax, 1, 8)));
33953402
assert_eq!(last_event("{\"a\":1,"), Error(SyntaxError(EOFWhileParsingObject, 1, 8)));
3403+
assert_eq!(last_event("{\"a\":1,}"), Error(SyntaxError(TrailingComma, 1, 8)));
33963404

33973405
assert_stream_equal(
33983406
"{}",

0 commit comments

Comments
 (0)