Examples

Structs and enums in JSON: The representation chosen by serde_json for structs and enums. Other data formats are encouraged to follow an analogous approach where possible.

Default value for a field: Some examples of the #[serde(default)] attribute.

Handwritten generic type bounds: Some unusual scenarios in which Serde codegen infers the wrong generic type bounds. The impl bounds can be replaced with handwritten ones using the #[serde(bound)] attribute.

Deserialize for custom map type: Detailed explanation of each step involved in deserializing a map.

Serialize for custom map type: Other complex types (sequences, structs, tuple structs) are very similar to this as well.

Array of values without buffering: Deserialize the maximum value of an array of integers without holding the whole array in memory at once. This approach can be adapted to handle a variety of other situations in which data needs to be processed while being deserialized instead of after.

Serialize enum as number: A macro to impl Serialize and Deserialize for a C-like enum in a way that represents it as a u64 across all data formats.

Serialize fields as camelCase: One common application of the #[serde(rename)] attribute.

Skip serializing field: Some examples of the #[serde(skip_serializing)] and #[serde(skip_serializing_if)] attributes.

De/Serialize for other crate: Implementing Serialize and Deserialize for a type in a different crate by wrapping it in a newtype wrapper.

Manually deserialize struct: The long form of the Deserialize impl generated by codegen for a simple struct.

Using Serde trait objects: The erased-serde crate provides type-erased Serde traits that can be used as &Serialize/&Serializer or Box<Serialize>/Box<Serializer>.

Transcode one format into another: Use the serde-transcode crate to stream input in one format to output in another format efficiently.

Deserialize either a string or a struct: The docker-compose.yml configuration file has a "build" key which can be either a string or a struct.