From 6525ffa3640a1c6d902aaced7ac73a1ff44c6cc3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 4 Sep 2023 22:35:02 -0700 Subject: [PATCH 01/18] Update actions/checkout@v3 -> v4 --- .github/workflows/ci.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9c984c0f8..dee0bfee1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: os: [ubuntu, windows] timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - run: cargo test - run: cargo test --features preserve_order --tests -- --skip ui --exact @@ -48,7 +48,7 @@ jobs: os: windows timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{matrix.rust}} @@ -74,7 +74,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - run: cargo generate-lockfile -Z minimal-versions - run: cargo check --locked @@ -86,7 +86,7 @@ jobs: MIRIFLAGS: -Zmiri-strict-provenance timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@miri - run: cargo miri setup - run: cargo miri test @@ -98,7 +98,7 @@ jobs: if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@clippy - run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic - run: cargo clippy --all-features --tests -- -Dclippy::all -Dclippy::pedantic @@ -108,7 +108,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - run: cargo doc --features raw_value,unbounded_depth env: @@ -119,7 +119,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/install@cargo-fuzz - run: cargo fuzz check @@ -130,7 +130,7 @@ jobs: if: github.event_name != 'pull_request' timeout-minutes: 45 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: dtolnay/install@cargo-outdated - run: cargo outdated --exit-code 1 - run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1 From 4cc9ea77abfbf8d7fe934a1726163d3821fb36f2 Mon Sep 17 00:00:00 2001 From: Chance Date: Wed, 6 Sep 2023 15:03:40 -0400 Subject: [PATCH 02/18] adds `as_str` to `Number` if `arbitrary_precision` is enabled --- src/number.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/number.rs b/src/number.rs index 7ff66681d..9254284dc 100644 --- a/src/number.rs +++ b/src/number.rs @@ -279,6 +279,16 @@ impl Number { } } + /// Returns the `&str` representation of the `Number`. + /// ``` + /// # use serde_json::Number; + /// + /// assert_eq!(Number::from_f64(256.0).unwrap().as_str(), "256.0"); + /// assert_eq!(Number::from_f64(34.0).unwrap().as_str(), "34.0"); + pub fn as_str(&self) -> &str { + &self.n + } + pub(crate) fn as_f32(&self) -> Option { #[cfg(not(feature = "arbitrary_precision"))] match self.n { From 99bc2df40b47931f776083bcdec47f281531fc55 Mon Sep 17 00:00:00 2001 From: Chance Date: Wed, 6 Sep 2023 15:13:42 -0400 Subject: [PATCH 03/18] adds missing cfg attr to as_str --- src/number.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/number.rs b/src/number.rs index 9254284dc..2bd2c8e42 100644 --- a/src/number.rs +++ b/src/number.rs @@ -279,6 +279,7 @@ impl Number { } } + #[cfg(feature = "arbitrary_precision")] /// Returns the `&str` representation of the `Number`. /// ``` /// # use serde_json::Number; From 029fda06fad9a4b2da7e5f0b2d0c5151b33e1a7b Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 7 Sep 2023 13:21:51 -0400 Subject: [PATCH 04/18] improves `Number::as_str` doc example --- src/number.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/number.rs b/src/number.rs index 2bd2c8e42..dfd16f720 100644 --- a/src/number.rs +++ b/src/number.rs @@ -283,9 +283,18 @@ impl Number { /// Returns the `&str` representation of the `Number`. /// ``` /// # use serde_json::Number; - /// - /// assert_eq!(Number::from_f64(256.0).unwrap().as_str(), "256.0"); - /// assert_eq!(Number::from_f64(34.0).unwrap().as_str(), "34.0"); + /// for value in [ + /// "7", + /// "12.34", + /// "34e-56789", + /// "0.0123456789000000012345678900000001234567890000123456789", + /// "343412345678910111213141516171819202122232425262728293034", + /// "-343412345678910111213141516171819202122232425262728293031", + /// ] { + /// println!("{value}"); + /// let number: Number = serde_json::from_str(value).unwrap(); + /// assert_eq!(number.as_str(), value); + /// } pub fn as_str(&self) -> &str { &self.n } From 1786de244fd5ec13c35936e40e917bbc63bb297a Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 7 Sep 2023 13:22:21 -0400 Subject: [PATCH 05/18] removes `println!` from example --- src/number.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/number.rs b/src/number.rs index dfd16f720..f4925a0b4 100644 --- a/src/number.rs +++ b/src/number.rs @@ -291,7 +291,6 @@ impl Number { /// "343412345678910111213141516171819202122232425262728293034", /// "-343412345678910111213141516171819202122232425262728293031", /// ] { - /// println!("{value}"); /// let number: Number = serde_json::from_str(value).unwrap(); /// assert_eq!(number.as_str(), value); /// } From b438004775bdc340b819f03bc73fb731b03533f6 Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 7 Sep 2023 15:04:28 -0400 Subject: [PATCH 06/18] adds `as_number` to `Value` --- src/value/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/value/mod.rs b/src/value/mod.rs index 79ffe9488..713f995c8 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -495,6 +495,34 @@ impl Value { } } + /// If the `Value` is an Number, returns the associated [`Number`]. Returns None + /// otherwise. + /// + /// ``` + /// # use serde_json::{json, Number}; + /// # + /// let v = json!({ "a": 1, "b": 2.2, "c": -3, "d": "4" }); + /// + /// // The number `1` is an u64. + /// assert_eq!(v["a"].as_number().unwrap(), &Number::from(1u64)); + /// + /// // The number `2.2` is an f64. + /// assert_eq!(v["b"].as_number().unwrap(), &Number::from_f64(2.2f64).unwrap()); + /// + /// // The number `-3` is an i64. + /// assert_eq!(v["c"].as_number().unwrap(), &Number::from(-3i64)); + /// + /// // The string `"4"` is not a number. + /// assert_eq!(v["d"].as_number(), None); + /// + /// ``` + pub fn as_number(&self) -> Option<&Number> { + match self { + Value::Number(number) => Some(number), + _ => None, + } + } + /// Returns true if the `Value` is a Number. Returns false otherwise. /// /// ``` From 2cd5d59cd1066b824c7f55fccd31d04ecdaf140e Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 7 Sep 2023 15:16:22 -0400 Subject: [PATCH 07/18] minor cleanup of documentation for `Value::as_number` --- src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index 713f995c8..7729834ef 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -507,7 +507,7 @@ impl Value { /// assert_eq!(v["a"].as_number().unwrap(), &Number::from(1u64)); /// /// // The number `2.2` is an f64. - /// assert_eq!(v["b"].as_number().unwrap(), &Number::from_f64(2.2f64).unwrap()); + /// assert_eq!(v["b"].as_number().unwrap(), &Number::from_f64(2.2).unwrap()); /// /// // The number `-3` is an i64. /// assert_eq!(v["c"].as_number().unwrap(), &Number::from(-3i64)); From cf433e9efd9fbb04ba7e35c51e56bee50b916770 Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 7 Sep 2023 15:19:09 -0400 Subject: [PATCH 08/18] removes `unwrap` from assertions in `Value::as_number` --- src/value/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index 7729834ef..7924d969f 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -504,13 +504,13 @@ impl Value { /// let v = json!({ "a": 1, "b": 2.2, "c": -3, "d": "4" }); /// /// // The number `1` is an u64. - /// assert_eq!(v["a"].as_number().unwrap(), &Number::from(1u64)); + /// assert_eq!(v["a"].as_number(), Some(&Number::from(1u64))); /// /// // The number `2.2` is an f64. - /// assert_eq!(v["b"].as_number().unwrap(), &Number::from_f64(2.2).unwrap()); + /// assert_eq!(v["b"].as_number(), Some(&Number::from_f64(2.2).unwrap())); /// /// // The number `-3` is an i64. - /// assert_eq!(v["c"].as_number().unwrap(), &Number::from(-3i64)); + /// assert_eq!(v["c"].as_number(), Some(&Number::from(-3i64))); /// /// // The string `"4"` is not a number. /// assert_eq!(v["d"].as_number(), None); From de39b2a1aaac8334ae885da37c591bd022fd339d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:01:48 -0700 Subject: [PATCH 09/18] Delete trailing whitespace from PR 1069 --- src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index d12d4cfbe..d8946506e 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -509,7 +509,7 @@ impl Value { /// /// // The string `"4"` is not a number. /// assert_eq!(v["d"].as_number(), None); - /// + /// /// ``` pub fn as_number(&self) -> Option<&Number> { match self { From 6a5fef919092c11a9a4eb827ac51001761795fe0 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:02:13 -0700 Subject: [PATCH 10/18] Wrap as_number documentation to 80 columns --- src/value/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index d8946506e..4f1d4878e 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -495,8 +495,8 @@ impl Value { } } - /// If the `Value` is an Number, returns the associated [`Number`]. Returns None - /// otherwise. + /// If the `Value` is an Number, returns the associated [`Number`]. Returns + /// None otherwise. /// /// ``` /// # use serde_json::{json, Number}; @@ -509,7 +509,6 @@ impl Value { /// /// // The string `"4"` is not a number. /// assert_eq!(v["d"].as_number(), None); - /// /// ``` pub fn as_number(&self) -> Option<&Number> { match self { From 5a39516161ea9efd7957e2b6c6c8fa76792026a9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:02:30 -0700 Subject: [PATCH 11/18] Reorder Value::as_number after is_number The other as_* methods all come after the corresponding is_* method. --- src/value/mod.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index 4f1d4878e..d109747e4 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -495,6 +495,25 @@ impl Value { } } + /// Returns true if the `Value` is a Number. Returns false otherwise. + /// + /// ``` + /// # use serde_json::json; + /// # + /// 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 { + Value::Number(_) => true, + _ => false, + } + } + /// If the `Value` is an Number, returns the associated [`Number`]. Returns /// None otherwise. /// @@ -517,25 +536,6 @@ impl Value { } } - /// Returns true if the `Value` is a Number. Returns false otherwise. - /// - /// ``` - /// # use serde_json::json; - /// # - /// 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 { - Value::Number(_) => true, - _ => false, - } - } - /// Returns true if the `Value` is an integer between `i64::MIN` and /// `i64::MAX`. /// From 95c5d6c8be64634d9bf408ef9679003e6b6f0220 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:04:49 -0700 Subject: [PATCH 12/18] Fix documentation typo from PR 1069 --- src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index d109747e4..a565b2998 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -514,7 +514,7 @@ impl Value { } } - /// If the `Value` is an Number, returns the associated [`Number`]. Returns + /// If the `Value` is a Number, returns the associated [`Number`]. Returns /// None otherwise. /// /// ``` From 11b603cf07b6298cdec9803ef0b32085e43c335c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:05:48 -0700 Subject: [PATCH 13/18] Resolve rustdoc::redundant_explicit_links lint warning: redundant explicit link target --> src/lib.rs:59:20 | 59 | //! [`from_slice`][from_slice] for parsing from a byte slice &[u8] and | ------------ ^^^^^^^^^^ explicit target is redundant | | | because label contains path that resolves to same destination | note: referenced explicit link target defined here --> src/lib.rs:295:19 | 295 | //! [from_slice]: crate::de::from_slice | ^^^^^^^^^^^^^^^^^^^^^ = note: when a link's destination is not specified, the label is used to resolve intra-doc links = note: `#[warn(rustdoc::redundant_explicit_links)]` on by default help: remove explicit link target | 59 | //! [`from_slice`] for parsing from a byte slice &[u8] and | ~~~~~~~~~~~~~~ warning: redundant explicit link target --> src/lib.rs:60:21 | 60 | //! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or | ------------- ^^^^^^^^^^^ explicit target is redundant | | | because label contains path that resolves to same destination | note: referenced explicit link target defined here --> src/lib.rs:296:20 | 296 | //! [from_reader]: crate::de::from_reader | ^^^^^^^^^^^^^^^^^^^^^^ = note: when a link's destination is not specified, the label is used to resolve intra-doc links help: remove explicit link target | 60 | //! [`from_reader`] for parsing from any `io::Read` like a File or | ~~~~~~~~~~~~~~~ --- src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 83f48a031..d2e5e0b62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,10 +55,9 @@ //! ``` //! //! A string of JSON data can be parsed into a `serde_json::Value` by the -//! [`serde_json::from_str`][from_str] function. There is also -//! [`from_slice`][from_slice] for parsing from a byte slice &[u8] and -//! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or -//! a TCP stream. +//! [`serde_json::from_str`][from_str] function. There is also [`from_slice`] +//! for parsing from a byte slice &[u8] and [`from_reader`] for parsing from any +//! `io::Read` like a File or a TCP stream. //! //! ``` //! use serde_json::{Result, Value}; From db75c22990f58c77d380c9dc02caa43e42b5b098 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:07:12 -0700 Subject: [PATCH 14/18] Fix unintended u8 link inferred by intra doc link --- README.md | 2 +- src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d70497924..a3ba288f1 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ enum Value { A string of JSON data can be parsed into a `serde_json::Value` by the [`serde_json::from_str`][from_str] function. There is also -[`from_slice`][from_slice] for parsing from a byte slice &[u8] and +[`from_slice`][from_slice] for parsing from a byte slice &\[u8\] and [`from_reader`][from_reader] for parsing from any `io::Read` like a File or a TCP stream. diff --git a/src/lib.rs b/src/lib.rs index d2e5e0b62..109de45c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,8 +56,8 @@ //! //! A string of JSON data can be parsed into a `serde_json::Value` by the //! [`serde_json::from_str`][from_str] function. There is also [`from_slice`] -//! for parsing from a byte slice &[u8] and [`from_reader`] for parsing from any -//! `io::Read` like a File or a TCP stream. +//! for parsing from a byte slice &\[u8\] and [`from_reader`] for parsing from +//! any `io::Read` like a File or a TCP stream. //! //! ``` //! use serde_json::{Result, Value}; From fc8dd13aa284d255b79504ed4ee15a27aba6228f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:15:00 -0700 Subject: [PATCH 15/18] Touch up PR 1067 --- src/number.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/number.rs b/src/number.rs index f4925a0b4..6b3003f42 100644 --- a/src/number.rs +++ b/src/number.rs @@ -279,8 +279,8 @@ impl Number { } } - #[cfg(feature = "arbitrary_precision")] /// Returns the `&str` representation of the `Number`. + /// /// ``` /// # use serde_json::Number; /// for value in [ @@ -294,6 +294,8 @@ impl Number { /// let number: Number = serde_json::from_str(value).unwrap(); /// assert_eq!(number.as_str(), value); /// } + /// ``` + #[cfg(feature = "arbitrary_precision")] pub fn as_str(&self) -> &str { &self.n } From f16cad635d2aadb2ef6610e765f49eaabc1c9bf1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:15:56 -0700 Subject: [PATCH 16/18] Add cfg banner to documentation of Number::as_str --- src/number.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/number.rs b/src/number.rs index 6b3003f42..9e319e8f5 100644 --- a/src/number.rs +++ b/src/number.rs @@ -296,6 +296,7 @@ impl Number { /// } /// ``` #[cfg(feature = "arbitrary_precision")] + #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary_precision")))] pub fn as_str(&self) -> &str { &self.n } From f346308cda4cd5a7dae0327a757fe41f064a2161 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:19:07 -0700 Subject: [PATCH 17/18] Elaborate on documentation of Number::as_str --- src/number.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/number.rs b/src/number.rs index 9e319e8f5..b0231a87b 100644 --- a/src/number.rs +++ b/src/number.rs @@ -279,7 +279,12 @@ impl Number { } } - /// Returns the `&str` representation of the `Number`. + /// Returns the exact original JSON representation that this Number was + /// parsed from. + /// + /// For numbers constructed not via parsing, such as by `From`, returns + /// the JSON representation that serde\_json would serialize for this + /// number. /// /// ``` /// # use serde_json::Number; From 45f10ec816e3f2765ac08f7ca73752326b0475d7 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 9 Sep 2023 12:19:41 -0700 Subject: [PATCH 18/18] Release 1.0.106 --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 335ad3baa..9f046b559 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_json" -version = "1.0.105" # remember to update html_root_url +version = "1.0.106" # remember to update html_root_url authors = ["Erick Tryzelaar ", "David Tolnay "] categories = ["encoding", "parser-implementations", "no-std"] description = "A JSON serialization file format" diff --git a/src/lib.rs b/src/lib.rs index 109de45c3..7dc197ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -299,7 +299,7 @@ //! [macro]: crate::json //! [`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core -#![doc(html_root_url = "https://docs.rs/serde_json/1.0.105")] +#![doc(html_root_url = "https://docs.rs/serde_json/1.0.106")] // Ignored clippy lints #![allow( clippy::collapsible_else_if,