serde.rs
Open in
urlscan Pro
2606:4700:3037::ac43:dd37
Public Scan
Submitted URL: http://serde.rs/
Effective URL: https://serde.rs/
Submission: On December 14 via manual from US — Scanned from DE
Effective URL: https://serde.rs/
Submission: On December 14 via manual from US — Scanned from DE
Form analysis
0 forms found in the DOMText Content
* Overview * Help * Serde data model * Using derive * Attributes * Container attributes * Variant attributes * Field attributes * Custom serialization * Implementing Serialize * Implementing Deserialize * Unit testing * Writing a data format * Conventions * Error handling * Implementing a Serializer * Implementing a Deserializer * Deserializer lifetimes * Examples * Structs and enums in JSON * Enum representations * Default value for a field * Struct flattening * Handwritten generic type bounds * Deserialize for custom map type * Array of values without buffering * Serialize enum as number * Serialize fields as camelCase * Skip serializing field * Derive for remote crate * Manually deserialize struct * Discarding data * Transcode into another format * Either string or struct * Convert error types * Custom date format * No-std support * Feature flags Edit AA SerifSans WhiteSepiaNight OVERVIEW SERDE Serde is a framework for serializing and deserializing Rust data structures efficiently and generically. The Serde ecosystem consists of data structures that know how to serialize and deserialize themselves along with data formats that know how to serialize and deserialize other things. Serde provides the layer by which these two groups interact with each other, allowing any supported data structure to be serialized and deserialized using any supported data format. DESIGN Where many other languages rely on runtime reflection for serializing data, Serde is instead built on Rust's powerful trait system. A data structure that knows how to serialize and deserialize itself is one that implements Serde's Serialize and Deserialize traits (or uses Serde's derive attribute to automatically generate implementations at compile time). This avoids any overhead of reflection or runtime type information. In fact in many situations the interaction between data structure and data format can be completely optimized away by the Rust compiler, leaving Serde serialization to perform the same speed as a handwritten serializer for the specific selection of data structure and data format. DATA FORMATS The following is a partial list of data formats that have been implemented for Serde by the community. * JSON, the ubiquitous JavaScript Object Notation used by many HTTP APIs. * Bincode, a compact binary format used for IPC within the Servo rendering engine. * CBOR, a Concise Binary Object Representation designed for small message size without the need for version negotiation. * YAML, a self-proclaimed human-friendly configuration language that ain't markup language. * MessagePack, an efficient binary format that resembles a compact JSON. * TOML, a minimal configuration format used by Cargo. * Pickle, a format common in the Python world. * RON, a Rusty Object Notation. * BSON, the data storage and network transfer format used by MongoDB. * Avro, a binary format used within Apache Hadoop, with support for schema definition. * JSON5, a superset of JSON including some productions from ES5. * Postcard, a no_std and embedded-systems friendly compact binary format. * URL query strings, in the x-www-form-urlencoded format. * Envy, a way to deserialize environment variables into Rust structs. (deserialization only) * Envy Store, a way to deserialize AWS Parameter Store parameters into Rust structs. (deserialization only) * S-expressions, the textual representation of code and data used by the Lisp language family. * D-Bus's binary wire format. * FlexBuffers, the schemaless cousin of Google's FlatBuffers zero-copy serialization format. * Bencode, a simple binary format used in the BitTorrent protocol. * DynamoDB Items, the format used by rusoto_dynamodb to transfer data to and from DynamoDB. * Hjson, a syntax extension to JSON designed around human reading and editing. (deserialization only) DATA STRUCTURES Out of the box, Serde is able to serialize and deserialize common Rust data types in any of the above formats. For example String, &str, usize, Vec<T>, HashMap<K,V> are all supported. In addition, Serde provides a derive macro to generate serialization implementations for structs in your own program. Using the derive macro goes like this: use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize, Debug)] struct Point { x: i32, y: i32, } fn main() { let point = Point { x: 1, y: 2 }; // Convert the Point to a JSON string. let serialized = serde_json::to_string(&point).unwrap(); // Prints serialized = {"x":1,"y":2} println!("serialized = {}", serialized); // Convert the JSON string back to a Point. let deserialized: Point = serde_json::from_str(&serialized).unwrap(); // Prints deserialized = Point { x: 1, y: 2 } println!("deserialized = {:?}", deserialized); }