Serde & JSON
11 steps in 5 sets of Rust.
Serde is the reason serialization in Rust is a derive and not a chore. Deriving first, then the field attributes that handle the gap between your struct names and whatever the JSON actually calls them.
Then shapes: nested structures, enums as tagged unions, collections. And finally custom implementations and what failure looks like, because real input is malformed more often than the examples suggest.
Deriving
- Deriving Serialize and DeserializeTwo derives and a struct becomes JSON in both directions.
- Untyped JSON with ValueWhen the shape is unknown, Value and the json! macro handle it dynamically.
- Maps and round tripsSerializing a map, then reading it back into the same type.
Field attributes
- Renaming fieldsRust names stay snake_case while the JSON keeps whatever the API expects.
- Optional and defaulted fieldsMissing keys, null values and fields you would rather not emit at all.
- Flattening and capturing extrasflatten merges a nested struct into the parent, or sweeps up unknown keys.
Shapes
- Nested structuresStructs inside vectors inside structs — serde follows the whole tree.
- Enums and taggingHow an enum crosses the wire: internally tagged, untagged, or as a bare string.
Custom & failure
- Custom field conversionsserialize_with and deserialize_with hand one field off to your own function.
- Handling serde errorsA parse failure tells you what it wanted, and where it gave up.
Encore
- api_models.rsA complete model set for an API payload: parse, validate, summarize.