File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -308,8 +308,12 @@ macro_rules! array {
308308/// ```
309309#[ macro_export]
310310macro_rules! object {
311+ // Empty object.
311312 { } => ( $crate:: JsonValue :: new_object( ) ) ;
312313
314+ // Non-empty object, no trailing comma.
315+ //
316+ // In this implementation, key/value pairs separated by commas.
313317 { $( $key: expr => $value: expr ) ,* } => ( {
314318 use $crate:: object:: Object ;
315319
@@ -319,6 +323,21 @@ macro_rules! object {
319323 object. insert( $key, $value. into( ) ) ;
320324 ) *
321325
326+ $crate:: JsonValue :: Object ( object)
327+ } ) ;
328+
329+ // Non-empty object, trailing comma.
330+ //
331+ // In this implementation, the comma is part of the value.
332+ { $( $key: expr => $value: expr, ) * } => ( {
333+ use $crate:: object:: Object ;
334+
335+ let mut object = Object :: new( ) ;
336+
337+ $(
338+ object. insert( $key, $value. into( ) ) ;
339+ ) *
340+
322341 $crate:: JsonValue :: Object ( object)
323342 } )
324343}
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ fn parse_array() {
126126
127127#[ test]
128128fn parse_object ( ) {
129+ // Without trailing comma
129130 assert_eq ! ( parse( r#"
130131
131132 {
@@ -137,6 +138,19 @@ fn parse_object() {
137138 "foo" => "bar" ,
138139 "num" => 10
139140 } ) ;
141+
142+ // Trailing comma in macro
143+ assert_eq ! ( parse( r#"
144+
145+ {
146+ "foo": "bar",
147+ "num": 10
148+ }
149+
150+ "# ) . unwrap( ) , object!{
151+ "foo" => "bar" ,
152+ "num" => 10 ,
153+ } ) ;
140154}
141155
142156#[ test]
You can’t perform that action at this time.
0 commit comments