From 4d4c7ea90694638d9d6ffa95c05ca5c13a7a84d8 Mon Sep 17 00:00:00 2001 From: David Teller Date: Sat, 7 Oct 2017 10:13:45 +0200 Subject: [PATCH] Resolves #125 - Accept trailing commas in object!{} --- src/lib.rs | 19 +++++++++++++++++++ tests/parse.rs | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7a0e793..7f1fb42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -308,8 +308,12 @@ macro_rules! array { /// ``` #[macro_export] macro_rules! object { + // Empty object. {} => ($crate::JsonValue::new_object()); + // Non-empty object, no trailing comma. + // + // In this implementation, key/value pairs separated by commas. { $( $key:expr => $value:expr ),* } => ({ use $crate::object::Object; @@ -319,6 +323,21 @@ macro_rules! object { object.insert($key, $value.into()); )* + $crate::JsonValue::Object(object) + }); + + // Non-empty object, trailing comma. + // + // In this implementation, the comma is part of the value. + { $( $key:expr => $value:expr, )* } => ({ + use $crate::object::Object; + + let mut object = Object::new(); + + $( + object.insert($key, $value.into()); + )* + $crate::JsonValue::Object(object) }) } diff --git a/tests/parse.rs b/tests/parse.rs index ff0b545..c6c483b 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -126,6 +126,7 @@ fn parse_array() { #[test] fn parse_object() { + // Without trailing comma assert_eq!(parse(r#" { @@ -137,6 +138,19 @@ fn parse_object() { "foo" => "bar", "num" => 10 }); + + // Trailing comma in macro + assert_eq!(parse(r#" + + { + "foo": "bar", + "num": 10 + } + + "#).unwrap(), object!{ + "foo" => "bar", + "num" => 10, + }); } #[test]