mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-29 06:40:33 +00:00
Merge pull request #7 from brson/develop
Added detailed description on JSON recipe.
This commit is contained in:
commit
4fa74d85f5
1 changed files with 22 additions and 25 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue