From a098f41e833b6fbf98cd6549e41747ae37b824ad Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 12 Jan 2019 15:34:13 -0800 Subject: [PATCH] Update examples to 2018 edition --- README.md | 121 ++++++++--------- src/de.rs | 61 ++++----- src/error.rs | 2 +- src/lib.rs | 129 ++++++++---------- src/macros.rs | 31 ++--- src/map.rs | 80 ++++------- src/number.rs | 44 ++---- src/raw.rs | 34 ++--- src/value/from.rs | 78 ++--------- src/value/index.rs | 21 +-- src/value/mod.rs | 328 +++++++++++++++------------------------------ 11 files changed, 321 insertions(+), 608 deletions(-) diff --git a/README.md b/README.md index 34ba0a3..7ca22e4 100644 --- a/README.md +++ b/README.md @@ -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" + ] } ``` @@ -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), @@ -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. - + ```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)?; @@ -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. - + ```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 { @@ -160,16 +154,17 @@ struct Person { phones: Vec, } -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 @@ -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. - + ```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]); @@ -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. - + @@ -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()) + ] }); ``` @@ -272,18 +266,13 @@ 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. - + ```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 { @@ -291,7 +280,7 @@ struct Address { 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(), diff --git a/src/de.rs b/src/de.rs index b2b87af..0a91a0b 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1257,10 +1257,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer { /// /// 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> { @@ -1283,10 +1280,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer { /// 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() { @@ -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() { @@ -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); @@ -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; @@ -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 { @@ -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); @@ -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 { @@ -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); diff --git a/src/error.rs b/src/error.rs index 778ce45..874a25d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -136,7 +136,7 @@ impl From 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 { diff --git a/src/lib.rs b/src/lib.rs index fa07f1b..ee4227e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,18 +3,18 @@ //! 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" +//! ] //! } //! ``` //! @@ -40,7 +40,7 @@ //! Any valid JSON data can be manipulated in the following recursive enum //! representation. This data structure is [`serde_json::Value`][value]. //! -//! ```rust +//! ```edition2018 //! # use serde_json::{Number, Map}; //! # //! # #[allow(dead_code)] @@ -60,21 +60,20 @@ //! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or //! a TCP stream. //! -//! ```rust -//! extern crate serde_json; +//! ```edition2018 +//! 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)?; @@ -119,14 +118,10 @@ //! Serde provides a powerful way of mapping JSON data into Rust data structures //! largely automatically. //! -//! ```rust -//! extern crate serde; -//! extern crate serde_json; -//! -//! #[macro_use] -//! extern crate serde_derive; -//! -//! use serde_json::Error; +//! ```edition2018 +//! # use serde_derive::{Deserialize, Serialize}; +//! use serde::{Deserialize, Serialize}; +//! use serde_json::Result; //! //! #[derive(Serialize, Deserialize)] //! struct Person { @@ -135,16 +130,17 @@ //! phones: Vec, //! } //! -//! 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 @@ -186,19 +182,18 @@ //! objects with very natural JSON syntax. In order to use this macro, //! `serde_json` needs to be imported with the `#[macro_use]` attribute. //! -//! ```rust -//! #[macro_use] -//! extern crate serde_json; +//! ```edition2018 +//! 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]); @@ -216,26 +211,22 @@ //! will check at compile time that the value you are interpolating is able to //! be represented as JSON. //! -//! ```rust -//! # #[macro_use] -//! # extern crate serde_json; +//! ```edition2018 +//! # use serde_json::json; //! # //! # fn random_phone() -> u16 { 0 } //! # -//! # fn main() { //! let full_name = "John Doe"; //! 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()) +//! ] //! }); -//! # let _ = john; -//! # } //! ``` //! //! This is amazingly convenient but we have the problem we had before with @@ -251,14 +242,10 @@ //! [`serde_json::to_writer`][to_writer] which serializes to any `io::Write` //! such as a File or a TCP stream. //! -//! ```rust -//! extern crate serde; -//! extern crate serde_json; -//! -//! #[macro_use] -//! extern crate serde_derive; -//! -//! use serde_json::Error; +//! ```edition2018 +//! # use serde_derive::{Deserialize, Serialize}; +//! use serde::{Deserialize, Serialize}; +//! use serde_json::Result; //! //! #[derive(Serialize, Deserialize)] //! struct Address { @@ -266,7 +253,7 @@ //! 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(), diff --git a/src/macros.rs b/src/macros.rs index 1b0fa0b..16116a0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,10 +1,8 @@ /// Construct a `serde_json::Value` from a JSON literal. /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_json; +/// ```edition2018 +/// # use serde_json::json; /// # -/// # fn main() { /// let value = json!({ /// "code": 200, /// "success": true, @@ -15,7 +13,6 @@ /// ] /// } /// }); -/// # } /// ``` /// /// Variables or expressions can be interpolated into the JSON literal. Any type @@ -25,38 +22,32 @@ /// interpolated type decides to fail, or if the interpolated type contains a /// map with non-string keys, the `json!` macro will panic. /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_json; +/// ```edition2018 +/// # use serde_json::json; /// # -/// # fn main() { /// let code = 200; /// let features = vec!["serde", "json"]; /// /// let value = json!({ -/// "code": code, -/// "success": code == 200, -/// "payload": { -/// features[0]: features[1] -/// } +/// "code": code, +/// "success": code == 200, +/// "payload": { +/// features[0]: features[1] +/// } /// }); -/// # } /// ``` /// /// Trailing commas are allowed inside both arrays and objects. /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_json; +/// ```edition2018 +/// # use serde_json::json; /// # -/// # fn main() { /// let value = json!([ /// "notice", /// "the", /// "trailing", /// "comma -->", /// ]); -/// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! json { diff --git a/src/map.rs b/src/map.rs index c06dcc8..6424630 100644 --- a/src/map.rs +++ b/src/map.rs @@ -236,7 +236,7 @@ impl PartialEq for Map { /// Access an element of this map. Panics if the given key is not present in the /// map. /// -/// ```rust +/// ```edition2018 /// # use serde_json::Value; /// # /// # let val = &Value::String("".to_owned()); @@ -264,16 +264,13 @@ where /// Mutably access an element of this map. Panics if the given key is not /// present in the map. /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_json; +/// ```edition2018 +/// # use serde_json::json; /// # -/// # fn main() { -/// # let mut map = serde_json::Map::new(); -/// # map.insert("key".to_owned(), serde_json::Value::Null); +/// # let mut map = serde_json::Map::new(); +/// # map.insert("key".to_owned(), serde_json::Value::Null); /// # /// map["key"] = json!("value"); -/// # } /// ``` impl<'a, Q: ?Sized> ops::IndexMut<&'a Q> for Map where @@ -443,7 +440,7 @@ impl<'a> Entry<'a> { /// /// # Examples /// - /// ```rust + /// ```edition2018 /// let mut map = serde_json::Map::new(); /// assert_eq!(map.entry("serde").key(), &"serde"); /// ``` @@ -459,16 +456,13 @@ impl<'a> Entry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut map = serde_json::Map::new(); /// map.entry("serde").or_insert(json!(12)); /// /// assert_eq!(map["serde"], 12); - /// # } /// ``` pub fn or_insert(self, default: Value) -> &'a mut Value { match self { @@ -483,16 +477,13 @@ impl<'a> Entry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut map = serde_json::Map::new(); /// map.entry("serde").or_insert_with(|| json!("hoho")); /// /// assert_eq!(map["serde"], "hoho".to_owned()); - /// # } /// ``` pub fn or_insert_with(self, default: F) -> &'a mut Value where @@ -511,7 +502,7 @@ impl<'a> VacantEntry<'a> { /// /// # Examples /// - /// ```rust + /// ```edition2018 /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -533,11 +524,9 @@ impl<'a> VacantEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -548,7 +537,6 @@ impl<'a> VacantEntry<'a> { /// } /// Entry::Occupied(_) => unimplemented!(), /// } - /// # } /// ``` #[inline] pub fn insert(self, value: Value) -> &'a mut Value { @@ -561,11 +549,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -577,7 +563,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// Entry::Vacant(_) => unimplemented!(), /// } - /// # } /// ``` #[inline] pub fn key(&self) -> &String { @@ -588,11 +573,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -604,7 +587,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// Entry::Vacant(_) => unimplemented!(), /// } - /// # } /// ``` #[inline] pub fn get(&self) -> &Value { @@ -615,11 +597,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -633,7 +613,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// /// assert_eq!(map["serde"].as_array().unwrap().len(), 4); - /// # } /// ``` #[inline] pub fn get_mut(&mut self) -> &mut Value { @@ -644,11 +623,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -662,7 +639,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// /// assert_eq!(map["serde"].as_array().unwrap().len(), 4); - /// # } /// ``` #[inline] pub fn into_mut(self) -> &'a mut Value { @@ -674,11 +650,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -691,7 +665,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// Entry::Vacant(_) => unimplemented!(), /// } - /// # } /// ``` #[inline] pub fn insert(&mut self, value: Value) -> Value { @@ -702,11 +675,9 @@ impl<'a> OccupiedEntry<'a> { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// use serde_json::map::Entry; /// /// let mut map = serde_json::Map::new(); @@ -718,7 +689,6 @@ impl<'a> OccupiedEntry<'a> { /// } /// Entry::Vacant(_) => unimplemented!(), /// } - /// # } /// ``` #[inline] pub fn remove(self) -> Value { diff --git a/src/number.rs b/src/number.rs index 999124e..b6ff8ff 100644 --- a/src/number.rs +++ b/src/number.rs @@ -46,11 +46,9 @@ impl Number { /// For any Number on which `is_i64` returns true, `as_i64` is guaranteed to /// return the integer value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let big = i64::max_value() as u64 + 10; /// let v = json!({ "a": 64, "b": big, "c": 256.0 }); /// @@ -61,7 +59,6 @@ impl Number { /// /// // Numbers with a decimal point are not considered integers. /// assert!(!v["c"].is_i64()); - /// # } /// ``` #[inline] pub fn is_i64(&self) -> bool { @@ -80,11 +77,9 @@ impl Number { /// For any Number on which `is_u64` returns true, `as_u64` is guaranteed to /// return the integer value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 64, "b": -64, "c": 256.0 }); /// /// assert!(v["a"].is_u64()); @@ -94,7 +89,6 @@ impl Number { /// /// // Numbers with a decimal point are not considered integers. /// assert!(!v["c"].is_u64()); - /// # } /// ``` #[inline] pub fn is_u64(&self) -> bool { @@ -115,11 +109,9 @@ impl Number { /// Currently this function returns true if and only if both `is_i64` and /// `is_u64` return false but this is not a guarantee in the future. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 256.0, "b": 64, "c": -64 }); /// /// assert!(v["a"].is_f64()); @@ -127,7 +119,6 @@ impl Number { /// // Integers. /// assert!(!v["b"].is_f64()); /// assert!(!v["c"].is_f64()); - /// # } /// ``` #[inline] pub fn is_f64(&self) -> bool { @@ -150,18 +141,15 @@ impl Number { /// If the `Number` is an integer, represent it as i64 if possible. Returns /// None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let big = i64::max_value() as u64 + 10; /// let v = json!({ "a": 64, "b": big, "c": 256.0 }); /// /// assert_eq!(v["a"].as_i64(), Some(64)); /// assert_eq!(v["b"].as_i64(), None); /// assert_eq!(v["c"].as_i64(), None); - /// # } /// ``` #[inline] pub fn as_i64(&self) -> Option { @@ -184,17 +172,14 @@ impl Number { /// If the `Number` is an integer, represent it as u64 if possible. Returns /// None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 64, "b": -64, "c": 256.0 }); /// /// assert_eq!(v["a"].as_u64(), Some(64)); /// assert_eq!(v["b"].as_u64(), None); /// assert_eq!(v["c"].as_u64(), None); - /// # } /// ``` #[inline] pub fn as_u64(&self) -> Option { @@ -209,17 +194,14 @@ impl Number { /// Represents the number as f64 if possible. Returns None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 256.0, "b": 64, "c": -64 }); /// /// assert_eq!(v["a"].as_f64(), Some(256.0)); /// assert_eq!(v["b"].as_f64(), Some(64.0)); /// assert_eq!(v["c"].as_f64(), Some(-64.0)); - /// # } /// ``` #[inline] pub fn as_f64(&self) -> Option { @@ -236,7 +218,7 @@ impl Number { /// Converts a finite `f64` to a `Number`. Infinite or NaN values are not JSON /// numbers. /// - /// ```rust + /// ```edition2018 /// # use std::f64; /// # /// # use serde_json::Number; diff --git a/src/raw.rs b/src/raw.rs index a6b6410..9be7cc7 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -22,11 +22,9 @@ use error::Error; /// /// # Example /// -/// ``` -/// #[macro_use] -/// extern crate serde_derive; -/// extern crate serde_json; -/// +/// ```edition2018 +/// # use serde_derive::{Deserialize, Serialize}; +/// use serde::{Deserialize, Serialize}; /// use serde_json::{Result, value::RawValue}; /// /// #[derive(Deserialize)] @@ -71,11 +69,8 @@ use error::Error; /// /// The typical usage of `RawValue` will be in the borrowed form: /// -/// ``` -/// # #[macro_use] -/// # extern crate serde_derive; -/// # extern crate serde_json; -/// # +/// ```edition2018 +/// # use serde_derive::Deserialize; /// # use serde_json::value::RawValue; /// # /// #[derive(Deserialize)] @@ -83,8 +78,6 @@ use error::Error; /// #[serde(borrow)] /// raw_value: &'a RawValue, /// } -/// # -/// # fn main() {} /// ``` /// /// The borrowed form is suitable when deserializing through @@ -99,19 +92,14 @@ use error::Error; /// [`serde_json::from_slice`]: ../fn.from_slice.html /// [`serde_json::from_reader`]: ../fn.from_reader.html /// -/// ``` -/// # #[macro_use] -/// # extern crate serde_derive; -/// # extern crate serde_json; -/// # +/// ```edition2018 +/// # use serde_derive::Deserialize; /// # use serde_json::value::RawValue; /// # /// #[derive(Deserialize)] /// struct SomeStruct { /// raw_value: Box, /// } -/// # -/// # fn main() {} /// ``` /// /// # Note @@ -196,11 +184,9 @@ impl RawValue { /// /// # Example /// - /// ``` - /// #[macro_use] - /// extern crate serde_derive; - /// extern crate serde_json; - /// + /// ```edition2018 + /// # use serde_derive::Deserialize; + /// use serde::Deserialize; /// use serde_json::{Result, value::RawValue}; /// /// #[derive(Deserialize)] diff --git a/src/value/from.rs b/src/value/from.rs index f3f1828..d647dea 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -33,15 +33,11 @@ impl From for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let f: f32 = 13.37; /// let x: Value = f.into(); - /// # } /// ``` fn from(f: f32) -> Self { From::from(f as f64) @@ -53,15 +49,11 @@ impl From for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let f: f64 = 13.37; /// let x: Value = f.into(); - /// # } /// ``` fn from(f: f64) -> Self { Number::from_f64(f).map_or(Value::Null, Value::Number) @@ -73,15 +65,11 @@ impl From for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let b = false; /// let x: Value = b.into(); - /// # } /// ``` fn from(f: bool) -> Self { Value::Bool(f) @@ -93,15 +81,11 @@ impl From for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let s: String = "lorem".to_string(); /// let x: Value = s.into(); - /// # } /// ``` fn from(f: String) -> Self { Value::String(f) @@ -113,15 +97,11 @@ impl<'a> From<&'a str> for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let s: &str = "lorem"; /// let x: Value = s.into(); - /// # } /// ``` fn from(f: &str) -> Self { Value::String(f.to_string()) @@ -133,28 +113,20 @@ impl<'a> From> for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// use std::borrow::Cow; /// /// let s: Cow = Cow::Borrowed("lorem"); /// let x: Value = s.into(); - /// # } /// ``` /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// use std::borrow::Cow; /// /// let s: Cow = Cow::Owned("lorem".to_string()); /// let x: Value = s.into(); - /// # } /// ``` fn from(f: Cow<'a, str>) -> Self { Value::String(f.into_owned()) @@ -166,16 +138,12 @@ impl From> for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::{Map, Value}; /// /// let mut m = Map::new(); /// m.insert("Lorem".to_string(), "ipsum".into()); /// let x: Value = m.into(); - /// # } /// ``` fn from(f: Map) -> Self { Value::Object(f) @@ -187,15 +155,11 @@ impl> From> for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let v = vec!["lorem", "ipsum", "dolor"]; /// let x: Value = v.into(); - /// # } /// ``` fn from(f: Vec) -> Self { Value::Array(f.into_iter().map(Into::into).collect()) @@ -207,15 +171,11 @@ impl<'a, T: Clone + Into> From<&'a [T]> for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let v: &[&str] = &["lorem", "ipsum", "dolor"]; /// let x: Value = v.into(); - /// # } /// ``` fn from(f: &'a [T]) -> Self { Value::Array(f.iter().cloned().map(Into::into).collect()) @@ -227,37 +187,25 @@ impl> ::std::iter::FromIterator for Value { /// /// # Examples /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let v = std::iter::repeat(42).take(5); /// let x: Value = v.collect(); - /// # } /// ``` /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use serde_json::Value; /// /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"]; /// let x: Value = v.into_iter().collect(); - /// # } /// ``` /// - /// ```rust - /// # extern crate serde_json; - /// # - /// # fn main() { + /// ```edition2018 /// use std::iter::FromIterator; /// use serde_json::Value; /// /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]); - /// # } /// ``` fn from_iter>(iter: I) -> Self { Value::Array(iter.into_iter().map(Into::into).collect()) diff --git a/src/value/index.rs b/src/value/index.rs index 2f4f7b4..47990a5 100644 --- a/src/value/index.rs +++ b/src/value/index.rs @@ -20,11 +20,9 @@ use map::Map; /// /// # Examples /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_json; +/// ```edition2018 +/// # use serde_json::json; /// # -/// # fn main() { /// let data = json!({ "inner": [1, 2, 3] }); /// /// // Data is a JSON map so it can be indexed with a string. @@ -34,7 +32,6 @@ use map::Map; /// let first = &inner[0]; /// /// assert_eq!(first, 1); -/// # } /// ``` pub trait Index: private::Sealed { /// Return None if the key is not already in the array or object. @@ -196,11 +193,9 @@ where /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let data = json!({ /// "x": { /// "y": ["z", "zz"] @@ -212,7 +207,6 @@ where /// /// assert_eq!(data["a"], json!(null)); // returns null for undefined values /// assert_eq!(data["a"]["b"], json!(null)); // does not panic - /// # } /// ``` fn index(&self, index: I) -> &Value { static NULL: Value = Value::Null; @@ -238,11 +232,9 @@ where /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut data = json!({ "x": 0 }); /// /// // replace an existing key @@ -258,7 +250,6 @@ where /// data["a"]["b"]["c"]["d"] = json!(true); /// /// println!("{}", data); - /// # } /// ``` fn index_mut(&mut self, index: I) -> &mut Value { index.index_or_insert(self) diff --git a/src/value/mod.rs b/src/value/mod.rs index 0224fb1..55803a1 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -6,19 +6,18 @@ //! objects with very natural JSON syntax. In order to use this macro, //! `serde_json` needs to be imported with the `#[macro_use]` attribute. //! -//! ```rust -//! #[macro_use] -//! extern crate serde_json; +//! ```edition2018 +//! 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]); @@ -36,26 +35,22 @@ //! will check at compile time that the value you are interpolating is able to //! be represented as JSON. //! -//! ```rust -//! # #[macro_use] -//! # extern crate serde_json; +//! ```edition2018 +//! # use serde_json::json; //! # //! # fn random_phone() -> u16 { 0 } //! # -//! # fn main() { //! let full_name = "John Doe"; //! 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()) +//! ] //! }); -//! # let _ = john; -//! # } //! ``` //! //! A string of JSON data can be parsed into a `serde_json::Value` by the @@ -64,21 +59,20 @@ //! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or //! a TCP stream. //! -//! ```rust -//! extern crate serde_json; -//! -//! use serde_json::{Value, Error}; +//! ```edition2018 +//! use serde_json::{json, Value, Error}; //! //! fn untyped_example() -> Result<(), Error> { //! // 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)?; @@ -125,61 +119,46 @@ use self::ser::Serializer; pub enum Value { /// Represents a JSON null value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!(null); - /// # } /// ``` Null, /// Represents a JSON boolean. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!(true); - /// # } /// ``` Bool(bool), /// Represents a JSON number, whether integer or floating point. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!(12.5); - /// # } /// ``` Number(Number), /// Represents a JSON string. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!("a string"); - /// # } /// ``` String(String), /// Represents a JSON array. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!(["an", "array"]); - /// # } /// ``` Array(Vec), @@ -191,13 +170,10 @@ pub enum Value { /// allows JSON data to be deserialized into a Value and serialized to a /// string while retaining the order of map keys in the input. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "an": "object" }); - /// # } /// ``` Object(Map), } @@ -239,11 +215,9 @@ impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> { impl fmt::Display for Value { /// Display a JSON value as a string. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let json = json!({ "city": "London", "street": "10 Downing Street" }); /// /// // Compact format: @@ -262,7 +236,6 @@ impl fmt::Display for Value { /// let pretty = format!("{:#}", json); /// assert_eq!(pretty, /// "{\n \"city\": \"London\",\n \"street\": \"10 Downing Street\"\n}"); - /// # } /// ``` fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let alternate = f.alternate(); @@ -294,11 +267,9 @@ impl Value { /// number. Also returns `None` if the given key does not exist in the map /// or the given index is not within the bounds of the array. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let object = json!({ "A": 65, "B": 66, "C": 67 }); /// assert_eq!(*object.get("A").unwrap(), json!(65)); /// @@ -306,18 +277,15 @@ impl Value { /// assert_eq!(*array.get(2).unwrap(), json!("C")); /// /// assert_eq!(array.get("A"), None); - /// # } /// ``` /// /// Square brackets can also be used to index into a value in a more concise /// way. This returns `Value::Null` in cases where `get` would have returned /// `None`. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let object = json!({ /// "A": ["a", "á", "à"], /// "B": ["b", "b́"], @@ -327,7 +295,6 @@ impl Value { /// /// assert_eq!(object["D"], json!(null)); /// assert_eq!(object[0]["x"]["y"]["z"], json!(null)); - /// # } /// ``` pub fn get(&self, index: I) -> Option<&Value> { index.index_into(self) @@ -342,17 +309,14 @@ impl Value { /// number. Also returns `None` if the given key does not exist in the map /// or the given index is not within the bounds of the array. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut object = json!({ "A": 65, "B": 66, "C": 67 }); /// *object.get_mut("A").unwrap() = json!(69); /// /// let mut array = json!([ "A", "B", "C" ]); /// *array.get_mut(2).unwrap() = json!("D"); - /// # } /// ``` pub fn get_mut(&mut self, index: I) -> Option<&mut Value> { index.index_into_mut(self) @@ -364,11 +328,9 @@ impl Value { /// `as_object_mut` are guaranteed to return the map representation of the /// object. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let obj = json!({ "a": { "nested": true }, "b": ["an", "array"] }); /// /// assert!(obj.is_object()); @@ -376,7 +338,6 @@ impl Value { /// /// // array, not an object /// assert!(!obj["b"].is_object()); - /// # } /// ``` pub fn is_object(&self) -> bool { self.as_object().is_some() @@ -385,11 +346,9 @@ impl Value { /// If the `Value` is an Object, returns the associated Map. Returns None /// otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": { "nested": true }, "b": ["an", "array"] }); /// /// // The length of `{"nested": true}` is 1 entry. @@ -397,7 +356,6 @@ impl Value { /// /// // The array `["an", "array"]` is not an object. /// assert_eq!(v["b"].as_object(), None); - /// # } /// ``` pub fn as_object(&self) -> Option<&Map> { match *self { @@ -409,17 +367,13 @@ impl Value { /// If the `Value` is an Object, returns the associated mutable Map. /// Returns None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut v = json!({ "a": { "nested": true } }); /// /// v["a"].as_object_mut().unwrap().clear(); /// assert_eq!(v, json!({ "a": {} })); - /// # } - /// /// ``` pub fn as_object_mut(&mut self) -> Option<&mut Map> { match *self { @@ -434,18 +388,15 @@ impl Value { /// `as_array_mut` are guaranteed to return the vector representing the /// array. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let obj = json!({ "a": ["an", "array"], "b": { "an": "object" } }); /// /// assert!(obj["a"].is_array()); /// /// // an object, not an array /// assert!(!obj["b"].is_array()); - /// # } /// ``` pub fn is_array(&self) -> bool { self.as_array().is_some() @@ -454,11 +405,9 @@ impl Value { /// If the `Value` is an Array, returns the associated vector. Returns None /// otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": ["an", "array"], "b": { "an": "object" } }); /// /// // The length of `["an", "array"]` is 2 elements. @@ -466,7 +415,6 @@ impl Value { /// /// // The object `{"an": "object"}` is not an array. /// assert_eq!(v["b"].as_array(), None); - /// # } /// ``` pub fn as_array(&self) -> Option<&Vec> { match *self { @@ -478,16 +426,13 @@ impl Value { /// If the `Value` is an Array, returns the associated mutable vector. /// Returns None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut v = json!({ "a": ["an", "array"] }); /// /// v["a"].as_array_mut().unwrap().clear(); /// assert_eq!(v, json!({ "a": [] })); - /// # } /// ``` pub fn as_array_mut(&mut self) -> Option<&mut Vec> { match *self { @@ -501,18 +446,15 @@ impl Value { /// For any Value on which `is_string` returns true, `as_str` is guaranteed /// to return the string slice. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": "some string", "b": false }); /// /// assert!(v["a"].is_string()); /// /// // The boolean `false` is not a string. /// assert!(!v["b"].is_string()); - /// # } /// ``` pub fn is_string(&self) -> bool { self.as_str().is_some() @@ -521,11 +463,9 @@ impl Value { /// If the `Value` is a String, returns the associated str. Returns None /// otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": "some string", "b": false }); /// /// assert_eq!(v["a"].as_str(), Some("some string")); @@ -542,7 +482,6 @@ impl Value { /// // /// // The value is: some string /// println!("The value is: {}", v["a"].as_str().unwrap()); - /// # } /// ``` pub fn as_str(&self) -> Option<&str> { match *self { @@ -553,18 +492,15 @@ impl Value { /// Returns true if the `Value` is a Number. Returns false otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 1, "b": "2" }); /// /// assert!(v["a"].is_number()); /// /// // The string `"2"` is a string, not a number. /// assert!(!v["b"].is_number()); - /// # } /// ``` pub fn is_number(&self) -> bool { match *self { @@ -579,11 +515,9 @@ impl Value { /// For any Value on which `is_i64` returns true, `as_i64` is guaranteed to /// return the integer value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let big = i64::max_value() as u64 + 10; /// let v = json!({ "a": 64, "b": big, "c": 256.0 }); /// @@ -594,7 +528,6 @@ impl Value { /// /// // Numbers with a decimal point are not considered integers. /// assert!(!v["c"].is_i64()); - /// # } /// ``` pub fn is_i64(&self) -> bool { match *self { @@ -608,11 +541,9 @@ impl Value { /// For any Value on which `is_u64` returns true, `as_u64` is guaranteed to /// return the integer value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 64, "b": -64, "c": 256.0 }); /// /// assert!(v["a"].is_u64()); @@ -622,7 +553,6 @@ impl Value { /// /// // Numbers with a decimal point are not considered integers. /// assert!(!v["c"].is_u64()); - /// # } /// ``` pub fn is_u64(&self) -> bool { match *self { @@ -639,11 +569,9 @@ impl Value { /// Currently this function returns true if and only if both `is_i64` and /// `is_u64` return false but this is not a guarantee in the future. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 256.0, "b": 64, "c": -64 }); /// /// assert!(v["a"].is_f64()); @@ -651,7 +579,6 @@ impl Value { /// // Integers. /// assert!(!v["b"].is_f64()); /// assert!(!v["c"].is_f64()); - /// # } /// ``` pub fn is_f64(&self) -> bool { match *self { @@ -663,18 +590,15 @@ impl Value { /// If the `Value` is an integer, represent it as i64 if possible. Returns /// None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let big = i64::max_value() as u64 + 10; /// let v = json!({ "a": 64, "b": big, "c": 256.0 }); /// /// assert_eq!(v["a"].as_i64(), Some(64)); /// assert_eq!(v["b"].as_i64(), None); /// assert_eq!(v["c"].as_i64(), None); - /// # } /// ``` pub fn as_i64(&self) -> Option { match *self { @@ -686,17 +610,14 @@ impl Value { /// If the `Value` is an integer, represent it as u64 if possible. Returns /// None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 64, "b": -64, "c": 256.0 }); /// /// assert_eq!(v["a"].as_u64(), Some(64)); /// assert_eq!(v["b"].as_u64(), None); /// assert_eq!(v["c"].as_u64(), None); - /// # } /// ``` pub fn as_u64(&self) -> Option { match *self { @@ -708,17 +629,14 @@ impl Value { /// If the `Value` is a number, represent it as f64 if possible. Returns /// None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": 256.0, "b": 64, "c": -64 }); /// /// assert_eq!(v["a"].as_f64(), Some(256.0)); /// assert_eq!(v["b"].as_f64(), Some(64.0)); /// assert_eq!(v["c"].as_f64(), Some(-64.0)); - /// # } /// ``` pub fn as_f64(&self) -> Option { match *self { @@ -732,18 +650,15 @@ impl Value { /// For any Value on which `is_boolean` returns true, `as_bool` is /// guaranteed to return the boolean value. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": false, "b": "false" }); /// /// assert!(v["a"].is_boolean()); /// /// // The string `"false"` is a string, not a boolean. /// assert!(!v["b"].is_boolean()); - /// # } /// ``` pub fn is_boolean(&self) -> bool { self.as_bool().is_some() @@ -752,18 +667,15 @@ impl Value { /// If the `Value` is a Boolean, returns the associated bool. Returns None /// otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": false, "b": "false" }); /// /// assert_eq!(v["a"].as_bool(), Some(false)); /// /// // The string `"false"` is a string, not a boolean. /// assert_eq!(v["b"].as_bool(), None); - /// # } /// ``` pub fn as_bool(&self) -> Option { match *self { @@ -777,18 +689,15 @@ impl Value { /// For any Value on which `is_null` returns true, `as_null` is guaranteed /// to return `Some(())`. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": null, "b": false }); /// /// assert!(v["a"].is_null()); /// /// // The boolean `false` is not null. /// assert!(!v["b"].is_null()); - /// # } /// ``` pub fn is_null(&self) -> bool { self.as_null().is_some() @@ -796,18 +705,15 @@ impl Value { /// If the `Value` is a Null, returns (). Returns None otherwise. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let v = json!({ "a": null, "b": false }); /// /// assert_eq!(v["a"].as_null(), Some(())); /// /// // The boolean `false` is not null. /// assert_eq!(v["b"].as_null(), None); - /// # } /// ``` pub fn as_null(&self) -> Option<()> { match *self { @@ -830,11 +736,9 @@ impl Value { /// /// # Examples /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let data = json!({ /// "x": { /// "y": ["z", "zz"] @@ -843,7 +747,6 @@ impl Value { /// /// assert_eq!(data.pointer("/x/y/1").unwrap(), &json!("zz")); /// assert_eq!(data.pointer("/a/b/c"), None); - /// # } /// ``` pub fn pointer<'a>(&'a self, pointer: &str) -> Option<&'a Value> { if pointer == "" { @@ -888,9 +791,7 @@ impl Value { /// /// # Example of Use /// - /// ```rust - /// extern crate serde_json; - /// + /// ```edition2018 /// use serde_json::Value; /// /// fn main() { @@ -945,15 +846,12 @@ impl Value { /// Takes the value out of the `Value`, leaving a `Null` in its place. /// - /// ```rust - /// # #[macro_use] - /// # extern crate serde_json; + /// ```edition2018 + /// # use serde_json::json; /// # - /// # fn main() { /// let mut v = json!({ "x": "y" }); /// assert_eq!(v["x"].take(), json!("y")); /// assert_eq!(v, json!({ "x": null })); - /// # } /// ``` pub fn take(&mut self) -> Value { mem::replace(self, Value::Null) @@ -966,12 +864,8 @@ impl Value { /// /// # Examples /// -/// ```rust -/// # #[macro_use] -/// # extern crate serde_derive; -/// # -/// # extern crate serde_json; -/// # +/// ```edition2018 +/// # use serde_derive::Deserialize; /// use serde_json::Value; /// /// #[derive(Deserialize)] @@ -1012,14 +906,10 @@ mod ser; /// /// # Example /// -/// ```rust -/// extern crate serde; -/// -/// #[macro_use] -/// extern crate serde_derive; -/// -/// #[macro_use] -/// extern crate serde_json; +/// ```edition2018 +/// # use serde_derive::Serialize; +/// use serde::Serialize; +/// use serde_json::json; /// /// use std::error::Error; /// @@ -1037,9 +927,9 @@ mod ser; /// /// // The type of `expected` is `serde_json::Value` /// let expected = json!({ -/// "fingerprint": "0xF9BA143B95FF6D82", -/// "location": "Menlo Park, CA", -/// }); +/// "fingerprint": "0xF9BA143B95FF6D82", +/// "location": "Menlo Park, CA", +/// }); /// /// let v = serde_json::to_value(u).unwrap(); /// assert_eq!(v, expected); @@ -1057,9 +947,7 @@ mod ser; /// This conversion can fail if `T`'s implementation of `Serialize` decides to /// fail, or if `T` contains a map with non-string keys. /// -/// ```rust -/// extern crate serde_json; -/// +/// ```edition2018 /// use std::collections::BTreeMap; /// /// fn main() { @@ -1083,14 +971,10 @@ where /// /// # Example /// -/// ```rust -/// #[macro_use] -/// extern crate serde_json; -/// -/// #[macro_use] -/// extern crate serde_derive; -/// -/// extern crate serde; +/// ```edition2018 +/// # use serde_derive::Deserialize; +/// use serde::Deserialize; +/// use serde_json::json; /// /// #[derive(Deserialize, Debug)] /// struct User { @@ -1101,9 +985,9 @@ where /// fn main() { /// // The type of `j` is `serde_json::Value` /// let j = json!({ -/// "fingerprint": "0xF9BA143B95FF6D82", -/// "location": "Menlo Park, CA" -/// }); +/// "fingerprint": "0xF9BA143B95FF6D82", +/// "location": "Menlo Park, CA" +/// }); /// /// let u: User = serde_json::from_value(j).unwrap(); /// println!("{:#?}", u);