File tree 2 files changed +33
-0
lines changed
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 @@ -126,6 +126,7 @@ fn parse_array() {
126
126
127
127
#[ test]
128
128
fn parse_object ( ) {
129
+ // Without trailing comma
129
130
assert_eq ! ( parse( r#"
130
131
131
132
{
@@ -137,6 +138,19 @@ fn parse_object() {
137
138
"foo" => "bar" ,
138
139
"num" => 10
139
140
} ) ;
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
+ } ) ;
140
154
}
141
155
142
156
#[ test]
You can’t perform that action at this time.
0 commit comments