API Reference
Serde
Framework for serializing and deserializing Rust data structures efficiently and generically.
Official Documentation ↗CompatibilityThis documentation covers version
v1.0. Check the official docs for the latest API surface.Installation
cargo
cargo add serdeMethods
No methods match this filter.
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically. The derive macros generate code at compile time, supporting many formats with zero runtime cost.
Key Features
- Derive macros: Serialize and Deserialize with attributes
- Zero-copy: Borrowed data avoids allocation where possible
- Multi-format: JSON, YAML, TOML, Bincode, and more
- Attributes: rename, skip, default, flatten, tag
- Error handling: Rich, typed deserialization errors
Errors
The official API reference declares the following error types:
serde::de::Error(trait) — official text: “pub trait Error: Sized + StdError { // Required method fn custom(msg: T) -> Self where T: Display; // Provided methods fn invalid_type(unexp: Unexpected<‘>, exp: &dyn Expected) -> Self { … } fn invalid_value(unexp: Unexpected<’>, exp: &dyn Expected) -> “ (source: https://docs.rs/serde/latest/serde/de/trait.Error.html, fetched 2026-11-08T16:01:12.9137723+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:01:12.9137723+08:00 via WebFetch docs.rs serde trait 页、serde.rs 官方手册 attributes/derive 页;serde_json 函数另取 docs.rs/serde_json 官方 fn 页.
serde_json::to_string→ signature from https://docs.rs/serde_json/latest/serde_json/fn.to_string.html; official text: “pub fn to_string(value: &T) -> Result where T: ?Sized + Serialize,” serde_json::from_str→ signature from https://docs.rs/serde_json/latest/serde_json/fn.from_str.html; official text: “pub fn from_str<’a, T>(s: &’a str) -> Resultwhere T: Deserialize<’a>,” #[serde(rename_all)]→ signature from https://serde.rs/container-attrs.html; official text: “######[serde(rename_all = "...")]Rename all the fields (if this is a struct) or variants (if this is an enum) according to the given case convention. The possible values are"lowercase","UPPERCASE", `“PascalCase”
FAQ
- Why is Serde so fast?
- Serde generates serialization code at compile time via derive macros, with zero-copy parsing where possible and no runtime reflection. This produces highly optimized, monomorphized code that outperforms most other language serializers.
- How do I handle optional and default fields?
- Use Option<T> for nullable fields and #[serde(default)] for defaults. Combine with #[serde(skip_serializing_if)] to omit empty values, keeping output compact and tolerant of missing input.
- Can Serde work with formats other than JSON?
- Yes. Serde is format-agnostic: serde_json, serde_yaml, toml, bincode, and serde_cbor all implement the Serializer and Visitor traits, so one derive works across formats.
Report a problem with this API docOpens a pre-filled issue on GitHub.