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 {
308
308
/// ```
309
309
#[ macro_export]
310
310
macro_rules! object {
311
+ // Empty object.
311
312
{ } => ( $crate:: JsonValue :: new_object( ) ) ;
312
313
314
+ // Non-empty object, no trailing comma.
315
+ //
316
+ // In this implementation, key/value pairs separated by commas.
313
317
{ $( $key: expr => $value: expr ) ,* } => ( {
314
318
use $crate:: object:: Object ;
315
319
@@ -319,6 +323,21 @@ macro_rules! object {
319
323
object. insert( $key, $value. into( ) ) ;
320
324
) *
321
325
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
+
322
341
$crate:: JsonValue :: Object ( object)
323
342
} )
324
343
}
Original file line number Diff line number Diff line change @@ -122,6 +122,7 @@ fn parse_array() {
122
122
123
123
#[ test]
124
124
fn parse_object ( ) {
125
+ // Without trailing comma
125
126
assert_eq ! ( parse( r#"
126
127
127
128
{
@@ -133,6 +134,19 @@ fn parse_object() {
133
134
"foo" => "bar" ,
134
135
"num" => 10
135
136
} ) ;
137
+
138
+ // Trailing comma in macro
139
+ assert_eq ! ( parse( r#"
140
+
141
+ {
142
+ "foo": "bar",
143
+ "num": 10
144
+ }
145
+
146
+ "# ) . unwrap( ) , object!{
147
+ "foo" => "bar" ,
148
+ "num" => 10 ,
149
+ } ) ;
136
150
}
137
151
138
152
#[ test]
You can’t perform that action at this time.
0 commit comments