Add support for serializing newtype structs

This enables formats like JSON to serialize newtype wrapper
structs without the normal object wrapper. Consider types like:

```rust
struct Point {
    x: u32,
    y: u32,
}

stuct MyPoint(Point);
```

Before this patch, `MyPoint(1,2)` would get serialized as
`[{"x":1,"y":2}]`, but now it gets serialized as `{"x":1,"y"2}`.
This commit is contained in:
Erick Tryzelaar
2015-07-30 09:38:09 -07:00
parent 6715fb6652
commit 97528b59cf
7 changed files with 180 additions and 41 deletions
+10
View File
@@ -158,6 +158,16 @@ impl<W, F> ser::Serializer for Serializer<W, F>
self.writer.write_all(b"null")
}
/// Override `visit_newtype_struct` to serialize newtypes without an object wrapper.
#[inline]
fn visit_newtype_struct<T>(&mut self,
_name: &'static str,
value: T) -> Result<(), Self::Error>
where T: ser::Serialize,
{
value.serialize(self)
}
#[inline]
fn visit_unit_variant(&mut self,
_name: &str,