mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-25 04:37:56 +00:00
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user