mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-25 13:00:21 +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:
|
||||
|
||||
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]
|
||||
|
||||
|
@ -10,37 +15,29 @@
|
|||
extern crate json;
|
||||
|
||||
fn main(){
|
||||
|
||||
let parsed = json::parse(r#"
|
||||
let parsed_data = json::parse(r#"
|
||||
|
||||
{
|
||||
"code": 200,
|
||||
"success": true,
|
||||
"payload": {
|
||||
"features": [
|
||||
"awesome",
|
||||
"easyAPI",
|
||||
"lowLearningCurve"
|
||||
"userid": 103609,
|
||||
"verified": true,
|
||||
"access_privelages": [
|
||||
"user",
|
||||
"admin"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
"#).unwrap();
|
||||
|
||||
let instantiated = object!{
|
||||
"code" => 200,
|
||||
"success" => true,
|
||||
"payload" => object!{
|
||||
"features" => array![
|
||||
"awesome",
|
||||
"easyAPI",
|
||||
"lowLearningCurve"
|
||||
let instantiated_data = object!{
|
||||
"userid" => 103609,
|
||||
"verified" => true,
|
||||
"access_privelages" => array![
|
||||
"user",
|
||||
"admin"
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(parsed, instantiated);
|
||||
|
||||
assert_eq!(parsed_data, instantiated_data);
|
||||
}
|
||||
```
|
||||
# License
|
||||
|
|
Loading…
Reference in a new issue