diff --git a/src/lib.rs b/src/lib.rs index 07eebc7..d1ffcea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![allow(unused_imports)] #![allow(unused_variables)] #![allow(non_camel_case_types)] +#![allow(clippy::four_forward_slashes)] // mod doc_test; // todo: failed to resolve: use of undeclared crate or module `doc_test` diff --git a/src/serde_json.rs b/src/serde_json.rs index 7c50d4f..716a4eb 100644 --- a/src/serde_json.rs +++ b/src/serde_json.rs @@ -17,7 +17,7 @@ struct City { longitude: f64, } -// 1. +// 1. Obj to json #[test] fn ex1_to_json() { let calabar = City { name: String::from("Calabar"), @@ -62,4 +62,66 @@ cbor (as UTF-8): bincode (as UTF-8): "\u{7}\0\0\0\0\0\0\0Calabar�+\u{7}\0\0\0\0\0������\u{13}@)\\���� @ -*/ \ No newline at end of file +*/ + + +// 2. json to obj + +use serde_derive::Deserialize; + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +struct Person { + first_name: String, + last_name: String, + age: u8, + address: Address, + phone_numbers: Vec, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +struct Address { + street: String, + city: String, + country: String, +} + +#[test] fn ex2_from_json() { + let json = r#"{ + "FirstName": "John", + "LastName": "Doe", + "Age": 43, + "Address": { + "Street": "Downing Street 10", + "City": "London", + "Country": "Great Britain" + }, + "PhoneNumbers": [ + "+44 1234567", + "+44 2345678" + ] + }"#; + + let person: Person = serde_json::from_str(json).expect("Invalid JSON"); + println!("{:#?}", person); + /* + Person { + first_name: "John", + last_name: "Doe", + age: 43, + address: Address { + street: "Downing Street 10", + city: "London", + country: "Great Britain", + }, + phone_numbers: [ + "+44 1234567", + "+44 2345678", + ], + } + */ + + assert_eq!(person.first_name, "John"); + assert_eq!(person.age, 43); +}