Examples

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

Enum representations: Externally tagged, internally tagged, adjacently tagged, and untagged ways of representing an enum in self-describing formats.

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

Handwritten generic type bounds: Some unusual scenarios in which Serde's derive 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.

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.

Derive for remote crate: Deriving Serialize and Deserialize implementations for a type in somebody else's crate.

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

Discarding data: Using IgnoredAny to efficiently discard data from a deserializer.

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.

Convert error types: Map a Serde error from some format into a Serde error for some other format using Error::custom.

Date in a custom format: Handle a chrono DateTime formatted with a custom string representation.