@@ -234,6 +234,7 @@ pub enum ErrorCode {
234
234
KeyMustBeAString ,
235
235
ExpectedColon ,
236
236
TrailingCharacters ,
237
+ TrailingComma ,
237
238
InvalidEscape ,
238
239
InvalidUnicodeCodePoint ,
239
240
LoneLeadingSurrogateInHexEscape ,
@@ -274,6 +275,7 @@ pub fn error_str(error: ErrorCode) -> &'static str {
274
275
KeyMustBeAString => "key must be a string" ,
275
276
ExpectedColon => "expected `:`" ,
276
277
TrailingCharacters => "trailing characters" ,
278
+ TrailingComma => "trailing comma" ,
277
279
InvalidEscape => "invalid escape" ,
278
280
UnrecognizedHex => "invalid \\ u escape (unrecognized hex)" ,
279
281
NotFourDigit => "invalid \\ u escape (not four digits)" ,
@@ -1681,7 +1683,11 @@ impl<T: Iterator<char>> Parser<T> {
1681
1683
fn parse_object ( & mut self , first : bool ) -> JsonEvent {
1682
1684
if self . ch_is ( '}' ) {
1683
1685
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
+ }
1685
1691
}
1686
1692
if self . stack . is_empty ( ) {
1687
1693
self . state = ParseBeforeFinish ;
@@ -2377,7 +2383,7 @@ mod tests {
2377
2383
F64Value , StringValue , NullValue , SyntaxError , Key , Index , Stack ,
2378
2384
InvalidSyntax , InvalidNumber , EOFWhileParsingObject , EOFWhileParsingList ,
2379
2385
EOFWhileParsingValue , EOFWhileParsingString , KeyMustBeAString , ExpectedColon ,
2380
- TrailingCharacters } ;
2386
+ TrailingCharacters , TrailingComma } ;
2381
2387
use std:: { i64, u64, f32, f64, io} ;
2382
2388
use std:: collections:: TreeMap ;
2383
2389
@@ -3379,6 +3385,7 @@ mod tests {
3379
3385
}
3380
3386
}
3381
3387
}
3388
+
3382
3389
#[ test]
3383
3390
#[ ignore( cfg( target_word_size = "32" ) ) ] // FIXME(#14064)
3384
3391
fn test_read_object_streaming ( ) {
@@ -3393,6 +3400,7 @@ mod tests {
3393
3400
assert_eq ! ( last_event( "{\" a\" :1" ) , Error ( SyntaxError ( EOFWhileParsingObject , 1 , 7 ) ) ) ;
3394
3401
assert_eq ! ( last_event( "{\" a\" :1 1" ) , Error ( SyntaxError ( InvalidSyntax , 1 , 8 ) ) ) ;
3395
3402
assert_eq ! ( last_event( "{\" a\" :1," ) , Error ( SyntaxError ( EOFWhileParsingObject , 1 , 8 ) ) ) ;
3403
+ assert_eq ! ( last_event( "{\" a\" :1,}" ) , Error ( SyntaxError ( TrailingComma , 1 , 8 ) ) ) ;
3396
3404
3397
3405
assert_stream_equal (
3398
3406
"{}" ,
0 commit comments