mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 00:08:01 +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:
@@ -124,15 +124,22 @@ fn serialize_item_struct(
|
||||
}
|
||||
}
|
||||
|
||||
match (named_fields.is_empty(), unnamed_fields == 0) {
|
||||
(true, true) => {
|
||||
match (named_fields.is_empty(), unnamed_fields) {
|
||||
(true, 0) => {
|
||||
serialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, false) => {
|
||||
(true, 1) => {
|
||||
serialize_newtype_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, _) => {
|
||||
serialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
@@ -142,7 +149,7 @@ fn serialize_item_struct(
|
||||
unnamed_fields,
|
||||
)
|
||||
}
|
||||
(false, true) => {
|
||||
(false, 0) => {
|
||||
serialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
@@ -153,7 +160,7 @@ fn serialize_item_struct(
|
||||
named_fields,
|
||||
)
|
||||
}
|
||||
(false, false) => {
|
||||
(false, _) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
@@ -169,6 +176,16 @@ fn serialize_unit_struct(
|
||||
quote_expr!(cx, serializer.visit_unit_struct($type_name))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident
|
||||
) -> P<ast::Expr> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, serializer.visit_newtype_struct($type_name, &self.0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
|
||||
Reference in New Issue
Block a user