mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-16 12:18:27 +00:00
Update to Serde 1.0
This commit is contained in:
parent
3e071dca8f
commit
70027ece9b
2 changed files with 35 additions and 28 deletions
|
@ -9,12 +9,12 @@ build = "build.rs"
|
|||
rand = "0.3.15"
|
||||
byteorder = "1.0.0"
|
||||
error-chain = "0.8.0"
|
||||
json = "0.11.5"
|
||||
toml = "0.3"
|
||||
toml = "0.4"
|
||||
petgraph = "0.4.3"
|
||||
crossbeam = "0.2"
|
||||
serde_derive = "0.9.11"
|
||||
serde = "0.9.11"
|
||||
serde = "1.0.0"
|
||||
serde_derive = "1.0.0"
|
||||
serde_json = "1.0.0"
|
||||
rayon = "0.6.0"
|
||||
clap = "2.22.2"
|
||||
|
||||
|
|
55
src/json.md
55
src/json.md
|
@ -1,50 +1,57 @@
|
|||
# JSON
|
||||
[![json-badge]][json]
|
||||
[![badge]][`serde_json`]
|
||||
|
||||
## 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 `=>`.
|
||||
The [`serde_json`] crate provides a [`from_str`] function to parse a `&str` of
|
||||
JSON into a type of the caller's choice.
|
||||
|
||||
After demonstrating two simple ways to write JSON, the `assert_eq!` macro ensures equivalence.
|
||||
Unstructured JSON can be parsed into a universal [`serde_json::Value`] type that
|
||||
is able to represent any valid JSON data.
|
||||
|
||||
The example below shows a `&str` of JSON being parsed and then compared to what
|
||||
we expect the parsed value to be. The expected value is declared using the
|
||||
[`json!`] macro.
|
||||
|
||||
```rust
|
||||
#[macro_use]
|
||||
extern crate json;
|
||||
extern crate serde_json;
|
||||
|
||||
fn main(){
|
||||
let parsed_data = json::parse(r#"
|
||||
use serde_json::Value;
|
||||
|
||||
{
|
||||
fn main() {
|
||||
let j = r#"{
|
||||
"userid": 103609,
|
||||
"verified": true,
|
||||
"access_privelages": [
|
||||
"user",
|
||||
"admin"
|
||||
]
|
||||
}"#;
|
||||
|
||||
let parsed: Value = serde_json::from_str(j).unwrap();
|
||||
|
||||
let expected = json!({
|
||||
"userid": 103609,
|
||||
"verified": true,
|
||||
"access_privelages": [
|
||||
"user",
|
||||
"admin"
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
"#).unwrap();
|
||||
|
||||
let instantiated_data = object!{
|
||||
"userid" => 103609,
|
||||
"verified" => true,
|
||||
"access_privelages" => array![
|
||||
"user",
|
||||
"admin"
|
||||
]
|
||||
};
|
||||
|
||||
assert_eq!(parsed_data, instantiated_data);
|
||||
assert_eq!(parsed, expected);
|
||||
}
|
||||
```
|
||||
|
||||
# License
|
||||
|
||||
MIT/Apache-2.0
|
||||
|
||||
<!-- Links -->
|
||||
|
||||
[json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg?label=json
|
||||
[json]: http://json.rs/doc/json
|
||||
[badge]: https://img.shields.io/crates/v/serde_json.svg?label=serde_json
|
||||
[`serde_json`]: https://docs.serde.rs/serde_json/
|
||||
[`from_str`]: https://docs.serde.rs/serde_json/fn.from_str.html
|
||||
[`serde_json::Value`]: https://docs.serde.rs/serde_json/enum.Value.html
|
||||
[`json!]: https://docs.serde.rs/serde_json/macro.json.html
|
||||
|
|
Loading…
Add table
Reference in a new issue