Expand description
Serialization and deserialization framework.
serde
is a framework for serializing and deserializing Rust data structures
efficiently and generically.
The core of serde
consists of the Serialize
and Deserialize
traits,
which can be automatically derived for most data structures.
These traits define how your types convert to and from
various data formats like JSON, YAML, MessagePack, and many others
through separate format crates.
Serde operates on a data model that is independent of the
underlying data format. This means you can serialize your data
to JSON with serde_json
, YAML with serde_yaml
,
or any other supported format without changing your data structures.
The design is intentionally modular:
serde
provides the serialization framework,
while separate crates like serde_json
provide format-specific implementations.
§Examples
Deriving serialization for a struct:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Person {
name: String,
age: u32,
email: Option<String>,
}
let person = Person {
name: "Alice".to_string(),
age: 30,
email: Some("alice@example.com".to_string()),
};
// Serialize to JSON (requires serde_json)
let json = serde_json::to_string(&person).unwrap();
println!("{}", json);
// Output: {"name":"Alice","age":30,"email":"alice@example.com"}
// Deserialize from JSON
let parsed: Person = serde_json::from_str(&json).unwrap();
println!("{:?}", parsed);
Customizing field names and handling missing fields:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Config {
#[serde(rename = "server-port")]
port: u16,
#[serde(default = "default_timeout")]
timeout_ms: u64,
#[serde(skip_serializing_if = "Option::is_none")]
debug_mode: Option<bool>,
}
fn default_timeout() -> u64 {
5000
}
let json = r#"{"server-port": 8080}"#;
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(config.port, 8080);
assert_eq!(config.timeout_ms, 5000);
Modules§
- de
- Generic data structure deserialization framework.
- ser
- Generic data structure serialization framework.
Macros§
- forward_
to_ deserialize_ any - Helper macro when implementing the
Deserializer
part of a new data format for Serde.
Traits§
- Deserialize
- A data structure that can be deserialized from any data format supported by Serde.
- Deserializer
- A data format that can deserialize any data structure supported by Serde.
- Serialize
- A data structure that can be serialized into any data format supported by Serde.
- Serializer
- A data format that can serialize any data structure supported by Serde.