Skip to content

Commit

Permalink
Update examples to 2018 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 13, 2019
1 parent 48980ef commit a098f41
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 608 deletions.
121 changes: 55 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ You may be looking for:
JSON is a ubiquitous open-standard format that uses human-readable text to
transmit data objects consisting of key-value pairs.

```json,ignore
```json
{
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
}
```

Expand All @@ -64,7 +64,7 @@ between each of these representations.
Any valid JSON data can be manipulated in the following recursive enum
representation. This data structure is [`serde_json::Value`][value].

```rust,ignore
```rust
enum Value {
Null,
Bool(bool),
Expand All @@ -81,25 +81,24 @@ A string of JSON data can be parsed into a `serde_json::Value` by the
[`from_reader`][from_reader] for parsing from any `io::Read` like a File or
a TCP stream.

<a href="https://play.rust-lang.org/?gist=a266662bc71712e080efbf25ce30f306" target="_blank">
<a href="https://play.rust-lang.org/?edition=2018&gist=d69d8e3156d4bb81c4461b60b772ab72" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>

```rust
extern crate serde_json;
use serde_json::{Result, Value};

use serde_json::{Value, Error};

fn untyped_example() -> Result<(), Error> {
fn untyped_example() -> Result<()> {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;

// Parse the string of data into serde_json::Value.
let v: Value = serde_json::from_str(data)?;
Expand Down Expand Up @@ -140,18 +139,13 @@ in one of the dozens of places it is used in your code.
Serde provides a powerful way of mapping JSON data into Rust data structures
largely automatically.

<a href="https://play.rust-lang.org/?gist=cff572b80d3f078c942a2151e6020adc" target="_blank">
<a href="https://play.rust-lang.org/?edition=2018&gist=15cfab66d38ff8a15a9cf1d8d897ac68" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>

```rust
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use serde_json::Error;
use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct Person {
Expand All @@ -160,16 +154,17 @@ struct Person {
phones: Vec<String>,
}

fn typed_example() -> Result<(), Error> {
fn typed_example() -> Result<()> {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;

// Parse the string of data into a Person object. This is exactly the
// same function as the one that produced serde_json::Value above, but
Expand Down Expand Up @@ -207,23 +202,22 @@ Serde JSON provides a [`json!` macro][macro] to build `serde_json::Value`
objects with very natural JSON syntax. In order to use this macro,
`serde_json` needs to be imported with the `#[macro_use]` attribute.

<a href="https://play.rust-lang.org/?gist=c216d6beabd9429a6ac13b8f88938dfe" target="_blank">
<a href="https://play.rust-lang.org/?edition=2018&gist=6ccafad431d72b62e77cc34c8e879b24" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>

```rust
#[macro_use]
extern crate serde_json;
use serde_json::json;

fn main() {
// The type of `john` is `serde_json::Value`
let john = json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
});

println!("first phone number: {}", john["phones"][0]);
Expand All @@ -241,7 +235,7 @@ be interpolated directly into the JSON value as you are building it. Serde
will check at compile time that the value you are interpolating is able to
be represented as JSON.

<a href="https://play.rust-lang.org/?gist=aae3af4d274bd249d1c8a947076355f2" target="_blank">
<a href="https://play.rust-lang.org/?edition=2018&gist=f9101a6e61dfc9e02c6a67f315ed24f2" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>

Expand All @@ -251,11 +245,11 @@ let age_last_year = 42;

// The type of `john` is `serde_json::Value`
let john = json!({
"name": full_name,
"age": age_last_year + 1,
"phones": [
format!("+44 {}", random_phone())
]
"name": full_name,
"age": age_last_year + 1,
"phones": [
format!("+44 {}", random_phone())
]
});
```

Expand All @@ -272,26 +266,21 @@ A data structure can be converted to a JSON string by
[`serde_json::to_writer`][to_writer] which serializes to any `io::Write`
such as a File or a TCP stream.

<a href="https://play.rust-lang.org/?gist=40967ece79921c77fd78ebc8f177c063" target="_blank">
<a href="https://play.rust-lang.org/?edition=2018&gist=3472242a08ed2ff88a944f2a2283b0ee" target="_blank">
<img align="right" width="50" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/run.png">
</a>

```rust
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use serde_json::Error;
use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct Address {
street: String,
city: String,
}

fn print_an_address() -> Result<(), Error> {
fn print_an_address() -> Result<()> {
// Some data structure.
let address = Address {
street: "10 Downing Street".to_owned(),
Expand Down
61 changes: 23 additions & 38 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,10 +1257,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
///
/// You can use this to parse JSON strings containing invalid UTF-8 bytes.
///
/// ```rust
/// extern crate serde_json;
/// extern crate serde_bytes;
///
/// ```edition2018
/// use serde_bytes::ByteBuf;
///
/// fn look_at_bytes() -> Result<(), serde_json::Error> {
Expand All @@ -1283,10 +1280,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
/// to be valid, and `\u` escape sequences are required to represent valid
/// Unicode code points.
///
/// ```rust
/// extern crate serde_json;
/// extern crate serde_bytes;
///
/// ```edition2018
/// use serde_bytes::ByteBuf;
///
/// fn look_at_bytes() {
Expand Down Expand Up @@ -1956,9 +1950,7 @@ where
/// The data can consist of any JSON value. Values need to be a self-delineating value e.g.
/// arrays, objects, or strings, or be followed by whitespace or a self-delineating value.
///
/// ```rust
/// extern crate serde_json;
///
/// ```edition2018
/// use serde_json::{Deserializer, Value};
///
/// fn main() {
Expand Down Expand Up @@ -2006,7 +1998,7 @@ where
/// If a stream deserializer returns an EOF error, new data can be joined to
/// `old_data[stream.byte_offset()..]` to try again.
///
/// ```rust
/// ```edition2018
/// let data = b"[0] [1] [";
///
/// let de = serde_json::Deserializer::from_slice(data);
Expand Down Expand Up @@ -2125,12 +2117,9 @@ where
///
/// # Example
///
/// ```rust
/// #[macro_use]
/// extern crate serde_derive;
///
/// extern crate serde;
/// extern crate serde_json;
/// ```edition2018
/// # use serde_derive::Deserialize;
/// use serde::Deserialize;
///
/// use std::error::Error;
/// use std::fs::File;
Expand Down Expand Up @@ -2184,12 +2173,9 @@ where
///
/// # Example
///
/// ```rust
/// #[macro_use]
/// extern crate serde_derive;
///
/// extern crate serde;
/// extern crate serde_json;
/// ```edition2018
/// # use serde_derive::Deserialize;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
Expand All @@ -2199,10 +2185,11 @@ where
///
/// fn main() {
/// // The type of `j` is `&[u8]`
/// let j = b"{
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
/// let j = b"
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = serde_json::from_slice(j).unwrap();
/// println!("{:#?}", u);
Expand All @@ -2229,12 +2216,9 @@ where
///
/// # Example
///
/// ```rust
/// #[macro_use]
/// extern crate serde_derive;
///
/// extern crate serde;
/// extern crate serde_json;
/// ```edition2018
/// # use serde_derive::Deserialize;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
Expand All @@ -2244,10 +2228,11 @@ where
///
/// fn main() {
/// // The type of `j` is `&str`
/// let j = "{
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
/// let j = "
/// {
/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
/// \"location\": \"Menlo Park, CA\"
/// }";
///
/// let u: User = serde_json::from_str(j).unwrap();
/// println!("{:#?}", u);
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl From<Error> for io::Error {
/// JSON syntax and data errors are turned into `InvalidData` IO errors.
/// EOF errors are turned into `UnexpectedEof` IO errors.
///
/// ```rust
/// ```edition2018
/// use std::io;
///
/// enum MyError {
Expand Down
Loading

0 comments on commit a098f41

Please sign in to comment.