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

cargocargo add serde

Methods

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 页.

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.