Tutorial by Examples

main.rs extern crate serde; extern crate serde_json; // Import this crate to derive the Serialize and Deserialize traits. #[macro_use] extern crate serde_derive; #[derive(Serialize, Deserialize, Debug)] struct Point { x: i32, y: i32, } fn main() { let point = Point { x: ...
extern crate serde; extern crate serde_json; macro_rules! enum_str { ($name:ident { $($variant:ident($str:expr), )* }) => { #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum $name { $($variant,)* } impl ::serde::Serialize for $name {...
extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; #[derive(Serialize)] struct Person { #[serde(rename="firstName")] first_name: String, #[serde(rename="lastName")] last_name: String, } fn main() { let person = ...
extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; #[derive(Deserialize, Debug)] struct Request { // Use the result of a function as the default if "resource" is // not included in the input. #[serde(default="default_resource&quot...
extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; use std::collections::BTreeMap as Map; #[derive(Serialize)] struct Resource { // Always serialized. name: String, // Never serialized. #[serde(skip_serializing)] hash: String, ...
impl<K, V> Serialize for MyMap<K, V> where K: Serialize, V: Serialize { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer { let mut state = serializer.serialize_map(Some(self.len()))?; ...
// A Visitor is a type that holds methods that a Deserializer can drive // depending on what is contained in the input data. // // In the case of a map we need generic type parameters K and V to be // able to set the output type correctly, but don't require any state. // This is an example of a...
Suppose we have an array of integers and we want to figure out the maximum value without holding the whole array in memory all at once. This approach can be adapted to handle a variety of other situations in which data needs to be processed while being deserialized instead of after. extern crate se...
When deriving Serialize and Deserialize implementations for structs with generic type parameters, most of the time Serde is able to infer the correct trait bounds without help from the programmer. It uses several heuristics to guess the right bound, but most importantly it puts a bound of T: Seriali...
Rust's coherence rule requires that either the trait or the type for which you are implementing the trait must be defined in the same crate as the impl, so it is not possible to implement Serialize and Deserialize for a type in a different crate directly. The newtype pattern and Deref coercion provi...

Page 1 of 1