Skip to content

Commit ddd1322

Browse files
committed
Resolves #125 - Accept trailing commas in object!{}
1 parent 083a571 commit ddd1322

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,12 @@ macro_rules! array {
308308
/// ```
309309
#[macro_export]
310310
macro_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
}

tests/parse.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn parse_array() {
122122

123123
#[test]
124124
fn parse_object() {
125+
// Without trailing comma
125126
assert_eq!(parse(r#"
126127
127128
{
@@ -133,6 +134,19 @@ fn parse_object() {
133134
"foo" => "bar",
134135
"num" => 10
135136
});
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+
});
136150
}
137151

138152
#[test]

0 commit comments

Comments
 (0)