Merge pull request #7 from brson/develop

Added detailed description on JSON recipe.
This commit is contained in:
Trent Spice 2017-02-25 13:26:21 -05:00 committed by GitHub
commit 4fa74d85f5

View file

@ -2,6 +2,11 @@
##JSON implementation in Rust: ##JSON implementation in Rust:
The example below shows two simple ways to embed JSON in Rust.
The first method parses block JSON as a block using the parse method from the json crate. It then unwraps the parsed JSON.
The second method instantiates an object as JSON using the object macro. Key value relationships are easily set using `=>`.
After demonstrating two simple ways to write JSON, the `assert_eq!` macro ensures equivalence.
[![json][json-badge]][json] [![json][json-badge]][json]
@ -10,37 +15,29 @@
extern crate json; extern crate json;
fn main(){ fn main(){
let parsed_data = json::parse(r#"
let parsed = json::parse(r#"
{ {
"code": 200, "userid": 103609,
"success": true, "verified": true,
"payload": { "access_privelages": [
"features": [ "user",
"awesome", "admin"
"easyAPI",
"lowLearningCurve"
] ]
} }
}
"#).unwrap(); "#).unwrap();
let instantiated = object!{ let instantiated_data = object!{
"code" => 200, "userid" => 103609,
"success" => true, "verified" => true,
"payload" => object!{ "access_privelages" => array![
"features" => array![ "user",
"awesome", "admin"
"easyAPI",
"lowLearningCurve"
] ]
}
}; };
assert_eq!(parsed, instantiated); assert_eq!(parsed_data, instantiated_data);
} }
``` ```
# License # License