Module serde
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 ;
let person = Person ;
// Serialize to JSON (requires serde_json)
let json = to_string.unwrap;
println!;
// Output: {"name":"Alice","age":30,"email":"alice@example.com"}
// Deserialize from JSON
let parsed: Person = from_str.unwrap;
println!;
Customizing field names and handling missing fields:
use ;
let json = r#"{"server-port": 8080}"#;
let config: Config = from_str.unwrap;
assert_eq!;
assert_eq!;