Rust & Serde JSON deserialization examples? Rust & Serde JSON deserialization examples? json json

Rust & Serde JSON deserialization examples?


Most of the standard data structures are serializable, so the following structures should work:

#[derive(Serialize, Deserialize)]struct Data {    FirstName: String,    LastName: String,    Age: u32,    Address: Address,    PhoneNumbers: Vec<String>}#[derive(Serialize, Deserialize)]struct Address {    Street: String,    City: String,    Country: String}

If some of the fields in input may be absent, then the corresponding structure fields should be Option<T> instead of just T.

Note that it is possible to name fields in a more "Rusty" manner, i.e. snake_case, because serde supports renaming annotations:

#[derive(Serialize, Deserialize)]struct Address {    #[serde(rename="Street")]    street: String,    #[serde(rename="City")]    city: String,    #[serde(rename="Country")]    country: String}

This issue is also relevant to fields renaming.