Skip to content

Commit 0768e61

Browse files
authored
Merge pull request #129 from Yoric/commas
Resolves #125 - Accept trailing commas in object!{}
2 parents 8a93f25 + ddd1322 commit 0768e61

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/lib.rs

+19
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

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn parse_array() {
126126

127127
#[test]
128128
fn 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]

0 commit comments

Comments
 (0)