Repository: maciejhirsz/json-rust (rust)

Issue:
Could we teach object! to accept trailing commas
Right now,

```rust
object!{
  "foo" => "bar",
}
```

fails because of the trailing comma. It would be nicer to accept them, as in the rest of Rust.

Pull request description:
Resolves #125 - Accept trailing commas in object!{}

Patch:
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)
     })
 }


Test patch:
diff --git a/tests/parse.rs b/tests/parse.rs
index 9540948..726b4dd 100644
--- a/tests/parse.rs
+++ b/tests/parse.rs
@@ -122,6 +122,7 @@ fn parse_array() {
 
 #[test]
 fn parse_object() {
+    // Without trailing comma
     assert_eq!(parse(r#"
 
     {
@@ -133,6 +134,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]