January 2019
Intermediate to advanced
520 pages
14h 32m
English
The serializer derived automatically uses the same nesting structure as your type. If you need to reduce the levels of nesting, you can set the #[serde(flatten)] attribute to use fields without enclosing objects. In the previous example, we used the Range type from the standard library to set a range in which to generate a random value, but we also want to see the implementation details in the serialized data. To do this, we need the start and end fields of the Range. We added this attribute to the field to cut out the { "range": ... } level of the structure.
For enumeration, serde_derive uses a tag as the name of the object. For example, the following JSON-RPC contains two variants:
#[derive(Serialize, Deserialize)]enum ...
Read now
Unlock full access