aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2022-03-08 22:02:28 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-03-08 22:02:28 +0000
commit49b2b027fb2cd07a2c9184d0e6aa046d3e3954c5 (patch)
tree58f7f050cf3633807c295fb927d3752b7b5a1a0d
parent3dc345ef1f9083d6dd70a8a74db41d7e91fdc6d1 (diff)
parent32d47430b7c92129423fa862b61fce050fce3a59 (diff)
downloadserde_json-android13-qpr3-c-s6-release.tar.gz
Merge "Update serde_json to 1.0.79" am: e3041fe03f am: a24a741dd2 am: 32d47430b7t_frc_odp_330442040t_frc_odp_330442000t_frc_ase_330444010android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32aml_go_odp_330912000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000android13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-mainline-go-adservices-releaseandroid13-frc-odp-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-release
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/serde_json/+/2005037 Change-Id: If88200b0ae08047f18997bbdae6bd40e6564ba8d
-rw-r--r--.cargo_vcs_info.json5
-rw-r--r--.github/workflows/ci.yml107
-rw-r--r--.gitignore4
-rw-r--r--Android.bp2
-rw-r--r--CONTRIBUTING.md46
-rw-r--r--Cargo.toml36
-rw-r--r--Cargo.toml.orig9
-rw-r--r--METADATA10
-rw-r--r--README.md29
-rw-r--r--build.rs6
-rw-r--r--src/de.rs54
-rw-r--r--src/error.rs9
-rw-r--r--src/io/core.rs4
-rw-r--r--src/lexical/algorithm.rs2
-rw-r--r--src/lexical/bhcomp.rs2
-rw-r--r--src/lexical/bignum.rs2
-rw-r--r--src/lexical/float.rs2
-rw-r--r--src/lexical/math.rs11
-rw-r--r--src/lexical/num.rs2
-rw-r--r--src/lexical/rounding.rs2
-rw-r--r--src/lexical/shift.rs2
-rw-r--r--src/lib.rs76
-rw-r--r--src/map.rs44
-rw-r--r--src/number.rs44
-rw-r--r--src/raw.rs21
-rw-r--r--src/read.rs133
-rw-r--r--src/ser.rs17
-rw-r--r--src/value/de.rs10
-rw-r--r--src/value/from.rs6
-rw-r--r--src/value/index.rs9
-rw-r--r--src/value/mod.rs8
-rw-r--r--src/value/partial_eq.rs2
-rw-r--r--src/value/ser.rs13
-rw-r--r--tests/compiletest.rs7
-rw-r--r--tests/debug.rs68
-rw-r--r--tests/lexical.rs52
-rw-r--r--tests/lexical/algorithm.rs110
-rw-r--r--tests/lexical/exponent.rs54
-rw-r--r--tests/lexical/float.rs581
-rw-r--r--tests/lexical/math.rs211
-rw-r--r--tests/lexical/num.rs76
-rw-r--r--tests/lexical/parse.rs204
-rw-r--r--tests/lexical/rounding.rs316
-rw-r--r--tests/macros/mod.rs59
-rw-r--r--tests/map.rs47
-rw-r--r--tests/regression.rs3
-rw-r--r--tests/regression/issue520.rs20
-rw-r--r--tests/regression/issue795.rs57
-rw-r--r--tests/regression/issue845.rs72
-rw-r--r--tests/stream.rs182
-rw-r--r--tests/test.rs2386
-rw-r--r--tests/ui/missing_colon.rs5
-rw-r--r--tests/ui/missing_colon.stderr7
-rw-r--r--tests/ui/missing_comma.rs5
-rw-r--r--tests/ui/missing_comma.stderr7
-rw-r--r--tests/ui/missing_value.rs5
-rw-r--r--tests/ui/missing_value.stderr7
-rw-r--r--tests/ui/not_found.rs5
-rw-r--r--tests/ui/not_found.stderr5
-rw-r--r--tests/ui/parse_expr.rs5
-rw-r--r--tests/ui/parse_expr.stderr5
-rw-r--r--tests/ui/parse_key.rs5
-rw-r--r--tests/ui/parse_key.stderr5
-rw-r--r--tests/ui/unexpected_after_array_element.rs5
-rw-r--r--tests/ui/unexpected_after_array_element.stderr5
-rw-r--r--tests/ui/unexpected_after_map_entry.rs5
-rw-r--r--tests/ui/unexpected_after_map_entry.stderr5
-rw-r--r--tests/ui/unexpected_colon.rs5
-rw-r--r--tests/ui/unexpected_colon.stderr5
-rw-r--r--tests/ui/unexpected_comma.rs5
-rw-r--r--tests/ui/unexpected_comma.stderr5
71 files changed, 5132 insertions, 208 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 76c8954..55d7152 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,6 @@
{
"git": {
- "sha1": "9420d3a0a4ebf17ef1deeecee07851f58ff6148f"
- }
+ "sha1": "7025523603fe604d11b92ccd4ca314e343d3ae50"
+ },
+ "path_in_vcs": ""
} \ No newline at end of file
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..b7e82ab
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,107 @@
+name: CI
+
+on:
+ push:
+ pull_request:
+ schedule: [cron: "40 1 * * *"]
+
+env:
+ RUSTFLAGS: -Dwarnings
+
+jobs:
+ test:
+ name: Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}}
+ runs-on: ${{matrix.os}}-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ os: [ubuntu, windows]
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@nightly
+ - run: cargo test
+ - run: cargo test --features preserve_order --tests -- --skip ui --exact
+ - run: cargo test --features float_roundtrip --tests -- --skip ui --exact
+ - run: cargo test --features arbitrary_precision --tests -- --skip ui --exact
+ - run: cargo test --features float_roundtrip,arbitrary_precision --tests -- --skip ui --exact
+ - run: cargo test --features raw_value --tests -- --skip ui --exact
+ - run: cargo test --features unbounded_depth --tests -- --skip ui --exact
+
+ build:
+ name: Rust ${{matrix.rust}} ${{matrix.os == 'windows' && '(windows)' || ''}}
+ runs-on: ${{matrix.os}}-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ rust: [beta, stable, 1.53.0, 1.46.0, 1.45.0, 1.40.0, 1.38.0, 1.36.0]
+ os: [ubuntu]
+ include:
+ - rust: stable
+ os: windows
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@master
+ with:
+ toolchain: ${{matrix.rust}}
+ - run: cargo check
+ - run: cargo check --features float_roundtrip
+ - run: cargo check --features arbitrary_precision
+ - run: cargo check --features raw_value
+ - run: cargo check --features unbounded_depth
+ - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc
+ - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,raw_value
+ - run: cargo check --features preserve_order
+ if: matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0'
+ - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,preserve_order
+ if: matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0'
+ - name: Build without std
+ run: |
+ rustup target add aarch64-unknown-none
+ cargo check \
+ --manifest-path tests/crate/Cargo.toml \
+ --target aarch64-unknown-none \
+ --no-default-features \
+ --features alloc
+ if: matrix.rust == 'stable' && matrix.os == 'ubuntu'
+
+ miri:
+ name: Miri
+ runs-on: ubuntu-latest
+ env:
+ MIRIFLAGS: "-Zmiri-tag-raw-pointers"
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@nightly
+ with:
+ components: miri
+ - run: cargo miri test
+ - run: cargo miri test --features preserve_order,float_roundtrip,arbitrary_precision,raw_value
+
+ clippy:
+ name: Clippy
+ runs-on: ubuntu-latest
+ if: github.event_name != 'pull_request'
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@clippy
+ - run: cargo clippy --tests -- -Dclippy::all -Dclippy::pedantic
+ - run: cargo clippy --all-features --tests -- -Dclippy::all -Dclippy::pedantic
+
+ docs:
+ name: Documentation
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@nightly
+ - run: cargo doc --features raw_value,unbounded_depth
+ env:
+ RUSTDOCFLAGS: --cfg docsrs
+
+ fuzz:
+ name: Fuzz
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - uses: dtolnay/rust-toolchain@nightly
+ - run: cargo install cargo-fuzz --debug
+ - run: cargo fuzz build -O
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..165eb22
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+target/
+**/*.rs.bk
+*.sw[po]
+Cargo.lock
diff --git a/Android.bp b/Android.bp
index ba9e37d..4768c80 100644
--- a/Android.bp
+++ b/Android.bp
@@ -43,7 +43,7 @@ rust_library {
host_supported: true,
crate_name: "serde_json",
cargo_env_compat: true,
- cargo_pkg_version: "1.0.68",
+ cargo_pkg_version: "1.0.79",
srcs: ["src/lib.rs"],
edition: "2018",
features: [
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..26e1578
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,46 @@
+# Contributing to Serde
+
+Serde welcomes contribution from everyone in the form of suggestions, bug
+reports, pull requests, and feedback. This document gives some guidance if you
+are thinking of helping us.
+
+## Submitting bug reports and feature requests
+
+Serde development is spread across lots of repositories. In general, prefer to
+open issues against the main [serde-rs/serde] repository unless the topic is
+clearly specific to JSON.
+
+[serde-rs/serde]: https://github.com/serde-rs/serde
+
+When reporting a bug or asking for help, please include enough details so that
+the people helping you can reproduce the behavior you are seeing. For some tips
+on how to approach this, read about how to produce a [Minimal, Complete, and
+Verifiable example].
+
+[Minimal, Complete, and Verifiable example]: https://stackoverflow.com/help/mcve
+
+When making a feature request, please make it clear what problem you intend to
+solve with the feature, any ideas for how Serde could support solving that
+problem, any possible alternatives, and any disadvantages.
+
+## Running the test suite
+
+We encourage you to check that the test suite passes locally before submitting a
+pull request with your changes. If anything does not pass, typically it will be
+easier to iterate and fix it locally than waiting for the CI servers to run
+tests for you.
+
+The test suite requires a nightly compiler.
+
+```sh
+# Run the full test suite, including doc test and compile-tests
+cargo test
+```
+
+## Conduct
+
+In all Serde-related forums, we follow the [Rust Code of Conduct]. For
+escalation or moderation issues please contact Erick (erick.tryzelaar@gmail.com)
+instead of the Rust moderation team.
+
+[Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct
diff --git a/Cargo.toml b/Cargo.toml
index bf7879d..cf800d8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,31 +11,45 @@
[package]
edition = "2018"
+rust-version = "1.36"
name = "serde_json"
-version = "1.0.68"
-authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
-include = ["build.rs", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
+version = "1.0.79"
+authors = [
+ "Erick Tryzelaar <erick.tryzelaar@gmail.com>",
+ "David Tolnay <dtolnay@gmail.com>",
+]
description = "A JSON serialization file format"
documentation = "https://docs.serde.rs/serde_json/"
readme = "README.md"
-keywords = ["json", "serde", "serialization"]
+keywords = [
+ "json",
+ "serde",
+ "serialization",
+]
categories = ["encoding"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/serde-rs/json"
+
[package.metadata.docs.rs]
-features = ["raw_value", "unbounded_depth"]
-rustdoc-args = ["--cfg", "docsrs"]
+features = [
+ "raw_value",
+ "unbounded_depth",
+]
targets = ["x86_64-unknown-linux-gnu"]
+rustdoc-args = [
+ "--cfg",
+ "docsrs",
+]
[package.metadata.playground]
features = ["raw_value"]
+
[dependencies.indexmap]
version = "1.5"
optional = true
[dependencies.itoa]
-version = "0.4.3"
-default-features = false
+version = "1.0"
[dependencies.ryu]
version = "1.0"
@@ -43,9 +57,13 @@ version = "1.0"
[dependencies.serde]
version = "1.0.100"
default-features = false
+
[dev-dependencies.automod]
version = "1.0"
+[dev-dependencies.ref-cast]
+version = "1.0"
+
[dev-dependencies.rustversion]
version = "1.0"
@@ -59,7 +77,7 @@ version = "1.0"
version = "0.1"
[dev-dependencies.trybuild]
-version = "1.0.19"
+version = "1.0.49"
features = ["diff"]
[features]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 5c647a1..1513e9b 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
[package]
name = "serde_json"
-version = "1.0.68" # remember to update html_root_url
+version = "1.0.79" # remember to update html_root_url
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
license = "MIT OR Apache-2.0"
description = "A JSON serialization file format"
@@ -9,22 +9,23 @@ documentation = "https://docs.serde.rs/serde_json/"
keywords = ["json", "serde", "serialization"]
categories = ["encoding"]
readme = "README.md"
-include = ["build.rs", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
edition = "2018"
+rust-version = "1.36"
[dependencies]
serde = { version = "1.0.100", default-features = false }
indexmap = { version = "1.5", optional = true }
-itoa = { version = "0.4.3", default-features = false }
+itoa = "1.0"
ryu = "1.0"
[dev-dependencies]
automod = "1.0"
+ref-cast = "1.0"
rustversion = "1.0"
serde_bytes = "0.11"
serde_derive = "1.0"
serde_stacker = "0.1"
-trybuild = { version = "1.0.19", features = ["diff"] }
+trybuild = { version = "1.0.49", features = ["diff"] }
[workspace]
members = ["tests/crate"]
diff --git a/METADATA b/METADATA
index f82a5b2..b5c0cc6 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/serde_json/serde_json-1.0.68.crate"
+ value: "https://static.crates.io/crates/serde_json/serde_json-1.0.79.crate"
}
- version: "1.0.68"
+ version: "1.0.79"
license_type: NOTICE
last_upgrade_date {
- year: 2021
- month: 9
- day: 22
+ year: 2022
+ month: 3
+ day: 1
}
}
diff --git a/README.md b/README.md
index 96587a0..6d8af69 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,11 @@
-# Serde JSON &emsp; [![Build Status]][travis] [![Latest Version]][crates.io] [![Rustc Version 1.31+]][rustc]
+# Serde JSON &emsp; [![Build Status]][travis] [![Latest Version]][crates.io] [![Rustc Version 1.36+]][rustc]
[Build Status]: https://img.shields.io/github/workflow/status/serde-rs/json/CI/master
[travis]: https://github.com/serde-rs/json/actions?query=branch%3Amaster
[Latest Version]: https://img.shields.io/crates/v/serde_json.svg
[crates.io]: https://crates.io/crates/serde\_json
-[Rustc Version 1.31+]: https://img.shields.io/badge/rustc-1.31+-lightgray.svg
-[rustc]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
+[Rustc Version 1.36+]: https://img.shields.io/badge/rustc-1.36+-lightgray.svg
+[rustc]: https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
@@ -330,17 +330,20 @@ Benchmarks live in the [serde-rs/json-benchmark] repo.
Serde is one of the most widely used Rust libraries so any place that Rustaceans
congregate will be able to help you out. For chat, consider trying the
-[#general] or [#beginners] channels of the unofficial community Discord, the
-[#rust-usage] channel of the official Rust Project Discord, or the
-[#general][zulip] stream in Zulip. For asynchronous, consider the [\[rust\] tag
-on StackOverflow][stackoverflow], the [/r/rust] subreddit which has a pinned
-weekly easy questions post, or the Rust [Discourse forum][discourse]. It's
-acceptable to file a support issue in this repo but they tend not to get as many
-eyes as any of the above and may get closed without a response after some time.
-
-[#general]: https://discord.com/channels/273534239310479360/274215136414400513
-[#beginners]: https://discord.com/channels/273534239310479360/273541522815713281
+[#rust-questions] or [#rust-beginners] channels of the unofficial community
+Discord (invite: <https://discord.gg/rust-lang-community>), the [#rust-usage] or
+[#beginners] channels of the official Rust Project Discord (invite:
+<https://discord.gg/rust-lang>), or the [#general][zulip] stream in Zulip. For
+asynchronous, consider the [\[rust\] tag on StackOverflow][stackoverflow], the
+[/r/rust] subreddit which has a pinned weekly easy questions post, or the Rust
+[Discourse forum][discourse]. It's acceptable to file a support issue in this
+repo but they tend not to get as many eyes as any of the above and may get
+closed without a response after some time.
+
+[#rust-questions]: https://discord.com/channels/273534239310479360/274215136414400513
+[#rust-beginners]: https://discord.com/channels/273534239310479360/273541522815713281
[#rust-usage]: https://discord.com/channels/442252698964721669/443150878111694848
+[#beginners]: https://discord.com/channels/442252698964721669/448238009733742612
[zulip]: https://rust-lang.zulipchat.com/#narrow/stream/122651-general
[stackoverflow]: https://stackoverflow.com/questions/tagged/rust
[/r/rust]: https://www.reddit.com/r/rust
diff --git a/build.rs b/build.rs
index 04ff4a0..e9ec7d5 100644
--- a/build.rs
+++ b/build.rs
@@ -31,6 +31,12 @@ fn main() {
if minor < 45 {
println!("cargo:rustc-cfg=no_btreemap_remove_entry");
}
+
+ // BTreeMap::retain
+ // https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#stabilized-apis
+ if minor < 53 {
+ println!("cargo:rustc-cfg=no_btreemap_retain");
+ }
}
fn rustc_minor_version() -> Option<u32> {
diff --git a/src/de.rs b/src/de.rs
index a2f34b9..ffd0d48 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -3,10 +3,16 @@
use crate::error::{Error, ErrorCode, Result};
#[cfg(feature = "float_roundtrip")]
use crate::lexical;
-use crate::lib::str::FromStr;
-use crate::lib::*;
use crate::number::Number;
use crate::read::{self, Fused, Reference};
+use alloc::string::String;
+use alloc::vec::Vec;
+#[cfg(feature = "float_roundtrip")]
+use core::iter;
+use core::iter::FusedIterator;
+use core::marker::PhantomData;
+use core::result;
+use core::str::FromStr;
use serde::de::{self, Expected, Unexpected};
use serde::{forward_to_deserialize_any, serde_if_integer128};
@@ -87,7 +93,9 @@ impl<'a> Deserializer<read::StrRead<'a>> {
macro_rules! overflow {
($a:ident * 10 + $b:ident, $c:expr) => {
- $a >= $c / 10 && ($a > $c / 10 || $b > $c % 10)
+ match $c {
+ c => $a >= c / 10 && ($a > c / 10 || $b > c % 10),
+ }
};
}
@@ -856,6 +864,15 @@ impl<'de, R: Read<'de>> Deserializer<R> {
buf.push('-');
}
self.scan_integer(&mut buf)?;
+ if positive {
+ if let Ok(unsigned) = buf.parse() {
+ return Ok(ParserNumber::U64(unsigned));
+ }
+ } else {
+ if let Ok(signed) = buf.parse() {
+ return Ok(ParserNumber::I64(signed));
+ }
+ }
Ok(ParserNumber::String(buf))
}
@@ -1560,7 +1577,8 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
///
/// # Examples
///
- /// You can use this to parse JSON strings containing invalid UTF-8 bytes.
+ /// You can use this to parse JSON strings containing invalid UTF-8 bytes,
+ /// or unpaired surrogates.
///
/// ```
/// use serde_bytes::ByteBuf;
@@ -1580,20 +1598,18 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
/// ```
///
/// Backslash escape sequences like `\n` are still interpreted and required
- /// to be valid, and `\u` escape sequences are required to represent valid
- /// Unicode code points.
+ /// to be valid. `\u` escape sequences are required to represent a valid
+ /// Unicode code point or lone surrogate.
///
/// ```
/// use serde_bytes::ByteBuf;
///
- /// fn look_at_bytes() {
- /// let json_data = b"\"invalid unicode surrogate: \\uD801\"";
- /// let parsed: Result<ByteBuf, _> = serde_json::from_slice(json_data);
- ///
- /// assert!(parsed.is_err());
- ///
- /// let expected_msg = "unexpected end of hex escape at line 1 column 35";
- /// assert_eq!(expected_msg, parsed.unwrap_err().to_string());
+ /// fn look_at_bytes() -> Result<(), serde_json::Error> {
+ /// let json_data = b"\"lone surrogate: \\uD801\"";
+ /// let bytes: ByteBuf = serde_json::from_slice(json_data)?;
+ /// let expected = b"lone surrogate: \xED\xA0\x81";
+ /// assert_eq!(expected, bytes.as_slice());
+ /// Ok(())
/// }
/// #
/// # look_at_bytes();
@@ -2168,10 +2184,18 @@ where
}
#[inline]
- fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
+ fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
+ #[cfg(feature = "raw_value")]
+ {
+ if name == crate::raw::TOKEN {
+ return self.de.deserialize_raw_value(visitor);
+ }
+ }
+
+ let _ = name;
visitor.visit_newtype_struct(self)
}
diff --git a/src/error.rs b/src/error.rs
index 4219d32..6390c43 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,9 +1,14 @@
//! When serializing or deserializing JSON goes wrong.
use crate::io;
-use crate::lib::str::FromStr;
-use crate::lib::*;
+use alloc::boxed::Box;
+use alloc::string::{String, ToString};
+use core::fmt::{self, Debug, Display};
+use core::result;
+use core::str::FromStr;
use serde::{de, ser};
+#[cfg(feature = "std")]
+use std::error;
/// This type represents all possible errors that can occur when serializing or
/// deserializing JSON data.
diff --git a/src/io/core.rs b/src/io/core.rs
index 97354eb..465ab8b 100644
--- a/src/io/core.rs
+++ b/src/io/core.rs
@@ -1,7 +1,9 @@
//! Reimplements core logic and types from `std::io` in an `alloc`-friendly
//! fashion.
-use crate::lib::*;
+use alloc::vec::Vec;
+use core::fmt::{self, Display};
+use core::result;
pub enum ErrorKind {
Other,
diff --git a/src/lexical/algorithm.rs b/src/lexical/algorithm.rs
index d01e5b9..a2cbf18 100644
--- a/src/lexical/algorithm.rs
+++ b/src/lexical/algorithm.rs
@@ -133,7 +133,7 @@ where
let shift = fp.normalize();
errors <<= shift;
- u64::error_is_accurate::<F>(errors, &fp)
+ u64::error_is_accurate::<F>(errors, fp)
}
}
diff --git a/src/lexical/bhcomp.rs b/src/lexical/bhcomp.rs
index 6d76c59..1f2a7bb 100644
--- a/src/lexical/bhcomp.rs
+++ b/src/lexical/bhcomp.rs
@@ -12,7 +12,7 @@ use super::float::*;
use super::math::*;
use super::num::*;
use super::rounding::*;
-use crate::lib::{cmp, mem};
+use core::{cmp, mem};
// MANTISSA
diff --git a/src/lexical/bignum.rs b/src/lexical/bignum.rs
index dee4e68..f9551f5 100644
--- a/src/lexical/bignum.rs
+++ b/src/lexical/bignum.rs
@@ -3,7 +3,7 @@
//! Big integer type definition.
use super::math::*;
-use crate::lib::Vec;
+use alloc::vec::Vec;
/// Storage for a big integer type.
#[derive(Clone, PartialEq, Eq)]
diff --git a/src/lexical/float.rs b/src/lexical/float.rs
index e320ead..2d434a2 100644
--- a/src/lexical/float.rs
+++ b/src/lexical/float.rs
@@ -108,7 +108,7 @@ impl ExtendedFloat {
F: Float,
Algorithm: FnOnce(&mut ExtendedFloat, i32),
{
- round_to_native::<F, _>(self, algorithm)
+ round_to_native::<F, _>(self, algorithm);
}
// FROM
diff --git a/src/lexical/math.rs b/src/lexical/math.rs
index 4890c0f..37cc1d2 100644
--- a/src/lexical/math.rs
+++ b/src/lexical/math.rs
@@ -9,7 +9,8 @@
use super::large_powers;
use super::num::*;
use super::small_powers::*;
-use crate::lib::{cmp, iter, mem, Vec};
+use alloc::vec::Vec;
+use core::{cmp, iter, mem};
// ALIASES
// -------
@@ -593,7 +594,7 @@ mod large {
// Iteratively add elements from y to x.
let mut carry = false;
- for (xi, yi) in (&mut x[xstart..]).iter_mut().zip(y.iter()) {
+ for (xi, yi) in x[xstart..].iter_mut().zip(y.iter()) {
// Only one op of the two can overflow, since we added at max
// Limb::max_value() + Limb::max_value(). Add the previous carry,
// and store the current carry for the next.
@@ -613,7 +614,7 @@ mod large {
/// AddAssign bigint to bigint.
#[inline]
pub fn iadd(x: &mut Vec<Limb>, y: &[Limb]) {
- iadd_impl(x, y, 0)
+ iadd_impl(x, y, 0);
}
/// Add bigint to bigint.
@@ -859,13 +860,13 @@ pub(crate) trait Math: Clone + Sized + Default {
/// Multiply by a power of 2.
#[inline]
fn imul_pow2(&mut self, n: u32) {
- self.ishl(n as usize)
+ self.ishl(n as usize);
}
/// Multiply by a power of 5.
#[inline]
fn imul_pow5(&mut self, n: u32) {
- small::imul_pow5(self.data_mut(), n)
+ small::imul_pow5(self.data_mut(), n);
}
/// MulAssign by a power of 10.
diff --git a/src/lexical/num.rs b/src/lexical/num.rs
index 27c78ed..e47e003 100644
--- a/src/lexical/num.rs
+++ b/src/lexical/num.rs
@@ -2,7 +2,7 @@
//! Utilities for Rust numbers.
-use crate::lib::ops;
+use core::ops;
/// Precalculated values of radix**i for i in range [0, arr.len()-1].
/// Each value can be **exactly** represented as that type.
diff --git a/src/lexical/rounding.rs b/src/lexical/rounding.rs
index d2704c9..6ec1292 100644
--- a/src/lexical/rounding.rs
+++ b/src/lexical/rounding.rs
@@ -5,7 +5,7 @@
use super::float::ExtendedFloat;
use super::num::*;
use super::shift::*;
-use crate::lib::mem;
+use core::mem;
// MASKS
diff --git a/src/lexical/shift.rs b/src/lexical/shift.rs
index b0bd469..a0bae01 100644
--- a/src/lexical/shift.rs
+++ b/src/lexical/shift.rs
@@ -3,7 +3,7 @@
//! Bit-shift helpers.
use super::float::ExtendedFloat;
-use crate::lib::mem;
+use core::mem;
// Shift extended-precision float right `shift` bytes.
#[inline]
diff --git a/src/lib.rs b/src/lib.rs
index 319ce71..63846e7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -300,10 +300,10 @@
//! [macro]: https://docs.serde.rs/serde_json/macro.json.html
//! [`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core
-#![doc(html_root_url = "https://docs.rs/serde_json/1.0.68")]
-#![deny(clippy::all, clippy::pedantic)]
+#![doc(html_root_url = "https://docs.rs/serde_json/1.0.79")]
// Ignored clippy lints
#![allow(
+ clippy::collapsible_else_if,
clippy::comparison_chain,
clippy::deprecated_cfg_attr,
clippy::doc_markdown,
@@ -313,6 +313,10 @@
clippy::match_like_matches_macro,
clippy::match_single_binding,
clippy::needless_doctest_main,
+ clippy::needless_late_init,
+ // clippy bug: https://github.com/rust-lang/rust-clippy/issues/8366
+ clippy::ptr_arg,
+ clippy::return_self_not_must_use,
clippy::transmute_ptr_to_ptr,
clippy::unnecessary_wraps,
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
@@ -320,6 +324,8 @@
)]
// Ignored clippy_pedantic lints
#![allow(
+ // buggy
+ clippy::iter_not_returning_iterator, // https://github.com/rust-lang/rust-clippy/issues/8285
// Deserializer::from_str, into_iter
clippy::should_implement_trait,
// integer and float ser/de requires these sorts of casts
@@ -359,65 +365,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
-////////////////////////////////////////////////////////////////////////////////
-
-#[cfg(not(feature = "std"))]
extern crate alloc;
-/// A facade around all the types we need from the `std`, `core`, and `alloc`
-/// crates. This avoids elaborate import wrangling having to happen in every
-/// module.
-mod lib {
- mod core {
- #[cfg(not(feature = "std"))]
- pub use core::*;
- #[cfg(feature = "std")]
- pub use std::*;
- }
-
- pub use self::core::cell::{Cell, RefCell};
- pub use self::core::clone::{self, Clone};
- pub use self::core::convert::{self, From, Into};
- pub use self::core::default::{self, Default};
- pub use self::core::fmt::{self, Debug, Display};
- pub use self::core::hash::{self, Hash};
- pub use self::core::iter::FusedIterator;
- pub use self::core::marker::{self, PhantomData};
- pub use self::core::ops::{Bound, RangeBounds};
- pub use self::core::result::{self, Result};
- pub use self::core::{borrow, char, cmp, iter, mem, num, ops, slice, str};
-
- #[cfg(not(feature = "std"))]
- pub use alloc::borrow::{Cow, ToOwned};
- #[cfg(feature = "std")]
- pub use std::borrow::{Cow, ToOwned};
-
- #[cfg(not(feature = "std"))]
- pub use alloc::string::{String, ToString};
- #[cfg(feature = "std")]
- pub use std::string::{String, ToString};
-
- #[cfg(not(feature = "std"))]
- pub use alloc::vec::{self, Vec};
- #[cfg(feature = "std")]
- pub use std::vec::{self, Vec};
-
- #[cfg(not(feature = "std"))]
- pub use alloc::boxed::Box;
- #[cfg(feature = "std")]
- pub use std::boxed::Box;
-
- #[cfg(not(feature = "std"))]
- pub use alloc::collections::{btree_map, BTreeMap};
- #[cfg(feature = "std")]
- pub use std::collections::{btree_map, BTreeMap};
-
- #[cfg(feature = "std")]
- pub use std::error;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
#[cfg(feature = "std")]
#[doc(inline)]
pub use crate::de::from_reader;
@@ -436,15 +385,12 @@ pub use crate::value::{from_value, to_value, Map, Number, Value};
// We only use our own error type; no need for From conversions provided by the
// standard library's try! macro. This reduces lines of LLVM IR by 4%.
macro_rules! tri {
- ($e:expr) => {
+ ($e:expr $(,)?) => {
match $e {
- crate::lib::Result::Ok(val) => val,
- crate::lib::Result::Err(err) => return crate::lib::Result::Err(err),
+ core::result::Result::Ok(val) => val,
+ core::result::Result::Err(err) => return core::result::Result::Err(err),
}
};
- ($e:expr,) => {
- tri!($e)
- };
}
#[macro_use]
diff --git a/src/map.rs b/src/map.rs
index 716f128..146eb6a 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -6,12 +6,19 @@
//! [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
//! [`IndexMap`]: https://docs.rs/indexmap/*/indexmap/map/struct.IndexMap.html
-use crate::lib::borrow::Borrow;
-use crate::lib::iter::FromIterator;
-use crate::lib::*;
use crate::value::Value;
+use alloc::string::String;
+use core::borrow::Borrow;
+use core::fmt::{self, Debug};
+use core::hash::Hash;
+use core::iter::{FromIterator, FusedIterator};
+#[cfg(feature = "preserve_order")]
+use core::mem;
+use core::ops;
use serde::de;
+#[cfg(not(feature = "preserve_order"))]
+use alloc::collections::{btree_map, BTreeMap};
#[cfg(feature = "preserve_order")]
use indexmap::{self, IndexMap};
@@ -94,6 +101,20 @@ impl Map<String, Value> {
self.map.get_mut(key)
}
+ /// Returns the key-value pair matching the given key.
+ ///
+ /// The key may be any borrowed form of the map's key type, but the ordering
+ /// on the borrowed form *must* match the ordering on the key type.
+ #[inline]
+ #[cfg(any(feature = "preserve_order", not(no_btreemap_get_key_value)))]
+ pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)>
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ self.map.get_key_value(key)
+ }
+
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, `None` is returned.
@@ -151,6 +172,8 @@ impl Map<String, Value> {
no_btreemap_get_key_value,
))]
{
+ use core::ops::{Bound, RangeBounds};
+
struct Key<'a, Q: ?Sized>(&'a Q);
impl<'a, Q: ?Sized> RangeBounds<Q> for Key<'a, Q> {
@@ -188,7 +211,7 @@ impl Map<String, Value> {
S: Into<String>,
{
#[cfg(not(feature = "preserve_order"))]
- use crate::lib::btree_map::Entry as EntryImpl;
+ use alloc::collections::btree_map::Entry as EntryImpl;
#[cfg(feature = "preserve_order")]
use indexmap::map::Entry as EntryImpl;
@@ -249,6 +272,19 @@ impl Map<String, Value> {
iter: self.map.values_mut(),
}
}
+
+ /// Retains only the elements specified by the predicate.
+ ///
+ /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)`
+ /// returns `false`.
+ #[cfg(not(no_btreemap_retain))]
+ #[inline]
+ pub fn retain<F>(&mut self, f: F)
+ where
+ F: FnMut(&String, &mut Value) -> bool,
+ {
+ self.map.retain(f);
+ }
}
#[allow(clippy::derivable_impls)] // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7655
diff --git a/src/number.rs b/src/number.rs
index c147618..b965271 100644
--- a/src/number.rs
+++ b/src/number.rs
@@ -1,6 +1,8 @@
use crate::de::ParserNumber;
use crate::error::Error;
-use crate::lib::*;
+use core::fmt::{self, Debug, Display};
+#[cfg(not(feature = "arbitrary_precision"))]
+use core::hash::{Hash, Hasher};
use serde::de::{self, Unexpected, Visitor};
use serde::{
forward_to_deserialize_any, serde_if_integer128, Deserialize, Deserializer, Serialize,
@@ -16,13 +18,13 @@ use serde::de::{IntoDeserializer, MapAccess};
pub(crate) const TOKEN: &str = "$serde_json::private::Number";
/// Represents a JSON number, whether integer or floating point.
-#[derive(Clone, Eq, PartialEq)]
+#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Number {
n: N,
}
#[cfg(not(feature = "arbitrary_precision"))]
-#[derive(Copy, Clone, PartialEq)]
+#[derive(Copy, Clone)]
enum N {
PosInt(u64),
/// Always less than zero.
@@ -31,10 +33,42 @@ enum N {
Float(f64),
}
+#[cfg(not(feature = "arbitrary_precision"))]
+impl PartialEq for N {
+ fn eq(&self, other: &Self) -> bool {
+ match (self, other) {
+ (N::PosInt(a), N::PosInt(b)) => a == b,
+ (N::NegInt(a), N::NegInt(b)) => a == b,
+ (N::Float(a), N::Float(b)) => a == b,
+ _ => false,
+ }
+ }
+}
+
// Implementing Eq is fine since any float values are always finite.
#[cfg(not(feature = "arbitrary_precision"))]
impl Eq for N {}
+#[cfg(not(feature = "arbitrary_precision"))]
+impl Hash for N {
+ fn hash<H: Hasher>(&self, h: &mut H) {
+ match *self {
+ N::PosInt(i) => i.hash(h),
+ N::NegInt(i) => i.hash(h),
+ N::Float(f) => {
+ if f == 0.0f64 {
+ // There are 2 zero representations, +0 and -0, which
+ // compare equal but have different bits. We use the +0 hash
+ // for both so that hash(+0) == hash(-0).
+ 0.0f64.to_bits().hash(h);
+ } else {
+ f.to_bits().hash(h);
+ }
+ }
+ }
+ }
+}
+
#[cfg(feature = "arbitrary_precision")]
type N = String;
@@ -130,7 +164,7 @@ impl Number {
{
for c in self.n.chars() {
if c == '.' || c == 'e' || c == 'E' {
- return self.n.parse::<f64>().ok().map_or(false, |f| f.is_finite());
+ return self.n.parse::<f64>().ok().map_or(false, f64::is_finite);
}
}
false
@@ -254,7 +288,7 @@ impl Number {
}
}
-impl fmt::Display for Number {
+impl Display for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.n {
diff --git a/src/raw.rs b/src/raw.rs
index b8d04ee..c8377ac 100644
--- a/src/raw.rs
+++ b/src/raw.rs
@@ -1,5 +1,9 @@
use crate::error::Error;
-use crate::lib::*;
+use alloc::borrow::ToOwned;
+use alloc::boxed::Box;
+use alloc::string::String;
+use core::fmt::{self, Debug, Display};
+use core::mem;
use serde::de::value::BorrowedStrDeserializer;
use serde::de::{
self, Deserialize, DeserializeSeed, Deserializer, IntoDeserializer, MapAccess, Unexpected,
@@ -122,6 +126,10 @@ impl RawValue {
fn from_owned(json: Box<str>) -> Box<Self> {
unsafe { mem::transmute::<Box<str>, Box<RawValue>>(json) }
}
+
+ fn into_owned(raw_value: Box<Self>) -> Box<str> {
+ unsafe { mem::transmute::<Box<RawValue>, Box<str>>(raw_value) }
+ }
}
impl Clone for Box<RawValue> {
@@ -216,6 +224,12 @@ impl RawValue {
}
}
+impl From<Box<RawValue>> for Box<str> {
+ fn from(raw_value: Box<RawValue>) -> Self {
+ RawValue::into_owned(raw_value)
+ }
+}
+
/// Convert a `T` into a boxed `RawValue`.
///
/// # Example
@@ -271,7 +285,7 @@ impl RawValue {
#[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))]
pub fn to_raw_value<T>(value: &T) -> Result<Box<RawValue>, Error>
where
- T: Serialize,
+ T: ?Sized + Serialize,
{
let json_string = crate::to_string(value)?;
Ok(RawValue::from_owned(json_string.into_boxed_str()))
@@ -435,9 +449,10 @@ impl<'de> Visitor<'de> for BoxedFromString {
where
E: de::Error,
{
- self.visit_string(s.to_owned())
+ Ok(RawValue::from_owned(s.to_owned().into_boxed_str()))
}
+ #[cfg(any(feature = "std", feature = "alloc"))]
fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: de::Error,
diff --git a/src/read.rs b/src/read.rs
index 8ed3d7d..1319d89 100644
--- a/src/read.rs
+++ b/src/read.rs
@@ -1,6 +1,9 @@
use crate::error::{Error, ErrorCode, Result};
-use crate::lib::ops::Deref;
-use crate::lib::*;
+use alloc::vec::Vec;
+use core::char;
+use core::cmp;
+use core::ops::Deref;
+use core::str;
#[cfg(feature = "std")]
use crate::io;
@@ -225,7 +228,7 @@ where
return result(self, scratch);
}
b'\\' => {
- tri!(parse_escape(self, scratch));
+ tri!(parse_escape(self, validate, scratch));
}
_ => {
if validate {
@@ -465,7 +468,7 @@ impl<'a> SliceRead<'a> {
b'\\' => {
scratch.extend_from_slice(&self.slice[start..self.index]);
self.index += 1;
- tri!(parse_escape(self, scratch));
+ tri!(parse_escape(self, validate, scratch));
start = self.index;
}
_ => {
@@ -654,8 +657,9 @@ impl<'a> Read<'a> for StrRead<'a> {
fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
- // The input is assumed to be valid UTF-8 and the \u-escapes are
- // checked along the way, so don't need to check here.
+ // The deserialization input came in as &str with a UTF-8 guarantee,
+ // and the \u-escapes are checked along the way, so don't need to
+ // check here.
Ok(unsafe { str::from_utf8_unchecked(bytes) })
})
}
@@ -677,7 +681,7 @@ impl<'a> Read<'a> for StrRead<'a> {
#[cfg(feature = "raw_value")]
fn begin_raw_buffering(&mut self) {
- self.delegate.begin_raw_buffering()
+ self.delegate.begin_raw_buffering();
}
#[cfg(feature = "raw_value")]
@@ -753,7 +757,7 @@ where
#[cfg(feature = "raw_value")]
fn begin_raw_buffering(&mut self) {
- R::begin_raw_buffering(self)
+ R::begin_raw_buffering(self);
}
#[cfg(feature = "raw_value")]
@@ -816,6 +820,16 @@ where
}
}
+fn peek_or_eof<'de, R>(read: &mut R) -> Result<u8>
+where
+ R: ?Sized + Read<'de>,
+{
+ match tri!(read.peek()) {
+ Some(b) => Ok(b),
+ None => error(read, ErrorCode::EofWhileParsingString),
+ }
+}
+
fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
where
R: ?Sized + Read<'de>,
@@ -830,7 +844,11 @@ fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
/// Parses a JSON escape sequence and appends it into the scratch space. Assumes
/// the previous byte read was a backslash.
-fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()> {
+fn parse_escape<'de, R: Read<'de>>(
+ read: &mut R,
+ validate: bool,
+ scratch: &mut Vec<u8>,
+) -> Result<()> {
let ch = tri!(next_or_eof(read));
match ch {
@@ -843,19 +861,56 @@ fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Resul
b'r' => scratch.push(b'\r'),
b't' => scratch.push(b'\t'),
b'u' => {
+ fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
+ scratch.extend_from_slice(&[
+ (n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
+ (n >> 6 & 0b0011_1111) as u8 | 0b1000_0000,
+ (n & 0b0011_1111) as u8 | 0b1000_0000,
+ ]);
+ }
+
let c = match tri!(read.decode_hex_escape()) {
- 0xDC00..=0xDFFF => {
- return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
+ n @ 0xDC00..=0xDFFF => {
+ return if validate {
+ error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
+ } else {
+ encode_surrogate(scratch, n);
+ Ok(())
+ };
}
- // Non-BMP characters are encoded as a sequence of
- // two hex escapes, representing UTF-16 surrogates.
+ // Non-BMP characters are encoded as a sequence of two hex
+ // escapes, representing UTF-16 surrogates. If deserializing a
+ // utf-8 string the surrogates are required to be paired,
+ // whereas deserializing a byte string accepts lone surrogates.
n1 @ 0xD800..=0xDBFF => {
- if tri!(next_or_eof(read)) != b'\\' {
- return error(read, ErrorCode::UnexpectedEndOfHexEscape);
+ if tri!(peek_or_eof(read)) == b'\\' {
+ read.discard();
+ } else {
+ return if validate {
+ read.discard();
+ error(read, ErrorCode::UnexpectedEndOfHexEscape)
+ } else {
+ encode_surrogate(scratch, n1);
+ Ok(())
+ };
}
- if tri!(next_or_eof(read)) != b'u' {
- return error(read, ErrorCode::UnexpectedEndOfHexEscape);
+
+ if tri!(peek_or_eof(read)) == b'u' {
+ read.discard();
+ } else {
+ return if validate {
+ read.discard();
+ error(read, ErrorCode::UnexpectedEndOfHexEscape)
+ } else {
+ encode_surrogate(scratch, n1);
+ // The \ prior to this byte started an escape sequence,
+ // so we need to parse that now. This recursive call
+ // does not blow the stack on malicious input because
+ // the escape is not \u, so it will be handled by one
+ // of the easy nonrecursive cases.
+ parse_escape(read, validate, scratch)
+ };
}
let n2 = tri!(read.decode_hex_escape());
@@ -874,12 +929,9 @@ fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Resul
}
}
- n => match char::from_u32(n as u32) {
- Some(c) => c,
- None => {
- return error(read, ErrorCode::InvalidUnicodeCodePoint);
- }
- },
+ // Every u16 outside of the surrogate ranges above is guaranteed
+ // to be a legal char.
+ n => char::from_u32(n as u32).unwrap(),
};
scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
@@ -903,36 +955,13 @@ where
match ch {
b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
b'u' => {
- let n = match tri!(read.decode_hex_escape()) {
- 0xDC00..=0xDFFF => {
- return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
- }
+ // At this point we don't care if the codepoint is valid. We just
+ // want to consume it. We don't actually know what is valid or not
+ // at this point, because that depends on if this string will
+ // ultimately be parsed into a string or a byte buffer in the "real"
+ // parse.
- // Non-BMP characters are encoded as a sequence of
- // two hex escapes, representing UTF-16 surrogates.
- n1 @ 0xD800..=0xDBFF => {
- if tri!(next_or_eof(read)) != b'\\' {
- return error(read, ErrorCode::UnexpectedEndOfHexEscape);
- }
- if tri!(next_or_eof(read)) != b'u' {
- return error(read, ErrorCode::UnexpectedEndOfHexEscape);
- }
-
- let n2 = tri!(read.decode_hex_escape());
-
- if n2 < 0xDC00 || n2 > 0xDFFF {
- return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
- }
-
- (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000
- }
-
- n => n as u32,
- };
-
- if char::from_u32(n).is_none() {
- return error(read, ErrorCode::InvalidUnicodeCodePoint);
- }
+ tri!(read.decode_hex_escape());
}
_ => {
return error(read, ErrorCode::InvalidEscape);
diff --git a/src/ser.rs b/src/ser.rs
index 54b0bf2..db77cd8 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -2,8 +2,10 @@
use crate::error::{Error, ErrorCode, Result};
use crate::io;
-use crate::lib::num::FpCategory;
-use crate::lib::*;
+use alloc::string::{String, ToString};
+use alloc::vec::Vec;
+use core::fmt::{self, Display};
+use core::num::FpCategory;
use serde::ser::{self, Impossible, Serialize};
use serde::serde_if_integer128;
@@ -756,7 +758,7 @@ where
#[cfg(feature = "arbitrary_precision")]
Compound::Number { ref mut ser, .. } => {
if key == crate::number::TOKEN {
- tri!(value.serialize(NumberStrEmitter(&mut *ser)));
+ tri!(value.serialize(NumberStrEmitter(ser)));
Ok(())
} else {
Err(invalid_number())
@@ -765,7 +767,7 @@ where
#[cfg(feature = "raw_value")]
Compound::RawValue { ref mut ser, .. } => {
if key == crate::raw::TOKEN {
- tri!(value.serialize(RawValueStrEmitter(&mut *ser)));
+ tri!(value.serialize(RawValueStrEmitter(ser)));
Ok(())
} else {
Err(invalid_raw_value())
@@ -1546,6 +1548,13 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a,
) -> Result<Self::SerializeStructVariant> {
Err(ser::Error::custom("expected RawValue"))
}
+
+ fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
+ where
+ T: ?Sized + Display,
+ {
+ self.serialize_str(&value.to_string())
+ }
}
/// Represents a character escape code in a type-safe manner.
diff --git a/src/value/de.rs b/src/value/de.rs
index 24ca826..75e49df 100644
--- a/src/value/de.rs
+++ b/src/value/de.rs
@@ -1,9 +1,15 @@
use crate::error::Error;
-use crate::lib::str::FromStr;
-use crate::lib::*;
use crate::map::Map;
use crate::number::Number;
use crate::value::Value;
+use alloc::borrow::{Cow, ToOwned};
+use alloc::string::String;
+#[cfg(feature = "raw_value")]
+use alloc::string::ToString;
+use alloc::vec::{self, Vec};
+use core::fmt;
+use core::slice;
+use core::str::FromStr;
use serde::de::{
self, Deserialize, DeserializeSeed, EnumAccess, Expected, IntoDeserializer, MapAccess,
SeqAccess, Unexpected, VariantAccess, Visitor,
diff --git a/src/value/from.rs b/src/value/from.rs
index 59e09fd..7b37ef6 100644
--- a/src/value/from.rs
+++ b/src/value/from.rs
@@ -1,8 +1,10 @@
use super::Value;
-use crate::lib::iter::FromIterator;
-use crate::lib::*;
use crate::map::Map;
use crate::number::Number;
+use alloc::borrow::Cow;
+use alloc::string::{String, ToString};
+use alloc::vec::Vec;
+use core::iter::FromIterator;
#[cfg(feature = "arbitrary_precision")]
use serde::serde_if_integer128;
diff --git a/src/value/index.rs b/src/value/index.rs
index d759a1d..0d90a5d 100644
--- a/src/value/index.rs
+++ b/src/value/index.rs
@@ -1,6 +1,9 @@
use super::Value;
-use crate::lib::*;
use crate::map::Map;
+use alloc::borrow::ToOwned;
+use alloc::string::String;
+use core::fmt::{self, Display};
+use core::ops;
/// A type that can be used to index into a `serde_json::Value`.
///
@@ -133,14 +136,14 @@ mod private {
pub trait Sealed {}
impl Sealed for usize {}
impl Sealed for str {}
- impl Sealed for super::String {}
+ impl Sealed for alloc::string::String {}
impl<'a, T> Sealed for &'a T where T: ?Sized + Sealed {}
}
/// Used in panic messages.
struct Type<'a>(&'a Value);
-impl<'a> fmt::Display for Type<'a> {
+impl<'a> Display for Type<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
Value::Null => formatter.write_str("null"),
diff --git a/src/value/mod.rs b/src/value/mod.rs
index a28da66..3f00c95 100644
--- a/src/value/mod.rs
+++ b/src/value/mod.rs
@@ -92,7 +92,11 @@
use crate::error::Error;
use crate::io;
-use crate::lib::*;
+use alloc::string::String;
+use alloc::vec::Vec;
+use core::fmt::{self, Debug, Display};
+use core::mem;
+use core::str;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
@@ -191,7 +195,7 @@ impl Debug for Value {
}
}
-impl fmt::Display for Value {
+impl Display for Value {
/// Display a JSON value as a string.
///
/// ```
diff --git a/src/value/partial_eq.rs b/src/value/partial_eq.rs
index 354ea5a..b4ef84c 100644
--- a/src/value/partial_eq.rs
+++ b/src/value/partial_eq.rs
@@ -1,5 +1,5 @@
use super::Value;
-use crate::lib::*;
+use alloc::string::String;
fn eq_i64(value: &Value, other: i64) -> bool {
value.as_i64().map_or(false, |i| i == other)
diff --git a/src/value/ser.rs b/src/value/ser.rs
index 03cb12b..179380a 100644
--- a/src/value/ser.rs
+++ b/src/value/ser.rs
@@ -1,8 +1,12 @@
use crate::error::{Error, ErrorCode, Result};
-use crate::lib::*;
use crate::map::Map;
use crate::number::Number;
use crate::value::{to_value, Value};
+use alloc::borrow::ToOwned;
+use alloc::string::{String, ToString};
+use alloc::vec::Vec;
+use core::fmt::Display;
+use core::result;
use serde::ser::{Impossible, Serialize};
#[cfg(feature = "arbitrary_precision")]
@@ -1016,4 +1020,11 @@ impl serde::ser::Serializer for RawValueEmitter {
) -> Result<Self::SerializeStructVariant> {
Err(invalid_raw_value())
}
+
+ fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
+ where
+ T: ?Sized + Display,
+ {
+ self.serialize_str(&value.to_string())
+ }
}
diff --git a/tests/compiletest.rs b/tests/compiletest.rs
new file mode 100644
index 0000000..7974a62
--- /dev/null
+++ b/tests/compiletest.rs
@@ -0,0 +1,7 @@
+#[rustversion::attr(not(nightly), ignore)]
+#[cfg_attr(miri, ignore)]
+#[test]
+fn ui() {
+ let t = trybuild::TestCases::new();
+ t.compile_fail("tests/ui/*.rs");
+}
diff --git a/tests/debug.rs b/tests/debug.rs
new file mode 100644
index 0000000..d2d8448
--- /dev/null
+++ b/tests/debug.rs
@@ -0,0 +1,68 @@
+use serde_json::{json, Number, Value};
+
+#[test]
+fn number() {
+ assert_eq!(format!("{:?}", Number::from(1)), "Number(1)");
+ assert_eq!(format!("{:?}", Number::from(-1)), "Number(-1)");
+ assert_eq!(
+ format!("{:?}", Number::from_f64(1.0).unwrap()),
+ "Number(1.0)"
+ );
+}
+
+#[test]
+fn value_null() {
+ assert_eq!(format!("{:?}", json!(null)), "Null");
+}
+
+#[test]
+fn value_bool() {
+ assert_eq!(format!("{:?}", json!(true)), "Bool(true)");
+ assert_eq!(format!("{:?}", json!(false)), "Bool(false)");
+}
+
+#[test]
+fn value_number() {
+ assert_eq!(format!("{:?}", json!(1)), "Number(1)");
+ assert_eq!(format!("{:?}", json!(-1)), "Number(-1)");
+ assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)");
+}
+
+#[test]
+fn value_string() {
+ assert_eq!(format!("{:?}", json!("s")), "String(\"s\")");
+}
+
+#[test]
+fn value_array() {
+ assert_eq!(format!("{:?}", json!([])), "Array([])");
+}
+
+#[test]
+fn value_object() {
+ assert_eq!(format!("{:?}", json!({})), "Object({})");
+}
+
+#[test]
+fn error() {
+ let err = serde_json::from_str::<Value>("{0}").unwrap_err();
+ let expected = "Error(\"key must be a string\", line: 1, column: 2)";
+ assert_eq!(format!("{:?}", err), expected);
+}
+
+const INDENTED_EXPECTED: &str = r#"Object({
+ "array": Array([
+ Number(
+ 0,
+ ),
+ Number(
+ 1,
+ ),
+ ]),
+})"#;
+
+#[test]
+fn indented() {
+ let j = json!({ "array": [0, 1] });
+ assert_eq!(format!("{:#?}", j), INDENTED_EXPECTED);
+}
diff --git a/tests/lexical.rs b/tests/lexical.rs
new file mode 100644
index 0000000..6e0f07b
--- /dev/null
+++ b/tests/lexical.rs
@@ -0,0 +1,52 @@
+#![allow(
+ clippy::cast_lossless,
+ clippy::cast_possible_truncation,
+ clippy::cast_possible_wrap,
+ clippy::cast_precision_loss,
+ clippy::cast_sign_loss,
+ clippy::comparison_chain,
+ clippy::doc_markdown,
+ clippy::excessive_precision,
+ clippy::float_cmp,
+ clippy::if_not_else,
+ clippy::module_name_repetitions,
+ clippy::needless_late_init,
+ clippy::shadow_unrelated,
+ clippy::similar_names,
+ clippy::single_match_else,
+ clippy::too_many_lines,
+ clippy::unreadable_literal,
+ clippy::unseparated_literal_suffix,
+ clippy::wildcard_imports
+)]
+
+extern crate alloc;
+
+#[path = "../src/lexical/mod.rs"]
+mod lexical;
+
+mod lib {
+ pub use std::vec::Vec;
+ pub use std::{cmp, iter, mem, ops};
+}
+
+#[path = "lexical/algorithm.rs"]
+mod algorithm;
+
+#[path = "lexical/exponent.rs"]
+mod exponent;
+
+#[path = "lexical/float.rs"]
+mod float;
+
+#[path = "lexical/math.rs"]
+mod math;
+
+#[path = "lexical/num.rs"]
+mod num;
+
+#[path = "lexical/parse.rs"]
+mod parse;
+
+#[path = "lexical/rounding.rs"]
+mod rounding;
diff --git a/tests/lexical/algorithm.rs b/tests/lexical/algorithm.rs
new file mode 100644
index 0000000..7f3a2c6
--- /dev/null
+++ b/tests/lexical/algorithm.rs
@@ -0,0 +1,110 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::algorithm::*;
+use crate::lexical::num::Float;
+
+#[test]
+fn float_fast_path_test() {
+ // valid
+ let mantissa = (1 << f32::MANTISSA_SIZE) - 1;
+ let (min_exp, max_exp) = f32::exponent_limit();
+ for exp in min_exp..=max_exp {
+ let f = fast_path::<f32>(mantissa, exp);
+ assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
+ }
+
+ // Check slightly above valid exponents
+ let f = fast_path::<f32>(123, 15);
+ assert_eq!(f, Some(1.23e+17));
+
+ // Exponent is 1 too high, pushes over the mantissa.
+ let f = fast_path::<f32>(123, 16);
+ assert!(f.is_none());
+
+ // Mantissa is too large, checked_mul should overflow.
+ let f = fast_path::<f32>(mantissa, 11);
+ assert!(f.is_none());
+
+ // invalid exponents
+ let (min_exp, max_exp) = f32::exponent_limit();
+ let f = fast_path::<f32>(mantissa, min_exp - 1);
+ assert!(f.is_none(), "exponent under min_exp");
+
+ let f = fast_path::<f32>(mantissa, max_exp + 1);
+ assert!(f.is_none(), "exponent above max_exp");
+}
+
+#[test]
+fn double_fast_path_test() {
+ // valid
+ let mantissa = (1 << f64::MANTISSA_SIZE) - 1;
+ let (min_exp, max_exp) = f64::exponent_limit();
+ for exp in min_exp..=max_exp {
+ let f = fast_path::<f64>(mantissa, exp);
+ assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
+ }
+
+ // invalid exponents
+ let (min_exp, max_exp) = f64::exponent_limit();
+ let f = fast_path::<f64>(mantissa, min_exp - 1);
+ assert!(f.is_none(), "exponent under min_exp");
+
+ let f = fast_path::<f64>(mantissa, max_exp + 1);
+ assert!(f.is_none(), "exponent above max_exp");
+
+ assert_eq!(
+ Some(0.04628372940652459),
+ fast_path::<f64>(4628372940652459, -17)
+ );
+ assert_eq!(None, fast_path::<f64>(26383446160308229, -272));
+}
+
+#[test]
+fn moderate_path_test() {
+ let (f, valid) = moderate_path::<f64>(1234567890, -1, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.0);
+
+ let (f, valid) = moderate_path::<f64>(1234567891, -1, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1);
+
+ let (f, valid) = moderate_path::<f64>(12345678912, -2, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12);
+
+ let (f, valid) = moderate_path::<f64>(123456789123, -3, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.123);
+
+ let (f, valid) = moderate_path::<f64>(1234567891234, -4, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1234);
+
+ let (f, valid) = moderate_path::<f64>(12345678912345, -5, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12345);
+
+ let (f, valid) = moderate_path::<f64>(123456789123456, -6, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.123456);
+
+ let (f, valid) = moderate_path::<f64>(1234567891234567, -7, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1234567);
+
+ let (f, valid) = moderate_path::<f64>(12345678912345679, -8, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12345679);
+
+ let (f, valid) = moderate_path::<f64>(4628372940652459, -17, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 0.04628372940652459);
+
+ let (f, valid) = moderate_path::<f64>(26383446160308229, -272, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 2.6383446160308229e-256);
+
+ let (_, valid) = moderate_path::<f64>(26383446160308230, -272, false);
+ assert!(!valid, "should be invalid");
+}
diff --git a/tests/lexical/exponent.rs b/tests/lexical/exponent.rs
new file mode 100644
index 0000000..f7a847b
--- /dev/null
+++ b/tests/lexical/exponent.rs
@@ -0,0 +1,54 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::exponent::*;
+
+#[test]
+fn scientific_exponent_test() {
+ // 0 digits in the integer
+ assert_eq!(scientific_exponent(0, 0, 5), -6);
+ assert_eq!(scientific_exponent(10, 0, 5), 4);
+ assert_eq!(scientific_exponent(-10, 0, 5), -16);
+
+ // >0 digits in the integer
+ assert_eq!(scientific_exponent(0, 1, 5), 0);
+ assert_eq!(scientific_exponent(0, 2, 5), 1);
+ assert_eq!(scientific_exponent(0, 2, 20), 1);
+ assert_eq!(scientific_exponent(10, 2, 20), 11);
+ assert_eq!(scientific_exponent(-10, 2, 20), -9);
+
+ // Underflow
+ assert_eq!(
+ scientific_exponent(i32::min_value(), 0, 0),
+ i32::min_value()
+ );
+ assert_eq!(
+ scientific_exponent(i32::min_value(), 0, 5),
+ i32::min_value()
+ );
+
+ // Overflow
+ assert_eq!(
+ scientific_exponent(i32::max_value(), 0, 0),
+ i32::max_value() - 1
+ );
+ assert_eq!(
+ scientific_exponent(i32::max_value(), 5, 0),
+ i32::max_value()
+ );
+}
+
+#[test]
+fn mantissa_exponent_test() {
+ assert_eq!(mantissa_exponent(10, 5, 0), 5);
+ assert_eq!(mantissa_exponent(0, 5, 0), -5);
+ assert_eq!(
+ mantissa_exponent(i32::max_value(), 5, 0),
+ i32::max_value() - 5
+ );
+ assert_eq!(mantissa_exponent(i32::max_value(), 0, 5), i32::max_value());
+ assert_eq!(mantissa_exponent(i32::min_value(), 5, 0), i32::min_value());
+ assert_eq!(
+ mantissa_exponent(i32::min_value(), 0, 5),
+ i32::min_value() + 5
+ );
+}
diff --git a/tests/lexical/float.rs b/tests/lexical/float.rs
new file mode 100644
index 0000000..c87e7e1
--- /dev/null
+++ b/tests/lexical/float.rs
@@ -0,0 +1,581 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::float::ExtendedFloat;
+use crate::lexical::rounding::round_nearest_tie_even;
+use std::{f32, f64};
+
+// NORMALIZE
+
+fn check_normalize(mant: u64, exp: i32, shift: u32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ assert_eq!(x.normalize(), shift);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn normalize_test() {
+ // F32
+ // 0
+ check_normalize(0, 0, 0, 0, 0);
+
+ // min value
+ check_normalize(1, -149, 63, 9223372036854775808, -212);
+
+ // 1.0e-40
+ check_normalize(71362, -149, 47, 10043308644012916736, -196);
+
+ // 1.0e-20
+ check_normalize(12379400, -90, 40, 13611294244890214400, -130);
+
+ // 1.0
+ check_normalize(8388608, -23, 40, 9223372036854775808, -63);
+
+ // 1e20
+ check_normalize(11368684, 43, 40, 12500000250510966784, 3);
+
+ // max value
+ check_normalize(16777213, 104, 40, 18446740775174668288, 64);
+
+ // F64
+
+ // min value
+ check_normalize(1, -1074, 63, 9223372036854775808, -1137);
+
+ // 1.0e-250
+ check_normalize(6448907850777164, -883, 11, 13207363278391631872, -894);
+
+ // 1.0e-150
+ check_normalize(7371020360979573, -551, 11, 15095849699286165504, -562);
+
+ // 1.0e-45
+ check_normalize(6427752177035961, -202, 11, 13164036458569648128, -213);
+
+ // 1.0e-40
+ check_normalize(4903985730770844, -185, 11, 10043362776618688512, -196);
+
+ // 1.0e-20
+ check_normalize(6646139978924579, -119, 11, 13611294676837537792, -130);
+
+ // 1.0
+ check_normalize(4503599627370496, -52, 11, 9223372036854775808, -63);
+
+ // 1e20
+ check_normalize(6103515625000000, 14, 11, 12500000000000000000, 3);
+
+ // 1e40
+ check_normalize(8271806125530277, 80, 11, 16940658945086007296, 69);
+
+ // 1e150
+ check_normalize(5503284107318959, 446, 11, 11270725851789228032, 435);
+
+ // 1e250
+ check_normalize(6290184345309700, 778, 11, 12882297539194265600, 767);
+
+ // max value
+ check_normalize(9007199254740991, 971, 11, 18446744073709549568, 960);
+}
+
+// ROUND
+
+fn check_round_to_f32(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ x.round_to_native::<f32, _>(round_nearest_tie_even);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn round_to_f32_test() {
+ // This is lossy, so some of these values are **slightly** rounded.
+
+ // underflow
+ check_round_to_f32(9223372036854775808, -213, 0, -149);
+
+ // min value
+ check_round_to_f32(9223372036854775808, -212, 1, -149);
+
+ // 1.0e-40
+ check_round_to_f32(10043308644012916736, -196, 71362, -149);
+
+ // 1.0e-20
+ check_round_to_f32(13611294244890214400, -130, 12379400, -90);
+
+ // 1.0
+ check_round_to_f32(9223372036854775808, -63, 8388608, -23);
+
+ // 1e20
+ check_round_to_f32(12500000250510966784, 3, 11368684, 43);
+
+ // max value
+ check_round_to_f32(18446740775174668288, 64, 16777213, 104);
+
+ // overflow
+ check_round_to_f32(18446740775174668288, 65, 16777213, 105);
+}
+
+fn check_round_to_f64(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ x.round_to_native::<f64, _>(round_nearest_tie_even);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn round_to_f64_test() {
+ // This is lossy, so some of these values are **slightly** rounded.
+
+ // underflow
+ check_round_to_f64(9223372036854775808, -1138, 0, -1074);
+
+ // min value
+ check_round_to_f64(9223372036854775808, -1137, 1, -1074);
+
+ // 1.0e-250
+ check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551);
+
+ // 1.0e-150
+ check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551);
+
+ // 1.0e-45
+ check_round_to_f64(13164036458569648128, -213, 6427752177035961, -202);
+
+ // 1.0e-40
+ check_round_to_f64(10043362776618688512, -196, 4903985730770844, -185);
+
+ // 1.0e-20
+ check_round_to_f64(13611294676837537792, -130, 6646139978924579, -119);
+
+ // 1.0
+ check_round_to_f64(9223372036854775808, -63, 4503599627370496, -52);
+
+ // 1e20
+ check_round_to_f64(12500000000000000000, 3, 6103515625000000, 14);
+
+ // 1e40
+ check_round_to_f64(16940658945086007296, 69, 8271806125530277, 80);
+
+ // 1e150
+ check_round_to_f64(11270725851789228032, 435, 5503284107318959, 446);
+
+ // 1e250
+ check_round_to_f64(12882297539194265600, 767, 6290184345309700, 778);
+
+ // max value
+ check_round_to_f64(18446744073709549568, 960, 9007199254740991, 971);
+
+ // Bug fixes
+ // 1.2345e-308
+ check_round_to_f64(10234494226754558294, -1086, 2498655817078750, -1074);
+}
+
+fn assert_normalized_eq(mut x: ExtendedFloat, mut y: ExtendedFloat) {
+ x.normalize();
+ y.normalize();
+ assert_eq!(x, y);
+}
+
+#[test]
+fn from_float() {
+ let values: [f32; 26] = [
+ 1e-40, 2e-40, 1e-35, 2e-35, 1e-30, 2e-30, 1e-25, 2e-25, 1e-20, 2e-20, 1e-15, 2e-15, 1e-10,
+ 2e-10, 1e-5, 2e-5, 1.0, 2.0, 1e5, 2e5, 1e10, 2e10, 1e15, 2e15, 1e20, 2e20,
+ ];
+ for value in &values {
+ assert_normalized_eq(
+ ExtendedFloat::from_float(*value),
+ ExtendedFloat::from_float(*value as f64),
+ );
+ }
+}
+
+// TO
+
+// Sample of interesting numbers to check during standard test builds.
+const INTEGERS: [u64; 32] = [
+ 0, // 0x0
+ 1, // 0x1
+ 7, // 0x7
+ 15, // 0xF
+ 112, // 0x70
+ 119, // 0x77
+ 127, // 0x7F
+ 240, // 0xF0
+ 247, // 0xF7
+ 255, // 0xFF
+ 2032, // 0x7F0
+ 2039, // 0x7F7
+ 2047, // 0x7FF
+ 4080, // 0xFF0
+ 4087, // 0xFF7
+ 4095, // 0xFFF
+ 65520, // 0xFFF0
+ 65527, // 0xFFF7
+ 65535, // 0xFFFF
+ 1048560, // 0xFFFF0
+ 1048567, // 0xFFFF7
+ 1048575, // 0xFFFFF
+ 16777200, // 0xFFFFF0
+ 16777207, // 0xFFFFF7
+ 16777215, // 0xFFFFFF
+ 268435440, // 0xFFFFFF0
+ 268435447, // 0xFFFFFF7
+ 268435455, // 0xFFFFFFF
+ 4294967280, // 0xFFFFFFF0
+ 4294967287, // 0xFFFFFFF7
+ 4294967295, // 0xFFFFFFFF
+ 18446744073709551615, // 0xFFFFFFFFFFFFFFFF
+];
+
+#[test]
+fn to_f32_test() {
+ // underflow
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -213,
+ };
+ assert_eq!(x.into_float::<f32>(), 0.0);
+
+ // min value
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -212,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-45);
+
+ // 1.0e-40
+ let x = ExtendedFloat {
+ mant: 10043308644012916736,
+ exp: -196,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-40);
+
+ // 1.0e-20
+ let x = ExtendedFloat {
+ mant: 13611294244890214400,
+ exp: -130,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-20);
+
+ // 1.0
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -63,
+ };
+ assert_eq!(x.into_float::<f32>(), 1.0);
+
+ // 1e20
+ let x = ExtendedFloat {
+ mant: 12500000250510966784,
+ exp: 3,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e20);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 18446740775174668288,
+ exp: 64,
+ };
+ assert_eq!(x.into_float::<f32>(), 3.402823e38);
+
+ // almost max, high exp
+ let x = ExtendedFloat {
+ mant: 1048575,
+ exp: 108,
+ };
+ assert_eq!(x.into_float::<f32>(), 3.4028204e38);
+
+ // max value + 1
+ let x = ExtendedFloat {
+ mant: 16777216,
+ exp: 104,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // max value + 1
+ let x = ExtendedFloat {
+ mant: 1048576,
+ exp: 108,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // 1e40
+ let x = ExtendedFloat {
+ mant: 16940658945086007296,
+ exp: 69,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // Integers.
+ for int in &INTEGERS {
+ let fp = ExtendedFloat { mant: *int, exp: 0 };
+ assert_eq!(fp.into_float::<f32>(), *int as f32, "{:?} as f32", *int);
+ }
+}
+
+#[test]
+fn to_f64_test() {
+ // underflow
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -1138,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ // min value
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -1137,
+ };
+ assert_eq!(x.into_float::<f64>(), 5e-324);
+
+ // 1.0e-250
+ let x = ExtendedFloat {
+ mant: 13207363278391631872,
+ exp: -894,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-250);
+
+ // 1.0e-150
+ let x = ExtendedFloat {
+ mant: 15095849699286165504,
+ exp: -562,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-150);
+
+ // 1.0e-45
+ let x = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-45);
+
+ // 1.0e-40
+ let x = ExtendedFloat {
+ mant: 10043362776618688512,
+ exp: -196,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-40);
+
+ // 1.0e-20
+ let x = ExtendedFloat {
+ mant: 13611294676837537792,
+ exp: -130,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-20);
+
+ // 1.0
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -63,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.0);
+
+ // 1e20
+ let x = ExtendedFloat {
+ mant: 12500000000000000000,
+ exp: 3,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e20);
+
+ // 1e40
+ let x = ExtendedFloat {
+ mant: 16940658945086007296,
+ exp: 69,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e40);
+
+ // 1e150
+ let x = ExtendedFloat {
+ mant: 11270725851789228032,
+ exp: 435,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e150);
+
+ // 1e250
+ let x = ExtendedFloat {
+ mant: 12882297539194265600,
+ exp: 767,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e250);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 9007199254740991,
+ exp: 971,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 18446744073709549568,
+ exp: 960,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);
+
+ // overflow
+ let x = ExtendedFloat {
+ mant: 9007199254740992,
+ exp: 971,
+ };
+ assert_eq!(x.into_float::<f64>(), f64::INFINITY);
+
+ // overflow
+ let x = ExtendedFloat {
+ mant: 18446744073709549568,
+ exp: 961,
+ };
+ assert_eq!(x.into_float::<f64>(), f64::INFINITY);
+
+ // Underflow
+ // Adapted from failures in strtod.
+ let x = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709550712,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ let x = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709551460,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ let x = ExtendedFloat {
+ exp: -1138,
+ mant: 9223372036854776103,
+ };
+ assert_eq!(x.into_float::<f64>(), 5e-324);
+
+ // Integers.
+ for int in &INTEGERS {
+ let fp = ExtendedFloat { mant: *int, exp: 0 };
+ assert_eq!(fp.into_float::<f64>(), *int as f64, "{:?} as f64", *int);
+ }
+}
+
+// OPERATIONS
+
+fn check_mul(a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
+ let r = a.mul(&b);
+ assert_eq!(r, c);
+}
+
+#[test]
+fn mul_test() {
+ // Normalized (64-bit mantissa)
+ let a = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ let b = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -62,
+ };
+ let c = ExtendedFloat {
+ mant: 6582018229284824064,
+ exp: -211,
+ };
+ check_mul(a, b, c);
+
+ // Check with integers
+ // 64-bit mantissa
+ let mut a = ExtendedFloat { mant: 10, exp: 0 };
+ let mut b = ExtendedFloat { mant: 10, exp: 0 };
+ a.normalize();
+ b.normalize();
+ assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);
+
+ // Check both values need high bits set.
+ let a = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ assert_eq!(a.mul(&b).into_float::<f64>(), 4.0);
+
+ // Check both values need high bits set.
+ let a = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);
+}
+
+fn check_imul(mut a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
+ a.imul(&b);
+ assert_eq!(a, c);
+}
+
+#[test]
+fn imul_test() {
+ // Normalized (64-bit mantissa)
+ let a = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ let b = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -62,
+ };
+ let c = ExtendedFloat {
+ mant: 6582018229284824064,
+ exp: -211,
+ };
+ check_imul(a, b, c);
+
+ // Check with integers
+ // 64-bit mantissa
+ let mut a = ExtendedFloat { mant: 10, exp: 0 };
+ let mut b = ExtendedFloat { mant: 10, exp: 0 };
+ a.normalize();
+ b.normalize();
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 100.0);
+
+ // Check both values need high bits set.
+ let mut a = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 4.0);
+
+ // Check both values need high bits set.
+ let mut a = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 100.0);
+}
diff --git a/tests/lexical/math.rs b/tests/lexical/math.rs
new file mode 100644
index 0000000..79d3ef3
--- /dev/null
+++ b/tests/lexical/math.rs
@@ -0,0 +1,211 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::math::{Limb, Math};
+use std::cmp;
+
+#[derive(Clone, Default)]
+struct Bigint {
+ data: Vec<Limb>,
+}
+
+impl Math for Bigint {
+ fn data(&self) -> &Vec<Limb> {
+ &self.data
+ }
+
+ fn data_mut(&mut self) -> &mut Vec<Limb> {
+ &mut self.data
+ }
+}
+
+#[cfg(limb_width_32)]
+pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
+ x.iter().cloned().collect()
+}
+
+#[cfg(limb_width_64)]
+pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
+ let mut v = Vec::<Limb>::default();
+ for xi in x.chunks(2) {
+ match xi.len() {
+ 1 => v.push(xi[0] as u64),
+ 2 => v.push(((xi[1] as u64) << 32) | (xi[0] as u64)),
+ _ => unreachable!(),
+ }
+ }
+
+ v
+}
+
+#[test]
+fn compare_test() {
+ // Simple
+ let x = Bigint {
+ data: from_u32(&[1]),
+ };
+ let y = Bigint {
+ data: from_u32(&[2]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Less);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Greater);
+
+ // Check asymmetric
+ let x = Bigint {
+ data: from_u32(&[5, 1]),
+ };
+ let y = Bigint {
+ data: from_u32(&[2]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+
+ // Check when we use reverse ordering properly.
+ let x = Bigint {
+ data: from_u32(&[5, 1, 9]),
+ };
+ let y = Bigint {
+ data: from_u32(&[6, 2, 8]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+
+ // Complex scenario, check it properly uses reverse ordering.
+ let x = Bigint {
+ data: from_u32(&[0, 1, 9]),
+ };
+ let y = Bigint {
+ data: from_u32(&[4294967295, 0, 9]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+}
+
+#[test]
+fn hi64_test() {
+ assert_eq!(Bigint::from_u64(0xA).hi64(), (0xA000000000000000, false));
+ assert_eq!(Bigint::from_u64(0xAB).hi64(), (0xAB00000000000000, false));
+ assert_eq!(
+ Bigint::from_u64(0xAB00000000).hi64(),
+ (0xAB00000000000000, false)
+ );
+ assert_eq!(
+ Bigint::from_u64(0xA23456789A).hi64(),
+ (0xA23456789A000000, false)
+ );
+}
+
+#[test]
+fn bit_length_test() {
+ let x = Bigint {
+ data: from_u32(&[0, 0, 0, 1]),
+ };
+ assert_eq!(x.bit_length(), 97);
+
+ let x = Bigint {
+ data: from_u32(&[0, 0, 0, 3]),
+ };
+ assert_eq!(x.bit_length(), 98);
+
+ let x = Bigint {
+ data: from_u32(&[1 << 31]),
+ };
+ assert_eq!(x.bit_length(), 32);
+}
+
+#[test]
+fn iadd_small_test() {
+ // Overflow check (single)
+ // This should set all the internal data values to 0, the top
+ // value to (1<<31), and the bottom value to (4>>1).
+ // This is because the max_value + 1 leads to all 0s, we set the
+ // topmost bit to 1.
+ let mut x = Bigint {
+ data: from_u32(&[4294967295]),
+ };
+ x.iadd_small(5);
+ assert_eq!(x.data, from_u32(&[4, 1]));
+
+ // No overflow, single value
+ let mut x = Bigint {
+ data: from_u32(&[5]),
+ };
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[12]));
+
+ // Single carry, internal overflow
+ let mut x = Bigint::from_u64(0x80000000FFFFFFFF);
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[6, 0x80000001]));
+
+ // Double carry, overflow
+ let mut x = Bigint::from_u64(0xFFFFFFFFFFFFFFFF);
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[6, 0, 1]));
+}
+
+#[test]
+fn imul_small_test() {
+ // No overflow check, 1-int.
+ let mut x = Bigint {
+ data: from_u32(&[5]),
+ };
+ x.imul_small(7);
+ assert_eq!(x.data, from_u32(&[35]));
+
+ // No overflow check, 2-ints.
+ let mut x = Bigint::from_u64(0x4000000040000);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[0x00140000, 0x140000]));
+
+ // Overflow, 1 carry.
+ let mut x = Bigint {
+ data: from_u32(&[0x33333334]),
+ };
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 1]));
+
+ // Overflow, 1 carry, internal.
+ let mut x = Bigint::from_u64(0x133333334);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 6]));
+
+ // Overflow, 2 carries.
+ let mut x = Bigint::from_u64(0x3333333333333334);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 0, 1]));
+}
+
+#[test]
+fn shl_test() {
+ // Pattern generated via `''.join(["1" +"0"*i for i in range(20)])`
+ let mut big = Bigint {
+ data: from_u32(&[0xD2210408]),
+ };
+ big.ishl(5);
+ assert_eq!(big.data, from_u32(&[0x44208100, 0x1A]));
+ big.ishl(32);
+ assert_eq!(big.data, from_u32(&[0, 0x44208100, 0x1A]));
+ big.ishl(27);
+ assert_eq!(big.data, from_u32(&[0, 0, 0xD2210408]));
+
+ // 96-bits of previous pattern
+ let mut big = Bigint {
+ data: from_u32(&[0x20020010, 0x8040100, 0xD2210408]),
+ };
+ big.ishl(5);
+ assert_eq!(big.data, from_u32(&[0x400200, 0x802004, 0x44208101, 0x1A]));
+ big.ishl(32);
+ assert_eq!(
+ big.data,
+ from_u32(&[0, 0x400200, 0x802004, 0x44208101, 0x1A])
+ );
+ big.ishl(27);
+ assert_eq!(
+ big.data,
+ from_u32(&[0, 0, 0x20020010, 0x8040100, 0xD2210408])
+ );
+}
diff --git a/tests/lexical/num.rs b/tests/lexical/num.rs
new file mode 100644
index 0000000..1a94be0
--- /dev/null
+++ b/tests/lexical/num.rs
@@ -0,0 +1,76 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::num::{AsPrimitive, Float, Integer, Number};
+
+fn check_as_primitive<T: AsPrimitive>(t: T) {
+ let _: u32 = t.as_u32();
+ let _: u64 = t.as_u64();
+ let _: u128 = t.as_u128();
+ let _: usize = t.as_usize();
+ let _: f32 = t.as_f32();
+ let _: f64 = t.as_f64();
+}
+
+#[test]
+fn as_primitive_test() {
+ check_as_primitive(1u32);
+ check_as_primitive(1u64);
+ check_as_primitive(1u128);
+ check_as_primitive(1usize);
+ check_as_primitive(1f32);
+ check_as_primitive(1f64);
+}
+
+fn check_number<T: Number>(x: T, y: T) {
+ // Copy, partialeq, partialord
+ let _ = x;
+ assert!(x < y);
+ assert!(x != y);
+
+ // Operations
+ let _ = y + x;
+
+ // Conversions already tested.
+}
+
+#[test]
+fn number_test() {
+ check_number(1u32, 5);
+ check_number(1u64, 5);
+ check_number(1u128, 5);
+ check_number(1usize, 5);
+ check_number(1f32, 5.0);
+ check_number(1f64, 5.0);
+}
+
+fn check_integer<T: Integer>(x: T) {
+ // Bitwise operations
+ let _ = x & T::ZERO;
+}
+
+#[test]
+fn integer_test() {
+ check_integer(65u32);
+ check_integer(65u64);
+ check_integer(65u128);
+ check_integer(65usize);
+}
+
+fn check_float<T: Float>(x: T) {
+ // Check functions
+ let _ = x.pow10(5);
+ let _ = x.to_bits();
+ assert!(T::from_bits(x.to_bits()) == x);
+
+ // Check properties
+ let _ = x.to_bits() & T::SIGN_MASK;
+ let _ = x.to_bits() & T::EXPONENT_MASK;
+ let _ = x.to_bits() & T::HIDDEN_BIT_MASK;
+ let _ = x.to_bits() & T::MANTISSA_MASK;
+}
+
+#[test]
+fn float_test() {
+ check_float(123f32);
+ check_float(123f64);
+}
diff --git a/tests/lexical/parse.rs b/tests/lexical/parse.rs
new file mode 100644
index 0000000..80ca25e
--- /dev/null
+++ b/tests/lexical/parse.rs
@@ -0,0 +1,204 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::num::Float;
+use crate::lexical::parse::{parse_concise_float, parse_truncated_float};
+use core::f64;
+use core::fmt::Debug;
+
+fn check_concise_float<F>(mantissa: u64, exponent: i32, expected: F)
+where
+ F: Float + Debug,
+{
+ assert_eq!(parse_concise_float::<F>(mantissa, exponent), expected);
+}
+
+fn check_truncated_float<F>(integer: &str, fraction: &str, exponent: i32, expected: F)
+where
+ F: Float + Debug,
+{
+ let integer = integer.as_bytes();
+ let fraction = fraction.as_bytes();
+ assert_eq!(
+ parse_truncated_float::<F>(integer, fraction, exponent),
+ expected,
+ );
+}
+
+#[test]
+fn parse_f32_test() {
+ check_concise_float(0, 0, 0.0_f32);
+ check_concise_float(12345, -4, 1.2345_f32);
+ check_concise_float(12345, -3, 12.345_f32);
+ check_concise_float(123456789, -4, 12345.6789_f32);
+ check_concise_float(12345, 6, 1.2345e10_f32);
+ check_concise_float(12345, -42, 1.2345e-38_f32);
+
+ // Check expected rounding, using borderline cases.
+ // Round-down, halfway
+ check_concise_float(16777216, 0, 16777216.0_f32);
+ check_concise_float(16777217, 0, 16777216.0_f32);
+ check_concise_float(16777218, 0, 16777218.0_f32);
+ check_concise_float(33554432, 0, 33554432.0_f32);
+ check_concise_float(33554434, 0, 33554432.0_f32);
+ check_concise_float(33554436, 0, 33554436.0_f32);
+ check_concise_float(17179869184, 0, 17179869184.0_f32);
+ check_concise_float(17179870208, 0, 17179869184.0_f32);
+ check_concise_float(17179871232, 0, 17179871232.0_f32);
+
+ // Round-up, halfway
+ check_concise_float(16777218, 0, 16777218.0_f32);
+ check_concise_float(16777219, 0, 16777220.0_f32);
+ check_concise_float(16777220, 0, 16777220.0_f32);
+
+ check_concise_float(33554436, 0, 33554436.0_f32);
+ check_concise_float(33554438, 0, 33554440.0_f32);
+ check_concise_float(33554440, 0, 33554440.0_f32);
+
+ check_concise_float(17179871232, 0, 17179871232.0_f32);
+ check_concise_float(17179872256, 0, 17179873280.0_f32);
+ check_concise_float(17179873280, 0, 17179873280.0_f32);
+
+ // Round-up, above halfway
+ check_concise_float(33554435, 0, 33554436.0_f32);
+ check_concise_float(17179870209, 0, 17179871232.0_f32);
+
+ // Check exactly halfway, round-up at halfway
+ check_truncated_float("1", "00000017881393432617187499", 0, 1.0000001_f32);
+ check_truncated_float("1", "000000178813934326171875", 0, 1.0000002_f32);
+ check_truncated_float("1", "00000017881393432617187501", 0, 1.0000002_f32);
+}
+
+#[test]
+fn parse_f64_test() {
+ check_concise_float(0, 0, 0.0_f64);
+ check_concise_float(12345, -4, 1.2345_f64);
+ check_concise_float(12345, -3, 12.345_f64);
+ check_concise_float(123456789, -4, 12345.6789_f64);
+ check_concise_float(12345, 6, 1.2345e10_f64);
+ check_concise_float(12345, -312, 1.2345e-308_f64);
+
+ // Check expected rounding, using borderline cases.
+ // Round-down, halfway
+ check_concise_float(9007199254740992, 0, 9007199254740992.0_f64);
+ check_concise_float(9007199254740993, 0, 9007199254740992.0_f64);
+ check_concise_float(9007199254740994, 0, 9007199254740994.0_f64);
+
+ check_concise_float(18014398509481984, 0, 18014398509481984.0_f64);
+ check_concise_float(18014398509481986, 0, 18014398509481984.0_f64);
+ check_concise_float(18014398509481988, 0, 18014398509481988.0_f64);
+
+ check_concise_float(9223372036854775808, 0, 9223372036854775808.0_f64);
+ check_concise_float(9223372036854776832, 0, 9223372036854775808.0_f64);
+ check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64);
+
+ check_truncated_float(
+ "11417981541647679048466287755595961091061972992",
+ "",
+ 0,
+ 11417981541647679048466287755595961091061972992.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178368",
+ "",
+ 0,
+ 11417981541647679048466287755595961091061972992.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647681583767488212054764084468383744",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+
+ // Round-up, halfway
+ check_concise_float(9007199254740994, 0, 9007199254740994.0_f64);
+ check_concise_float(9007199254740995, 0, 9007199254740996.0_f64);
+ check_concise_float(9007199254740996, 0, 9007199254740996.0_f64);
+
+ check_concise_float(18014398509481988, 0, 18014398509481988.0_f64);
+ check_concise_float(18014398509481990, 0, 18014398509481992.0_f64);
+ check_concise_float(18014398509481992, 0, 18014398509481992.0_f64);
+
+ check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64);
+ check_concise_float(9223372036854778880, 0, 9223372036854779904.0_f64);
+ check_concise_float(9223372036854779904, 0, 9223372036854779904.0_f64);
+
+ check_truncated_float(
+ "11417981541647681583767488212054764084468383744",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647682851418088440284165581171589120",
+ "",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647684119068688668513567077874794496",
+ "",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+
+ // Round-up, above halfway
+ check_concise_float(9223372036854776833, 0, 9223372036854777856.0_f64);
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178369",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+
+ // Rounding error
+ // Adapted from failures in strtod.
+ check_concise_float(22250738585072014, -324, 2.2250738585072014e-308_f64);
+ check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187499", -308, 2.225073858507201e-308_f64);
+ check_truncated_float("2", "22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508791414915891303962110687008643869459464552765720740782062174337998814106326732925355228688137214901298112245145188984905722230728525513315575501591439747639798341180199932396254828901710708185069063066665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505108060994073026293712895895000358379996720725430436028407889577179615094551674824347103070260914462157228988025818254518032570701886087211312807951223342628836862232150377566662250398253433597456888442390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718644338377048603786162277173854562306587467901408672332763671875", -308, 2.2250738585072014e-308_f64);
+ check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187501", -308, 2.2250738585072014e-308_f64);
+ check_truncated_float("179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791", "9999999999999999999999999999999999999999999999999999999999999999999999", 0, 1.7976931348623157e+308_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984374999", -324, 5.0e-324_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375", -324, 1.0e-323_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375001", -324, 1.0e-323_f64);
+ check_truncated_float("", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125", 0, 0.0_f64);
+
+ // Rounding error
+ // Adapted from:
+ // https://www.exploringbinary.com/how-glibc-strtod-works/
+ check_truncated_float("", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0, 2.2250738585072011e-308_f64);
+
+ // Rounding error
+ // Adapted from test-parse-random failures.
+ check_concise_float(1009, -31, 1.009e-28_f64);
+ check_concise_float(18294, 304, f64::INFINITY);
+
+ // Rounding error
+ // Adapted from a @dangrabcad's issue #20.
+ check_concise_float(7689539722041643, 149, 7.689539722041643e164_f64);
+ check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", 0, 7.689539722041643e164_f64);
+ check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 7.689539722041643e164_f64);
+
+ // Check other cases similar to @dangrabcad's issue #20.
+ check_truncated_float("9223372036854776833", "0", 0, 9223372036854777856.0_f64);
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178369",
+ "0",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+ check_concise_float(90071992547409950, -1, 9007199254740996.0_f64);
+ check_concise_float(180143985094819900, -1, 18014398509481992.0_f64);
+ check_truncated_float("9223372036854778880", "0", 0, 9223372036854779904.0_f64);
+ check_truncated_float(
+ "11417981541647682851418088440284165581171589120",
+ "0",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+
+ // Check other cases ostensibly identified via proptest.
+ check_truncated_float("71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+ check_truncated_float("126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+ check_truncated_float("38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+}
diff --git a/tests/lexical/rounding.rs b/tests/lexical/rounding.rs
new file mode 100644
index 0000000..7ea1771
--- /dev/null
+++ b/tests/lexical/rounding.rs
@@ -0,0 +1,316 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::float::ExtendedFloat;
+use crate::lexical::num::Float;
+use crate::lexical::rounding::*;
+
+// MASKS
+
+#[test]
+fn lower_n_mask_test() {
+ assert_eq!(lower_n_mask(0u64), 0b0);
+ assert_eq!(lower_n_mask(1u64), 0b1);
+ assert_eq!(lower_n_mask(2u64), 0b11);
+ assert_eq!(lower_n_mask(10u64), 0b1111111111);
+ assert_eq!(lower_n_mask(32u64), 0b11111111111111111111111111111111);
+}
+
+#[test]
+fn lower_n_halfway_test() {
+ assert_eq!(lower_n_halfway(0u64), 0b0);
+ assert_eq!(lower_n_halfway(1u64), 0b1);
+ assert_eq!(lower_n_halfway(2u64), 0b10);
+ assert_eq!(lower_n_halfway(10u64), 0b1000000000);
+ assert_eq!(lower_n_halfway(32u64), 0b10000000000000000000000000000000);
+}
+
+#[test]
+fn nth_bit_test() {
+ assert_eq!(nth_bit(0u64), 0b1);
+ assert_eq!(nth_bit(1u64), 0b10);
+ assert_eq!(nth_bit(2u64), 0b100);
+ assert_eq!(nth_bit(10u64), 0b10000000000);
+ assert_eq!(nth_bit(31u64), 0b10000000000000000000000000000000);
+}
+
+#[test]
+fn internal_n_mask_test() {
+ assert_eq!(internal_n_mask(1u64, 0u64), 0b0);
+ assert_eq!(internal_n_mask(1u64, 1u64), 0b1);
+ assert_eq!(internal_n_mask(2u64, 1u64), 0b10);
+ assert_eq!(internal_n_mask(4u64, 2u64), 0b1100);
+ assert_eq!(internal_n_mask(10u64, 2u64), 0b1100000000);
+ assert_eq!(internal_n_mask(10u64, 4u64), 0b1111000000);
+ assert_eq!(
+ internal_n_mask(32u64, 4u64),
+ 0b11110000000000000000000000000000
+ );
+}
+
+// NEAREST ROUNDING
+
+#[test]
+fn round_nearest_test() {
+ // Check exactly halfway (b'1100000')
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(!above);
+ assert!(halfway);
+ assert_eq!(fp.mant, 1);
+
+ // Check above halfway (b'1100001')
+ let mut fp = ExtendedFloat { mant: 0x61, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(above);
+ assert!(!halfway);
+ assert_eq!(fp.mant, 1);
+
+ // Check below halfway (b'1011111')
+ let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(!above);
+ assert!(!halfway);
+ assert_eq!(fp.mant, 1);
+}
+
+// DIRECTED ROUNDING
+
+#[test]
+fn round_downward_test() {
+ // b0000000
+ let mut fp = ExtendedFloat { mant: 0x00, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+
+ // b1000000
+ let mut fp = ExtendedFloat { mant: 0x40, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // b1100000
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // b1110000
+ let mut fp = ExtendedFloat { mant: 0x70, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+}
+
+#[test]
+fn round_nearest_tie_even_test() {
+ // Check round-up, halfway
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 2);
+
+ // Check round-down, halfway
+ let mut fp = ExtendedFloat { mant: 0x20, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+
+ // Check round-up, above halfway
+ let mut fp = ExtendedFloat { mant: 0x61, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 2);
+
+ let mut fp = ExtendedFloat { mant: 0x21, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // Check round-down, below halfway
+ let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ let mut fp = ExtendedFloat { mant: 0x1F, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+}
+
+// HIGH-LEVEL
+
+#[test]
+fn round_to_float_test() {
+ // Denormal
+ let mut fp = ExtendedFloat {
+ mant: 1 << 63,
+ exp: f64::DENORMAL_EXPONENT - 15,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 48);
+ assert_eq!(fp.exp, f64::DENORMAL_EXPONENT);
+
+ // Halfway, round-down (b'1000000000000000000000000000000000000000000000000000010000000000')
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000400,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ // Halfway, round-up (b'1000000000000000000000000000000000000000000000000000110000000000')
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000C00,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Above halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000401,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000C01,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Below halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x80000000000003FF,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000BFF,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+}
+
+#[test]
+fn avoid_overflow_test() {
+ // Avoid overflow, fails by 1
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 5,
+ };
+ avoid_overflow::<f64>(&mut fp);
+ assert_eq!(fp.mant, 0xFFFFFFFFFFFF);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT + 5);
+
+ // Avoid overflow, succeeds
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 4,
+ };
+ avoid_overflow::<f64>(&mut fp);
+ assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT - 1);
+}
+
+#[test]
+fn round_to_native_test() {
+ // Overflow
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 4,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT - 1);
+
+ // Need denormal
+ let mut fp = ExtendedFloat {
+ mant: 1,
+ exp: f64::DENORMAL_EXPONENT + 48,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 48);
+ assert_eq!(fp.exp, f64::DENORMAL_EXPONENT);
+
+ // Halfway, round-down (b'10000000000000000000000000000000000000000000000000000100000')
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000020,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ // Halfway, round-up (b'10000000000000000000000000000000000000000000000000001100000')
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000060,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Above halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000021,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000061,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Below halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x40000000000001F,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x40000000000005F,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ // Underflow
+ // Adapted from failures in strtod.
+ let mut fp = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709550712,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0);
+ assert_eq!(fp.exp, 0);
+
+ let mut fp = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709551460,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0);
+ assert_eq!(fp.exp, 0);
+
+ let mut fp = ExtendedFloat {
+ exp: -1138,
+ mant: 9223372036854776103,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1);
+ assert_eq!(fp.exp, -1074);
+}
diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs
new file mode 100644
index 0000000..8ac4619
--- /dev/null
+++ b/tests/macros/mod.rs
@@ -0,0 +1,59 @@
+macro_rules! json_str {
+ ([]) => {
+ "[]"
+ };
+ ([ $e0:tt $(, $e:tt)* $(,)? ]) => {
+ concat!("[",
+ json_str!($e0),
+ $(",", json_str!($e),)*
+ "]")
+ };
+ ({}) => {
+ "{}"
+ };
+ ({ $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => {
+ concat!("{",
+ stringify!($k0), ":", json_str!($v0),
+ $(",", stringify!($k), ":", json_str!($v),)*
+ "}")
+ };
+ (($other:tt)) => {
+ $other
+ };
+ ($other:tt) => {
+ stringify!($other)
+ };
+}
+
+macro_rules! pretty_str {
+ ($json:tt) => {
+ pretty_str_impl!("", $json)
+ };
+}
+
+macro_rules! pretty_str_impl {
+ ($indent:expr, []) => {
+ "[]"
+ };
+ ($indent:expr, [ $e0:tt $(, $e:tt)* $(,)? ]) => {
+ concat!("[\n ",
+ $indent, pretty_str_impl!(concat!(" ", $indent), $e0),
+ $(",\n ", $indent, pretty_str_impl!(concat!(" ", $indent), $e),)*
+ "\n", $indent, "]")
+ };
+ ($indent:expr, {}) => {
+ "{}"
+ };
+ ($indent:expr, { $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => {
+ concat!("{\n ",
+ $indent, stringify!($k0), ": ", pretty_str_impl!(concat!(" ", $indent), $v0),
+ $(",\n ", $indent, stringify!($k), ": ", pretty_str_impl!(concat!(" ", $indent), $v),)*
+ "\n", $indent, "}")
+ };
+ ($indent:expr, ($other:tt)) => {
+ $other
+ };
+ ($indent:expr, $other:tt) => {
+ stringify!($other)
+ };
+}
diff --git a/tests/map.rs b/tests/map.rs
new file mode 100644
index 0000000..ae01969
--- /dev/null
+++ b/tests/map.rs
@@ -0,0 +1,47 @@
+use serde_json::{from_str, Map, Value};
+
+#[test]
+fn test_preserve_order() {
+ // Sorted order
+ #[cfg(not(feature = "preserve_order"))]
+ const EXPECTED: &[&str] = &["a", "b", "c"];
+
+ // Insertion order
+ #[cfg(feature = "preserve_order")]
+ const EXPECTED: &[&str] = &["b", "a", "c"];
+
+ let v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap();
+ let keys: Vec<_> = v.as_object().unwrap().keys().collect();
+ assert_eq!(keys, EXPECTED);
+}
+
+#[test]
+fn test_append() {
+ // Sorted order
+ #[cfg(not(feature = "preserve_order"))]
+ const EXPECTED: &[&str] = &["a", "b", "c"];
+
+ // Insertion order
+ #[cfg(feature = "preserve_order")]
+ const EXPECTED: &[&str] = &["b", "a", "c"];
+
+ let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap();
+ let val = v.as_object_mut().unwrap();
+ let mut m = Map::new();
+ m.append(val);
+ let keys: Vec<_> = m.keys().collect();
+
+ assert_eq!(keys, EXPECTED);
+ assert!(val.is_empty());
+}
+
+#[cfg(not(no_btreemap_retain))]
+#[test]
+fn test_retain() {
+ let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap();
+ let val = v.as_object_mut().unwrap();
+ val.retain(|k, _| k.as_str() != "b");
+
+ let keys: Vec<_> = val.keys().collect();
+ assert_eq!(keys, &["a", "c"]);
+}
diff --git a/tests/regression.rs b/tests/regression.rs
new file mode 100644
index 0000000..fb2b25c
--- /dev/null
+++ b/tests/regression.rs
@@ -0,0 +1,3 @@
+mod regression {
+ automod::dir!("tests/regression");
+}
diff --git a/tests/regression/issue520.rs b/tests/regression/issue520.rs
new file mode 100644
index 0000000..9ed3677
--- /dev/null
+++ b/tests/regression/issue520.rs
@@ -0,0 +1,20 @@
+#![allow(clippy::float_cmp)]
+
+use serde_derive::{Serialize, Deserialize};
+
+#[derive(Serialize, Deserialize, Debug)]
+#[serde(tag = "type", content = "data")]
+enum E {
+ Float(f32),
+}
+
+#[test]
+fn test() {
+ let e = E::Float(159.1);
+ let v = serde_json::to_value(e).unwrap();
+ let e = serde_json::from_value::<E>(v).unwrap();
+
+ match e {
+ E::Float(f) => assert_eq!(f, 159.1),
+ }
+}
diff --git a/tests/regression/issue795.rs b/tests/regression/issue795.rs
new file mode 100644
index 0000000..06b6872
--- /dev/null
+++ b/tests/regression/issue795.rs
@@ -0,0 +1,57 @@
+use serde::de::{
+ Deserialize, Deserializer, EnumAccess, IgnoredAny, MapAccess, VariantAccess, Visitor,
+};
+use serde_json::json;
+use std::fmt;
+
+#[derive(Debug)]
+pub enum Enum {
+ Variant { x: u8 },
+}
+
+impl<'de> Deserialize<'de> for Enum {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct EnumVisitor;
+
+ impl<'de> Visitor<'de> for EnumVisitor {
+ type Value = Enum;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("enum Enum")
+ }
+
+ fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ let (IgnoredAny, variant) = data.variant()?;
+ variant.struct_variant(&["x"], self)
+ }
+
+ fn visit_map<A>(self, mut data: A) -> Result<Self::Value, A::Error>
+ where
+ A: MapAccess<'de>,
+ {
+ let mut x = 0;
+ if let Some((IgnoredAny, value)) = data.next_entry()? {
+ x = value;
+ }
+ Ok(Enum::Variant { x })
+ }
+ }
+
+ deserializer.deserialize_enum("Enum", &["Variant"], EnumVisitor)
+ }
+}
+
+#[test]
+fn test() {
+ let s = r#" {"Variant":{"x":0,"y":0}} "#;
+ assert!(serde_json::from_str::<Enum>(s).is_err());
+
+ let j = json!({"Variant":{"x":0,"y":0}});
+ assert!(serde_json::from_value::<Enum>(j).is_err());
+}
diff --git a/tests/regression/issue845.rs b/tests/regression/issue845.rs
new file mode 100644
index 0000000..dcca556
--- /dev/null
+++ b/tests/regression/issue845.rs
@@ -0,0 +1,72 @@
+use serde::{Deserialize, Deserializer};
+use std::convert::TryFrom;
+use std::fmt::{self, Display};
+use std::marker::PhantomData;
+use std::str::FromStr;
+
+pub struct NumberVisitor<T> {
+ marker: PhantomData<T>,
+}
+
+impl<'de, T> serde::de::Visitor<'de> for NumberVisitor<T>
+where
+ T: TryFrom<u64> + TryFrom<i64> + FromStr,
+ <T as TryFrom<u64>>::Error: Display,
+ <T as TryFrom<i64>>::Error: Display,
+ <T as FromStr>::Err: Display,
+{
+ type Value = T;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("an integer or string")
+ }
+
+ fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ T::try_from(v).map_err(serde::de::Error::custom)
+ }
+
+ fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ T::try_from(v).map_err(serde::de::Error::custom)
+ }
+
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where
+ E: serde::de::Error,
+ {
+ v.parse().map_err(serde::de::Error::custom)
+ }
+}
+
+fn deserialize_integer_or_string<'de, D, T>(deserializer: D) -> Result<T, D::Error>
+where
+ D: Deserializer<'de>,
+ T: TryFrom<u64> + TryFrom<i64> + FromStr,
+ <T as TryFrom<u64>>::Error: Display,
+ <T as TryFrom<i64>>::Error: Display,
+ <T as FromStr>::Err: Display,
+{
+ deserializer.deserialize_any(NumberVisitor {
+ marker: PhantomData,
+ })
+}
+
+#[derive(Deserialize, Debug)]
+pub struct Struct {
+ #[serde(deserialize_with = "deserialize_integer_or_string")]
+ pub i: i64,
+}
+
+#[test]
+fn test() {
+ let j = r#" {"i":100} "#;
+ println!("{:?}", serde_json::from_str::<Struct>(j).unwrap());
+
+ let j = r#" {"i":"100"} "#;
+ println!("{:?}", serde_json::from_str::<Struct>(j).unwrap());
+}
diff --git a/tests/stream.rs b/tests/stream.rs
new file mode 100644
index 0000000..ca54e9a
--- /dev/null
+++ b/tests/stream.rs
@@ -0,0 +1,182 @@
+#![cfg(not(feature = "preserve_order"))]
+
+use serde_json::{json, Deserializer, Value};
+
+// Rustfmt issue https://github.com/rust-lang-nursery/rustfmt/issues/2740
+#[rustfmt::skip]
+macro_rules! test_stream {
+ ($data:expr, $ty:ty, |$stream:ident| $test:block) => {
+ {
+ let de = Deserializer::from_str($data);
+ let mut $stream = de.into_iter::<$ty>();
+ assert_eq!($stream.byte_offset(), 0);
+ $test
+ }
+ {
+ let de = Deserializer::from_slice($data.as_bytes());
+ let mut $stream = de.into_iter::<$ty>();
+ assert_eq!($stream.byte_offset(), 0);
+ $test
+ }
+ {
+ let mut bytes = $data.as_bytes();
+ let de = Deserializer::from_reader(&mut bytes);
+ let mut $stream = de.into_iter::<$ty>();
+ assert_eq!($stream.byte_offset(), 0);
+ $test
+ }
+ };
+}
+
+#[test]
+fn test_json_stream_newlines() {
+ let data = "{\"x\":39} {\"x\":40}{\"x\":41}\n{\"x\":42}";
+
+ test_stream!(data, Value, |stream| {
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 39);
+ assert_eq!(stream.byte_offset(), 8);
+
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 40);
+ assert_eq!(stream.byte_offset(), 17);
+
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 41);
+ assert_eq!(stream.byte_offset(), 25);
+
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 42);
+ assert_eq!(stream.byte_offset(), 34);
+
+ assert!(stream.next().is_none());
+ assert_eq!(stream.byte_offset(), 34);
+ });
+}
+
+#[test]
+fn test_json_stream_trailing_whitespaces() {
+ let data = "{\"x\":42} \t\n";
+
+ test_stream!(data, Value, |stream| {
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 42);
+ assert_eq!(stream.byte_offset(), 8);
+
+ assert!(stream.next().is_none());
+ assert_eq!(stream.byte_offset(), 11);
+ });
+}
+
+#[test]
+fn test_json_stream_truncated() {
+ let data = "{\"x\":40}\n{\"x\":";
+
+ test_stream!(data, Value, |stream| {
+ assert_eq!(stream.next().unwrap().unwrap()["x"], 40);
+ assert_eq!(stream.byte_offset(), 8);
+
+ assert!(stream.next().unwrap().unwrap_err().is_eof());
+ assert_eq!(stream.byte_offset(), 9);
+ });
+}
+
+#[test]
+fn test_json_stream_truncated_decimal() {
+ let data = "{\"x\":4.";
+
+ test_stream!(data, Value, |stream| {
+ assert!(stream.next().unwrap().unwrap_err().is_eof());
+ assert_eq!(stream.byte_offset(), 0);
+ });
+}
+
+#[test]
+fn test_json_stream_truncated_negative() {
+ let data = "{\"x\":-";
+
+ test_stream!(data, Value, |stream| {
+ assert!(stream.next().unwrap().unwrap_err().is_eof());
+ assert_eq!(stream.byte_offset(), 0);
+ });
+}
+
+#[test]
+fn test_json_stream_truncated_exponent() {
+ let data = "{\"x\":4e";
+
+ test_stream!(data, Value, |stream| {
+ assert!(stream.next().unwrap().unwrap_err().is_eof());
+ assert_eq!(stream.byte_offset(), 0);
+ });
+}
+
+#[test]
+fn test_json_stream_empty() {
+ let data = "";
+
+ test_stream!(data, Value, |stream| {
+ assert!(stream.next().is_none());
+ assert_eq!(stream.byte_offset(), 0);
+ });
+}
+
+#[test]
+fn test_json_stream_primitive() {
+ let data = "{} true{}1[]\nfalse\"hey\"2 ";
+
+ test_stream!(data, Value, |stream| {
+ assert_eq!(stream.next().unwrap().unwrap(), json!({}));
+ assert_eq!(stream.byte_offset(), 2);
+
+ assert_eq!(stream.next().unwrap().unwrap(), true);
+ assert_eq!(stream.byte_offset(), 7);
+
+ assert_eq!(stream.next().unwrap().unwrap(), json!({}));
+ assert_eq!(stream.byte_offset(), 9);
+
+ assert_eq!(stream.next().unwrap().unwrap(), 1);
+ assert_eq!(stream.byte_offset(), 10);
+
+ assert_eq!(stream.next().unwrap().unwrap(), json!([]));
+ assert_eq!(stream.byte_offset(), 12);
+
+ assert_eq!(stream.next().unwrap().unwrap(), false);
+ assert_eq!(stream.byte_offset(), 18);
+
+ assert_eq!(stream.next().unwrap().unwrap(), "hey");
+ assert_eq!(stream.byte_offset(), 23);
+
+ assert_eq!(stream.next().unwrap().unwrap(), 2);
+ assert_eq!(stream.byte_offset(), 24);
+
+ assert!(stream.next().is_none());
+ assert_eq!(stream.byte_offset(), 25);
+ });
+}
+
+#[test]
+fn test_json_stream_invalid_literal() {
+ let data = "truefalse";
+
+ test_stream!(data, Value, |stream| {
+ let second = stream.next().unwrap().unwrap_err();
+ assert_eq!(second.to_string(), "trailing characters at line 1 column 5");
+ });
+}
+
+#[test]
+fn test_json_stream_invalid_number() {
+ let data = "1true";
+
+ test_stream!(data, Value, |stream| {
+ let second = stream.next().unwrap().unwrap_err();
+ assert_eq!(second.to_string(), "trailing characters at line 1 column 2");
+ });
+}
+
+#[test]
+fn test_error() {
+ let data = "true wrong false";
+
+ test_stream!(data, Value, |stream| {
+ assert_eq!(stream.next().unwrap().unwrap(), true);
+ assert!(stream.next().unwrap().is_err());
+ assert!(stream.next().is_none());
+ });
+}
diff --git a/tests/test.rs b/tests/test.rs
new file mode 100644
index 0000000..b11635e
--- /dev/null
+++ b/tests/test.rs
@@ -0,0 +1,2386 @@
+#![cfg(not(feature = "preserve_order"))]
+#![allow(
+ clippy::cast_precision_loss,
+ clippy::excessive_precision,
+ clippy::float_cmp,
+ clippy::items_after_statements,
+ clippy::let_underscore_drop,
+ clippy::shadow_unrelated,
+ clippy::too_many_lines,
+ clippy::unreadable_literal,
+ clippy::unseparated_literal_suffix,
+ clippy::vec_init_then_push,
+ clippy::zero_sized_map_values
+)]
+#![cfg_attr(feature = "trace-macros", feature(trace_macros))]
+#[cfg(feature = "trace-macros")]
+trace_macros!(true);
+
+#[macro_use]
+mod macros;
+
+#[cfg(feature = "raw_value")]
+use ref_cast::RefCast;
+use serde::de::{self, IgnoredAny, IntoDeserializer};
+use serde::ser::{self, SerializeMap, SerializeSeq, Serializer};
+use serde::{Deserialize, Serialize};
+use serde_bytes::{ByteBuf, Bytes};
+#[cfg(feature = "raw_value")]
+use serde_json::value::RawValue;
+use serde_json::{
+ from_reader, from_slice, from_str, from_value, json, to_string, to_string_pretty, to_value,
+ to_vec, Deserializer, Number, Value,
+};
+use std::collections::hash_map::DefaultHasher;
+use std::collections::BTreeMap;
+#[cfg(feature = "raw_value")]
+use std::collections::HashMap;
+use std::fmt::{self, Debug};
+use std::hash::{Hash, Hasher};
+use std::io;
+use std::iter;
+use std::marker::PhantomData;
+use std::mem;
+use std::str::FromStr;
+use std::string::ToString;
+use std::{f32, f64};
+use std::{i16, i32, i64, i8};
+use std::{u16, u32, u64, u8};
+
+macro_rules! treemap {
+ () => {
+ BTreeMap::new()
+ };
+ ($($k:expr => $v:expr),+) => {
+ {
+ let mut m = BTreeMap::new();
+ $(
+ m.insert($k, $v);
+ )+
+ m
+ }
+ };
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+enum Animal {
+ Dog,
+ Frog(String, Vec<isize>),
+ Cat { age: usize, name: String },
+ AntHive(Vec<String>),
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct Inner {
+ a: (),
+ b: usize,
+ c: Vec<String>,
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct Outer {
+ inner: Vec<Inner>,
+}
+
+fn test_encode_ok<T>(errors: &[(T, &str)])
+where
+ T: PartialEq + Debug + ser::Serialize,
+{
+ for &(ref value, out) in errors {
+ let out = out.to_string();
+
+ let s = to_string(value).unwrap();
+ assert_eq!(s, out);
+
+ let v = to_value(&value).unwrap();
+ let s = to_string(&v).unwrap();
+ assert_eq!(s, out);
+ }
+}
+
+fn test_pretty_encode_ok<T>(errors: &[(T, &str)])
+where
+ T: PartialEq + Debug + ser::Serialize,
+{
+ for &(ref value, out) in errors {
+ let out = out.to_string();
+
+ let s = to_string_pretty(value).unwrap();
+ assert_eq!(s, out);
+
+ let v = to_value(&value).unwrap();
+ let s = to_string_pretty(&v).unwrap();
+ assert_eq!(s, out);
+ }
+}
+
+#[test]
+fn test_write_null() {
+ let tests = &[((), "null")];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_u64() {
+ let tests = &[(3u64, "3"), (u64::MAX, &u64::MAX.to_string())];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_i64() {
+ let tests = &[
+ (3i64, "3"),
+ (-2i64, "-2"),
+ (-1234i64, "-1234"),
+ (i64::MIN, &i64::MIN.to_string()),
+ ];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_f64() {
+ let tests = &[
+ (3.0, "3.0"),
+ (3.1, "3.1"),
+ (-1.5, "-1.5"),
+ (0.5, "0.5"),
+ (f64::MIN, "-1.7976931348623157e308"),
+ (f64::MAX, "1.7976931348623157e308"),
+ (f64::EPSILON, "2.220446049250313e-16"),
+ ];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_encode_nonfinite_float_yields_null() {
+ let v = to_value(::std::f64::NAN).unwrap();
+ assert!(v.is_null());
+
+ let v = to_value(::std::f64::INFINITY).unwrap();
+ assert!(v.is_null());
+
+ let v = to_value(::std::f32::NAN).unwrap();
+ assert!(v.is_null());
+
+ let v = to_value(::std::f32::INFINITY).unwrap();
+ assert!(v.is_null());
+}
+
+#[test]
+fn test_write_str() {
+ let tests = &[("", "\"\""), ("foo", "\"foo\"")];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_bool() {
+ let tests = &[(true, "true"), (false, "false")];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_char() {
+ let tests = &[
+ ('n', "\"n\""),
+ ('"', "\"\\\"\""),
+ ('\\', "\"\\\\\""),
+ ('/', "\"/\""),
+ ('\x08', "\"\\b\""),
+ ('\x0C', "\"\\f\""),
+ ('\n', "\"\\n\""),
+ ('\r', "\"\\r\""),
+ ('\t', "\"\\t\""),
+ ('\x0B', "\"\\u000b\""),
+ ('\u{3A3}', "\"\u{3A3}\""),
+ ];
+ test_encode_ok(tests);
+ test_pretty_encode_ok(tests);
+}
+
+#[test]
+fn test_write_list() {
+ test_encode_ok(&[
+ (vec![], "[]"),
+ (vec![true], "[true]"),
+ (vec![true, false], "[true,false]"),
+ ]);
+
+ test_encode_ok(&[
+ (vec![vec![], vec![], vec![]], "[[],[],[]]"),
+ (vec![vec![1, 2, 3], vec![], vec![]], "[[1,2,3],[],[]]"),
+ (vec![vec![], vec![1, 2, 3], vec![]], "[[],[1,2,3],[]]"),
+ (vec![vec![], vec![], vec![1, 2, 3]], "[[],[],[1,2,3]]"),
+ ]);
+
+ test_pretty_encode_ok(&[
+ (vec![vec![], vec![], vec![]], pretty_str!([[], [], []])),
+ (
+ vec![vec![1, 2, 3], vec![], vec![]],
+ pretty_str!([[1, 2, 3], [], []]),
+ ),
+ (
+ vec![vec![], vec![1, 2, 3], vec![]],
+ pretty_str!([[], [1, 2, 3], []]),
+ ),
+ (
+ vec![vec![], vec![], vec![1, 2, 3]],
+ pretty_str!([[], [], [1, 2, 3]]),
+ ),
+ ]);
+
+ test_pretty_encode_ok(&[
+ (vec![], "[]"),
+ (vec![true], pretty_str!([true])),
+ (vec![true, false], pretty_str!([true, false])),
+ ]);
+
+ let long_test_list = json!([false, null, ["foo\nbar", 3.5]]);
+
+ test_encode_ok(&[(
+ long_test_list.clone(),
+ json_str!([false, null, ["foo\nbar", 3.5]]),
+ )]);
+
+ test_pretty_encode_ok(&[(
+ long_test_list,
+ pretty_str!([false, null, ["foo\nbar", 3.5]]),
+ )]);
+}
+
+#[test]
+fn test_write_object() {
+ test_encode_ok(&[
+ (treemap!(), "{}"),
+ (treemap!("a".to_string() => true), "{\"a\":true}"),
+ (
+ treemap!(
+ "a".to_string() => true,
+ "b".to_string() => false
+ ),
+ "{\"a\":true,\"b\":false}",
+ ),
+ ]);
+
+ test_encode_ok(&[
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "{\"a\":{},\"b\":{},\"c\":{}}",
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "{\"a\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"b\":{},\"c\":{}}",
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "c".to_string() => treemap![]
+ ],
+ "{\"a\":{},\"b\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"c\":{}}",
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ]
+ ],
+ "{\"a\":{},\"b\":{},\"c\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}}}",
+ ),
+ ]);
+
+ test_encode_ok(&[(treemap!['c' => ()], "{\"c\":null}")]);
+
+ test_pretty_encode_ok(&[
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ pretty_str!({
+ "a": {},
+ "b": {},
+ "c": {}
+ }),
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ pretty_str!({
+ "a": {
+ "a": {
+ "a": [
+ 1,
+ 2,
+ 3
+ ]
+ },
+ "b": {},
+ "c": {}
+ },
+ "b": {},
+ "c": {}
+ }),
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ],
+ "c".to_string() => treemap![]
+ ],
+ pretty_str!({
+ "a": {},
+ "b": {
+ "a": {
+ "a": [
+ 1,
+ 2,
+ 3
+ ]
+ },
+ "b": {},
+ "c": {}
+ },
+ "c": {}
+ }),
+ ),
+ (
+ treemap![
+ "a".to_string() => treemap![],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![
+ "a".to_string() => treemap!["a" => vec![1,2,3]],
+ "b".to_string() => treemap![],
+ "c".to_string() => treemap![]
+ ]
+ ],
+ pretty_str!({
+ "a": {},
+ "b": {},
+ "c": {
+ "a": {
+ "a": [
+ 1,
+ 2,
+ 3
+ ]
+ },
+ "b": {},
+ "c": {}
+ }
+ }),
+ ),
+ ]);
+
+ test_pretty_encode_ok(&[
+ (treemap!(), "{}"),
+ (
+ treemap!("a".to_string() => true),
+ pretty_str!({
+ "a": true
+ }),
+ ),
+ (
+ treemap!(
+ "a".to_string() => true,
+ "b".to_string() => false
+ ),
+ pretty_str!( {
+ "a": true,
+ "b": false
+ }),
+ ),
+ ]);
+
+ let complex_obj = json!({
+ "b": [
+ {"c": "\x0c\x1f\r"},
+ {"d": ""}
+ ]
+ });
+
+ test_encode_ok(&[(
+ complex_obj.clone(),
+ json_str!({
+ "b": [
+ {
+ "c": (r#""\f\u001f\r""#)
+ },
+ {
+ "d": ""
+ }
+ ]
+ }),
+ )]);
+
+ test_pretty_encode_ok(&[(
+ complex_obj,
+ pretty_str!({
+ "b": [
+ {
+ "c": (r#""\f\u001f\r""#)
+ },
+ {
+ "d": ""
+ }
+ ]
+ }),
+ )]);
+}
+
+#[test]
+fn test_write_tuple() {
+ test_encode_ok(&[((5,), "[5]")]);
+
+ test_pretty_encode_ok(&[((5,), pretty_str!([5]))]);
+
+ test_encode_ok(&[((5, (6, "abc")), "[5,[6,\"abc\"]]")]);
+
+ test_pretty_encode_ok(&[((5, (6, "abc")), pretty_str!([5, [6, "abc"]]))]);
+}
+
+#[test]
+fn test_write_enum() {
+ test_encode_ok(&[
+ (Animal::Dog, "\"Dog\""),
+ (
+ Animal::Frog("Henry".to_string(), vec![]),
+ "{\"Frog\":[\"Henry\",[]]}",
+ ),
+ (
+ Animal::Frog("Henry".to_string(), vec![349]),
+ "{\"Frog\":[\"Henry\",[349]]}",
+ ),
+ (
+ Animal::Frog("Henry".to_string(), vec![349, 102]),
+ "{\"Frog\":[\"Henry\",[349,102]]}",
+ ),
+ (
+ Animal::Cat {
+ age: 5,
+ name: "Kate".to_string(),
+ },
+ "{\"Cat\":{\"age\":5,\"name\":\"Kate\"}}",
+ ),
+ (
+ Animal::AntHive(vec!["Bob".to_string(), "Stuart".to_string()]),
+ "{\"AntHive\":[\"Bob\",\"Stuart\"]}",
+ ),
+ ]);
+
+ test_pretty_encode_ok(&[
+ (Animal::Dog, "\"Dog\""),
+ (
+ Animal::Frog("Henry".to_string(), vec![]),
+ pretty_str!({
+ "Frog": [
+ "Henry",
+ []
+ ]
+ }),
+ ),
+ (
+ Animal::Frog("Henry".to_string(), vec![349]),
+ pretty_str!({
+ "Frog": [
+ "Henry",
+ [
+ 349
+ ]
+ ]
+ }),
+ ),
+ (
+ Animal::Frog("Henry".to_string(), vec![349, 102]),
+ pretty_str!({
+ "Frog": [
+ "Henry",
+ [
+ 349,
+ 102
+ ]
+ ]
+ }),
+ ),
+ ]);
+}
+
+#[test]
+fn test_write_option() {
+ test_encode_ok(&[(None, "null"), (Some("jodhpurs"), "\"jodhpurs\"")]);
+
+ test_encode_ok(&[
+ (None, "null"),
+ (Some(vec!["foo", "bar"]), "[\"foo\",\"bar\"]"),
+ ]);
+
+ test_pretty_encode_ok(&[(None, "null"), (Some("jodhpurs"), "\"jodhpurs\"")]);
+
+ test_pretty_encode_ok(&[
+ (None, "null"),
+ (Some(vec!["foo", "bar"]), pretty_str!(["foo", "bar"])),
+ ]);
+}
+
+#[test]
+fn test_write_newtype_struct() {
+ #[derive(Serialize, PartialEq, Debug)]
+ struct Newtype(BTreeMap<String, i32>);
+
+ let inner = Newtype(treemap!(String::from("inner") => 123));
+ let outer = treemap!(String::from("outer") => to_value(&inner).unwrap());
+
+ test_encode_ok(&[(inner, r#"{"inner":123}"#)]);
+
+ test_encode_ok(&[(outer, r#"{"outer":{"inner":123}}"#)]);
+}
+
+#[test]
+fn test_deserialize_number_to_untagged_enum() {
+ #[derive(Eq, PartialEq, Deserialize, Debug)]
+ #[serde(untagged)]
+ enum E {
+ N(i64),
+ }
+
+ assert_eq!(E::N(0), E::deserialize(Number::from(0)).unwrap());
+}
+
+fn test_parse_ok<T>(tests: Vec<(&str, T)>)
+where
+ T: Clone + Debug + PartialEq + ser::Serialize + de::DeserializeOwned,
+{
+ for (s, value) in tests {
+ let v: T = from_str(s).unwrap();
+ assert_eq!(v, value.clone());
+
+ let v: T = from_slice(s.as_bytes()).unwrap();
+ assert_eq!(v, value.clone());
+
+ // Make sure we can deserialize into a `Value`.
+ let json_value: Value = from_str(s).unwrap();
+ assert_eq!(json_value, to_value(&value).unwrap());
+
+ // Make sure we can deserialize from a `&Value`.
+ let v = T::deserialize(&json_value).unwrap();
+ assert_eq!(v, value);
+
+ // Make sure we can deserialize from a `Value`.
+ let v: T = from_value(json_value.clone()).unwrap();
+ assert_eq!(v, value);
+
+ // Make sure we can round trip back to `Value`.
+ let json_value2: Value = from_value(json_value.clone()).unwrap();
+ assert_eq!(json_value2, json_value);
+
+ // Make sure we can fully ignore.
+ let twoline = s.to_owned() + "\n3735928559";
+ let mut de = Deserializer::from_str(&twoline);
+ IgnoredAny::deserialize(&mut de).unwrap();
+ assert_eq!(0xDEAD_BEEF, u64::deserialize(&mut de).unwrap());
+
+ // Make sure every prefix is an EOF error, except that a prefix of a
+ // number may be a valid number.
+ if !json_value.is_number() {
+ for (i, _) in s.trim_end().char_indices() {
+ assert!(from_str::<Value>(&s[..i]).unwrap_err().is_eof());
+ assert!(from_str::<IgnoredAny>(&s[..i]).unwrap_err().is_eof());
+ }
+ }
+ }
+}
+
+// For testing representations that the deserializer accepts but the serializer
+// never generates. These do not survive a round-trip through Value.
+fn test_parse_unusual_ok<T>(tests: Vec<(&str, T)>)
+where
+ T: Clone + Debug + PartialEq + ser::Serialize + de::DeserializeOwned,
+{
+ for (s, value) in tests {
+ let v: T = from_str(s).unwrap();
+ assert_eq!(v, value.clone());
+
+ let v: T = from_slice(s.as_bytes()).unwrap();
+ assert_eq!(v, value.clone());
+ }
+}
+
+macro_rules! test_parse_err {
+ ($name:ident::<$($ty:ty),*>($arg:expr) => $expected:expr) => {
+ let actual = $name::<$($ty),*>($arg).unwrap_err().to_string();
+ assert_eq!(actual, $expected, "unexpected {} error", stringify!($name));
+ };
+}
+
+fn test_parse_err<T>(errors: &[(&str, &'static str)])
+where
+ T: Debug + PartialEq + de::DeserializeOwned,
+{
+ for &(s, err) in errors {
+ test_parse_err!(from_str::<T>(s) => err);
+ test_parse_err!(from_slice::<T>(s.as_bytes()) => err);
+ }
+}
+
+fn test_parse_slice_err<T>(errors: &[(&[u8], &'static str)])
+where
+ T: Debug + PartialEq + de::DeserializeOwned,
+{
+ for &(s, err) in errors {
+ test_parse_err!(from_slice::<T>(s) => err);
+ }
+}
+
+fn test_fromstr_parse_err<T>(errors: &[(&str, &'static str)])
+where
+ T: Debug + PartialEq + FromStr,
+ <T as FromStr>::Err: ToString,
+{
+ for &(s, err) in errors {
+ let actual = s.parse::<T>().unwrap_err().to_string();
+ assert_eq!(actual, err, "unexpected parsing error");
+ }
+}
+
+#[test]
+fn test_parse_null() {
+ test_parse_err::<()>(&[
+ ("n", "EOF while parsing a value at line 1 column 1"),
+ ("nul", "EOF while parsing a value at line 1 column 3"),
+ ("nulla", "trailing characters at line 1 column 5"),
+ ]);
+
+ test_parse_ok(vec![("null", ())]);
+}
+
+#[test]
+fn test_parse_bool() {
+ test_parse_err::<bool>(&[
+ ("t", "EOF while parsing a value at line 1 column 1"),
+ ("truz", "expected ident at line 1 column 4"),
+ ("f", "EOF while parsing a value at line 1 column 1"),
+ ("faz", "expected ident at line 1 column 3"),
+ ("truea", "trailing characters at line 1 column 5"),
+ ("falsea", "trailing characters at line 1 column 6"),
+ ]);
+
+ test_parse_ok(vec![
+ ("true", true),
+ (" true ", true),
+ ("false", false),
+ (" false ", false),
+ ]);
+}
+
+#[test]
+fn test_parse_char() {
+ test_parse_err::<char>(&[
+ (
+ "\"ab\"",
+ "invalid value: string \"ab\", expected a character at line 1 column 4",
+ ),
+ (
+ "10",
+ "invalid type: integer `10`, expected a character at line 1 column 2",
+ ),
+ ]);
+
+ test_parse_ok(vec![
+ ("\"n\"", 'n'),
+ ("\"\\\"\"", '"'),
+ ("\"\\\\\"", '\\'),
+ ("\"/\"", '/'),
+ ("\"\\b\"", '\x08'),
+ ("\"\\f\"", '\x0C'),
+ ("\"\\n\"", '\n'),
+ ("\"\\r\"", '\r'),
+ ("\"\\t\"", '\t'),
+ ("\"\\u000b\"", '\x0B'),
+ ("\"\\u000B\"", '\x0B'),
+ ("\"\u{3A3}\"", '\u{3A3}'),
+ ]);
+}
+
+#[test]
+fn test_parse_number_errors() {
+ test_parse_err::<f64>(&[
+ ("+", "expected value at line 1 column 1"),
+ (".", "expected value at line 1 column 1"),
+ ("-", "EOF while parsing a value at line 1 column 1"),
+ ("00", "invalid number at line 1 column 2"),
+ ("0x80", "trailing characters at line 1 column 2"),
+ ("\\0", "expected value at line 1 column 1"),
+ (".0", "expected value at line 1 column 1"),
+ ("0.", "EOF while parsing a value at line 1 column 2"),
+ ("1.", "EOF while parsing a value at line 1 column 2"),
+ ("1.a", "invalid number at line 1 column 3"),
+ ("1.e1", "invalid number at line 1 column 3"),
+ ("1e", "EOF while parsing a value at line 1 column 2"),
+ ("1e+", "EOF while parsing a value at line 1 column 3"),
+ ("1a", "trailing characters at line 1 column 2"),
+ (
+ "100e777777777777777777777777777",
+ "number out of range at line 1 column 14",
+ ),
+ (
+ "-100e777777777777777777777777777",
+ "number out of range at line 1 column 15",
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000", // 1e309
+ "number out of range at line 1 column 310",
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ .0e9", // 1e309
+ "number out of range at line 1 column 305",
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ e9", // 1e309
+ "number out of range at line 1 column 303",
+ ),
+ ]);
+}
+
+#[test]
+fn test_parse_i64() {
+ test_parse_ok(vec![
+ ("-2", -2),
+ ("-1234", -1234),
+ (" -1234 ", -1234),
+ (&i64::MIN.to_string(), i64::MIN),
+ (&i64::MAX.to_string(), i64::MAX),
+ ]);
+}
+
+#[test]
+fn test_parse_u64() {
+ test_parse_ok(vec![
+ ("0", 0u64),
+ ("3", 3u64),
+ ("1234", 1234),
+ (&u64::MAX.to_string(), u64::MAX),
+ ]);
+}
+
+#[test]
+fn test_parse_negative_zero() {
+ for negative_zero in &[
+ "-0",
+ "-0.0",
+ "-0e2",
+ "-0.0e2",
+ "-1e-400",
+ "-1e-4000000000000000000000000000000000000000000000000",
+ ] {
+ assert!(
+ from_str::<f32>(negative_zero).unwrap().is_sign_negative(),
+ "should have been negative: {:?}",
+ negative_zero,
+ );
+ assert!(
+ from_str::<f64>(negative_zero).unwrap().is_sign_negative(),
+ "should have been negative: {:?}",
+ negative_zero,
+ );
+ }
+}
+
+#[test]
+fn test_parse_f64() {
+ test_parse_ok(vec![
+ ("0.0", 0.0f64),
+ ("3.0", 3.0f64),
+ ("3.1", 3.1),
+ ("-1.2", -1.2),
+ ("0.4", 0.4),
+ // Edge case from:
+ // https://github.com/serde-rs/json/issues/536#issuecomment-583714900
+ ("2.638344616030823e-256", 2.638344616030823e-256),
+ ]);
+
+ #[cfg(not(feature = "arbitrary_precision"))]
+ test_parse_ok(vec![
+ // With arbitrary-precision enabled, this parses as Number{"3.00"}
+ // but the float is Number{"3.0"}
+ ("3.00", 3.0f64),
+ ("0.4e5", 0.4e5),
+ ("0.4e+5", 0.4e5),
+ ("0.4e15", 0.4e15),
+ ("0.4e+15", 0.4e15),
+ ("0.4e-01", 0.4e-1),
+ (" 0.4e-01 ", 0.4e-1),
+ ("0.4e-001", 0.4e-1),
+ ("0.4e-0", 0.4e0),
+ ("0.00e00", 0.0),
+ ("0.00e+00", 0.0),
+ ("0.00e-00", 0.0),
+ ("3.5E-2147483647", 0.0),
+ ("0.0100000000000000000001", 0.01),
+ (
+ &format!("{}", (i64::MIN as f64) - 1.0),
+ (i64::MIN as f64) - 1.0,
+ ),
+ (
+ &format!("{}", (u64::MAX as f64) + 1.0),
+ (u64::MAX as f64) + 1.0,
+ ),
+ (&format!("{}", f64::EPSILON), f64::EPSILON),
+ (
+ "0.0000000000000000000000000000000000000000000000000123e50",
+ 1.23,
+ ),
+ ("100e-777777777777777777777777777", 0.0),
+ (
+ "1010101010101010101010101010101010101010",
+ 10101010101010101010e20,
+ ),
+ (
+ "0.1010101010101010101010101010101010101010",
+ 0.1010101010101010101,
+ ),
+ ("0e1000000000000000000000000000000000000000000000", 0.0),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 00000000",
+ 1e308,
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ .0e8",
+ 1e308,
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ e8",
+ 1e308,
+ ),
+ (
+ "1000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000000000000000000000000000000000000000000000\
+ 000000000000000000e-10",
+ 1e308,
+ ),
+ ]);
+}
+
+#[test]
+fn test_value_as_f64() {
+ let v = serde_json::from_str::<Value>("1e1000");
+
+ #[cfg(not(feature = "arbitrary_precision"))]
+ assert!(v.is_err());
+
+ #[cfg(feature = "arbitrary_precision")]
+ assert_eq!(v.unwrap().as_f64(), None);
+}
+
+// Test roundtrip with some values that were not perfectly roundtripped by the
+// old f64 deserializer.
+#[cfg(feature = "float_roundtrip")]
+#[test]
+fn test_roundtrip_f64() {
+ for &float in &[
+ // Samples from quickcheck-ing roundtrip with `input: f64`. Comments
+ // indicate the value returned by the old deserializer.
+ 51.24817837550540_4, // 51.2481783755054_1
+ -93.3113703768803_3, // -93.3113703768803_2
+ -36.5739948427534_36, // -36.5739948427534_4
+ 52.31400820410624_4, // 52.31400820410624_
+ 97.4536532003468_5, // 97.4536532003468_4
+ // Samples from `rng.next_u64` + `f64::from_bits` + `is_finite` filter.
+ 2.0030397744267762e-253,
+ 7.101215824554616e260,
+ 1.769268377902049e74,
+ -1.6727517818542075e58,
+ 3.9287532173373315e299,
+ ] {
+ let json = serde_json::to_string(&float).unwrap();
+ let output: f64 = serde_json::from_str(&json).unwrap();
+ assert_eq!(float, output);
+ }
+}
+
+#[test]
+fn test_roundtrip_f32() {
+ // This number has 1 ULP error if parsed via f64 and converted to f32.
+ // https://github.com/serde-rs/json/pull/671#issuecomment-628534468
+ let float = 7.038531e-26;
+ let json = serde_json::to_string(&float).unwrap();
+ let output: f32 = serde_json::from_str(&json).unwrap();
+ assert_eq!(float, output);
+}
+
+#[test]
+fn test_serialize_char() {
+ let value = json!(
+ ({
+ let mut map = BTreeMap::new();
+ map.insert('c', ());
+ map
+ })
+ );
+ assert_eq!(&Value::Null, value.get("c").unwrap());
+}
+
+#[cfg(feature = "arbitrary_precision")]
+#[test]
+fn test_malicious_number() {
+ #[derive(Serialize)]
+ #[serde(rename = "$serde_json::private::Number")]
+ struct S {
+ #[serde(rename = "$serde_json::private::Number")]
+ f: &'static str,
+ }
+
+ let actual = serde_json::to_value(&S { f: "not a number" })
+ .unwrap_err()
+ .to_string();
+ assert_eq!(actual, "invalid number at line 1 column 1");
+}
+
+#[test]
+fn test_parse_number() {
+ test_parse_ok(vec![
+ ("0.0", Number::from_f64(0.0f64).unwrap()),
+ ("3.0", Number::from_f64(3.0f64).unwrap()),
+ ("3.1", Number::from_f64(3.1).unwrap()),
+ ("-1.2", Number::from_f64(-1.2).unwrap()),
+ ("0.4", Number::from_f64(0.4).unwrap()),
+ ]);
+
+ test_fromstr_parse_err::<Number>(&[
+ (" 1.0", "invalid number at line 1 column 1"),
+ ("1.0 ", "invalid number at line 1 column 4"),
+ ("\t1.0", "invalid number at line 1 column 1"),
+ ("1.0\t", "invalid number at line 1 column 4"),
+ ]);
+
+ #[cfg(feature = "arbitrary_precision")]
+ test_parse_ok(vec![
+ ("1e999", Number::from_string_unchecked("1e999".to_owned())),
+ ("1e+999", Number::from_string_unchecked("1e+999".to_owned())),
+ ("-1e999", Number::from_string_unchecked("-1e999".to_owned())),
+ ("1e-999", Number::from_string_unchecked("1e-999".to_owned())),
+ ("1E999", Number::from_string_unchecked("1E999".to_owned())),
+ ("1E+999", Number::from_string_unchecked("1E+999".to_owned())),
+ ("-1E999", Number::from_string_unchecked("-1E999".to_owned())),
+ ("1E-999", Number::from_string_unchecked("1E-999".to_owned())),
+ ("1E+000", Number::from_string_unchecked("1E+000".to_owned())),
+ (
+ "2.3e999",
+ Number::from_string_unchecked("2.3e999".to_owned()),
+ ),
+ (
+ "-2.3e999",
+ Number::from_string_unchecked("-2.3e999".to_owned()),
+ ),
+ ]);
+}
+
+#[test]
+fn test_parse_string() {
+ test_parse_err::<String>(&[
+ ("\"", "EOF while parsing a string at line 1 column 1"),
+ ("\"lol", "EOF while parsing a string at line 1 column 4"),
+ ("\"lol\"a", "trailing characters at line 1 column 6"),
+ (
+ "\"\\uD83C\\uFFFF\"",
+ "lone leading surrogate in hex escape at line 1 column 13",
+ ),
+ (
+ "\"\n\"",
+ "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0",
+ ),
+ (
+ "\"\x1F\"",
+ "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2",
+ ),
+ ]);
+
+ test_parse_slice_err::<String>(&[
+ (
+ &[b'"', 159, 146, 150, b'"'],
+ "invalid unicode code point at line 1 column 5",
+ ),
+ (
+ &[b'"', b'\\', b'n', 159, 146, 150, b'"'],
+ "invalid unicode code point at line 1 column 7",
+ ),
+ (
+ &[b'"', b'\\', b'u', 48, 48, 51],
+ "EOF while parsing a string at line 1 column 6",
+ ),
+ (
+ &[b'"', b'\\', b'u', 250, 48, 51, 48, b'"'],
+ "invalid escape at line 1 column 4",
+ ),
+ (
+ &[b'"', b'\\', b'u', 48, 250, 51, 48, b'"'],
+ "invalid escape at line 1 column 5",
+ ),
+ (
+ &[b'"', b'\\', b'u', 48, 48, 250, 48, b'"'],
+ "invalid escape at line 1 column 6",
+ ),
+ (
+ &[b'"', b'\\', b'u', 48, 48, 51, 250, b'"'],
+ "invalid escape at line 1 column 7",
+ ),
+ (
+ &[b'"', b'\n', b'"'],
+ "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0",
+ ),
+ (
+ &[b'"', b'\x1F', b'"'],
+ "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2",
+ ),
+ ]);
+
+ test_parse_ok(vec![
+ ("\"\"", "".to_string()),
+ ("\"foo\"", "foo".to_string()),
+ (" \"foo\" ", "foo".to_string()),
+ ("\"\\\"\"", "\"".to_string()),
+ ("\"\\b\"", "\x08".to_string()),
+ ("\"\\n\"", "\n".to_string()),
+ ("\"\\r\"", "\r".to_string()),
+ ("\"\\t\"", "\t".to_string()),
+ ("\"\\u12ab\"", "\u{12ab}".to_string()),
+ ("\"\\uAB12\"", "\u{AB12}".to_string()),
+ ("\"\\uD83C\\uDF95\"", "\u{1F395}".to_string()),
+ ]);
+}
+
+#[test]
+fn test_parse_list() {
+ test_parse_err::<Vec<f64>>(&[
+ ("[", "EOF while parsing a list at line 1 column 1"),
+ ("[ ", "EOF while parsing a list at line 1 column 2"),
+ ("[1", "EOF while parsing a list at line 1 column 2"),
+ ("[1,", "EOF while parsing a value at line 1 column 3"),
+ ("[1,]", "trailing comma at line 1 column 4"),
+ ("[1 2]", "expected `,` or `]` at line 1 column 4"),
+ ("[]a", "trailing characters at line 1 column 3"),
+ ]);
+
+ test_parse_ok(vec![
+ ("[]", vec![]),
+ ("[ ]", vec![]),
+ ("[null]", vec![()]),
+ (" [ null ] ", vec![()]),
+ ]);
+
+ test_parse_ok(vec![("[true]", vec![true])]);
+
+ test_parse_ok(vec![("[3,1]", vec![3u64, 1]), (" [ 3 , 1 ] ", vec![3, 1])]);
+
+ test_parse_ok(vec![("[[3], [1, 2]]", vec![vec![3u64], vec![1, 2]])]);
+
+ test_parse_ok(vec![("[1]", (1u64,))]);
+
+ test_parse_ok(vec![("[1, 2]", (1u64, 2u64))]);
+
+ test_parse_ok(vec![("[1, 2, 3]", (1u64, 2u64, 3u64))]);
+
+ test_parse_ok(vec![("[1, [2, 3]]", (1u64, (2u64, 3u64)))]);
+}
+
+#[test]
+fn test_parse_object() {
+ test_parse_err::<BTreeMap<String, u32>>(&[
+ ("{", "EOF while parsing an object at line 1 column 1"),
+ ("{ ", "EOF while parsing an object at line 1 column 2"),
+ ("{1", "key must be a string at line 1 column 2"),
+ ("{ \"a\"", "EOF while parsing an object at line 1 column 5"),
+ ("{\"a\"", "EOF while parsing an object at line 1 column 4"),
+ ("{\"a\" ", "EOF while parsing an object at line 1 column 5"),
+ ("{\"a\" 1", "expected `:` at line 1 column 6"),
+ ("{\"a\":", "EOF while parsing a value at line 1 column 5"),
+ ("{\"a\":1", "EOF while parsing an object at line 1 column 6"),
+ ("{\"a\":1 1", "expected `,` or `}` at line 1 column 8"),
+ ("{\"a\":1,", "EOF while parsing a value at line 1 column 7"),
+ ("{}a", "trailing characters at line 1 column 3"),
+ ]);
+
+ test_parse_ok(vec![
+ ("{}", treemap!()),
+ ("{ }", treemap!()),
+ ("{\"a\":3}", treemap!("a".to_string() => 3u64)),
+ ("{ \"a\" : 3 }", treemap!("a".to_string() => 3)),
+ (
+ "{\"a\":3,\"b\":4}",
+ treemap!("a".to_string() => 3, "b".to_string() => 4),
+ ),
+ (
+ " { \"a\" : 3 , \"b\" : 4 } ",
+ treemap!("a".to_string() => 3, "b".to_string() => 4),
+ ),
+ ]);
+
+ test_parse_ok(vec![(
+ "{\"a\": {\"b\": 3, \"c\": 4}}",
+ treemap!(
+ "a".to_string() => treemap!(
+ "b".to_string() => 3u64,
+ "c".to_string() => 4
+ )
+ ),
+ )]);
+
+ test_parse_ok(vec![("{\"c\":null}", treemap!('c' => ()))]);
+}
+
+#[test]
+fn test_parse_struct() {
+ test_parse_err::<Outer>(&[
+ (
+ "5",
+ "invalid type: integer `5`, expected struct Outer at line 1 column 1",
+ ),
+ (
+ "\"hello\"",
+ "invalid type: string \"hello\", expected struct Outer at line 1 column 7",
+ ),
+ (
+ "{\"inner\": true}",
+ "invalid type: boolean `true`, expected a sequence at line 1 column 14",
+ ),
+ ("{}", "missing field `inner` at line 1 column 2"),
+ (
+ r#"{"inner": [{"b": 42, "c": []}]}"#,
+ "missing field `a` at line 1 column 29",
+ ),
+ ]);
+
+ test_parse_ok(vec![
+ (
+ "{
+ \"inner\": []
+ }",
+ Outer { inner: vec![] },
+ ),
+ (
+ "{
+ \"inner\": [
+ { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] }
+ ]
+ }",
+ Outer {
+ inner: vec![Inner {
+ a: (),
+ b: 2,
+ c: vec!["abc".to_string(), "xyz".to_string()],
+ }],
+ },
+ ),
+ ]);
+
+ let v: Outer = from_str(
+ "[
+ [
+ [ null, 2, [\"abc\", \"xyz\"] ]
+ ]
+ ]",
+ )
+ .unwrap();
+
+ assert_eq!(
+ v,
+ Outer {
+ inner: vec![Inner {
+ a: (),
+ b: 2,
+ c: vec!["abc".to_string(), "xyz".to_string()],
+ }],
+ }
+ );
+
+ let j = json!([null, 2, []]);
+ Inner::deserialize(&j).unwrap();
+ Inner::deserialize(j).unwrap();
+}
+
+#[test]
+fn test_parse_option() {
+ test_parse_ok(vec![
+ ("null", None::<String>),
+ ("\"jodhpurs\"", Some("jodhpurs".to_string())),
+ ]);
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ struct Foo {
+ x: Option<isize>,
+ }
+
+ let value: Foo = from_str("{}").unwrap();
+ assert_eq!(value, Foo { x: None });
+
+ test_parse_ok(vec![
+ ("{\"x\": null}", Foo { x: None }),
+ ("{\"x\": 5}", Foo { x: Some(5) }),
+ ]);
+}
+
+#[test]
+fn test_parse_enum_errors() {
+ test_parse_err::<Animal>(
+ &[
+ ("{}", "expected value at line 1 column 2"),
+ ("[]", "expected value at line 1 column 1"),
+ ("\"unknown\"",
+ "unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 9"),
+ ("{\"unknown\":null}",
+ "unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 10"),
+ ("{\"Dog\":", "EOF while parsing a value at line 1 column 7"),
+ ("{\"Dog\":}", "expected value at line 1 column 8"),
+ ("{\"Dog\":{}}", "invalid type: map, expected unit at line 1 column 7"),
+ ("\"Frog\"", "invalid type: unit variant, expected tuple variant"),
+ ("\"Frog\" 0 ", "invalid type: unit variant, expected tuple variant"),
+ ("{\"Frog\":{}}",
+ "invalid type: map, expected tuple variant Animal::Frog at line 1 column 8"),
+ ("{\"Cat\":[]}", "invalid length 0, expected struct variant Animal::Cat with 2 elements at line 1 column 9"),
+ ("{\"Cat\":[0]}", "invalid length 1, expected struct variant Animal::Cat with 2 elements at line 1 column 10"),
+ ("{\"Cat\":[0, \"\", 2]}", "trailing characters at line 1 column 16"),
+ ("{\"Cat\":{\"age\": 5, \"name\": \"Kate\", \"foo\":\"bar\"}",
+ "unknown field `foo`, expected `age` or `name` at line 1 column 39"),
+
+ // JSON does not allow trailing commas in data structures
+ ("{\"Cat\":[0, \"Kate\",]}", "trailing comma at line 1 column 19"),
+ ("{\"Cat\":{\"age\": 2, \"name\": \"Kate\",}}",
+ "trailing comma at line 1 column 34"),
+ ],
+ );
+}
+
+#[test]
+fn test_parse_enum() {
+ test_parse_ok(vec![
+ ("\"Dog\"", Animal::Dog),
+ (" \"Dog\" ", Animal::Dog),
+ (
+ "{\"Frog\":[\"Henry\",[]]}",
+ Animal::Frog("Henry".to_string(), vec![]),
+ ),
+ (
+ " { \"Frog\": [ \"Henry\" , [ 349, 102 ] ] } ",
+ Animal::Frog("Henry".to_string(), vec![349, 102]),
+ ),
+ (
+ "{\"Cat\": {\"age\": 5, \"name\": \"Kate\"}}",
+ Animal::Cat {
+ age: 5,
+ name: "Kate".to_string(),
+ },
+ ),
+ (
+ " { \"Cat\" : { \"age\" : 5 , \"name\" : \"Kate\" } } ",
+ Animal::Cat {
+ age: 5,
+ name: "Kate".to_string(),
+ },
+ ),
+ (
+ " { \"AntHive\" : [\"Bob\", \"Stuart\"] } ",
+ Animal::AntHive(vec!["Bob".to_string(), "Stuart".to_string()]),
+ ),
+ ]);
+
+ test_parse_unusual_ok(vec![
+ ("{\"Dog\":null}", Animal::Dog),
+ (" { \"Dog\" : null } ", Animal::Dog),
+ ]);
+
+ test_parse_ok(vec![(
+ concat!(
+ "{",
+ " \"a\": \"Dog\",",
+ " \"b\": {\"Frog\":[\"Henry\", []]}",
+ "}"
+ ),
+ treemap!(
+ "a".to_string() => Animal::Dog,
+ "b".to_string() => Animal::Frog("Henry".to_string(), vec![])
+ ),
+ )]);
+}
+
+#[test]
+fn test_parse_trailing_whitespace() {
+ test_parse_ok(vec![
+ ("[1, 2] ", vec![1u64, 2]),
+ ("[1, 2]\n", vec![1, 2]),
+ ("[1, 2]\t", vec![1, 2]),
+ ("[1, 2]\t \n", vec![1, 2]),
+ ]);
+}
+
+#[test]
+fn test_multiline_errors() {
+ test_parse_err::<BTreeMap<String, String>>(&[(
+ "{\n \"foo\":\n \"bar\"",
+ "EOF while parsing an object at line 3 column 6",
+ )]);
+}
+
+#[test]
+fn test_missing_option_field() {
+ #[derive(Debug, PartialEq, Deserialize)]
+ struct Foo {
+ x: Option<u32>,
+ }
+
+ let value: Foo = from_str("{}").unwrap();
+ assert_eq!(value, Foo { x: None });
+
+ let value: Foo = from_str("{\"x\": 5}").unwrap();
+ assert_eq!(value, Foo { x: Some(5) });
+
+ let value: Foo = from_value(json!({})).unwrap();
+ assert_eq!(value, Foo { x: None });
+
+ let value: Foo = from_value(json!({"x": 5})).unwrap();
+ assert_eq!(value, Foo { x: Some(5) });
+}
+
+#[test]
+fn test_missing_nonoption_field() {
+ #[derive(Debug, PartialEq, Deserialize)]
+ struct Foo {
+ x: u32,
+ }
+
+ test_parse_err::<Foo>(&[("{}", "missing field `x` at line 1 column 2")]);
+}
+
+#[test]
+fn test_missing_renamed_field() {
+ #[derive(Debug, PartialEq, Deserialize)]
+ struct Foo {
+ #[serde(rename = "y")]
+ x: Option<u32>,
+ }
+
+ let value: Foo = from_str("{}").unwrap();
+ assert_eq!(value, Foo { x: None });
+
+ let value: Foo = from_str("{\"y\": 5}").unwrap();
+ assert_eq!(value, Foo { x: Some(5) });
+
+ let value: Foo = from_value(json!({})).unwrap();
+ assert_eq!(value, Foo { x: None });
+
+ let value: Foo = from_value(json!({"y": 5})).unwrap();
+ assert_eq!(value, Foo { x: Some(5) });
+}
+
+#[test]
+fn test_serialize_seq_with_no_len() {
+ #[derive(Clone, Debug, PartialEq)]
+ struct MyVec<T>(Vec<T>);
+
+ impl<T> ser::Serialize for MyVec<T>
+ where
+ T: ser::Serialize,
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: ser::Serializer,
+ {
+ let mut seq = serializer.serialize_seq(None)?;
+ for elem in &self.0 {
+ seq.serialize_element(elem)?;
+ }
+ seq.end()
+ }
+ }
+
+ struct Visitor<T> {
+ marker: PhantomData<MyVec<T>>,
+ }
+
+ impl<'de, T> de::Visitor<'de> for Visitor<T>
+ where
+ T: de::Deserialize<'de>,
+ {
+ type Value = MyVec<T>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("array")
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<MyVec<T>, E>
+ where
+ E: de::Error,
+ {
+ Ok(MyVec(Vec::new()))
+ }
+
+ #[inline]
+ fn visit_seq<V>(self, mut visitor: V) -> Result<MyVec<T>, V::Error>
+ where
+ V: de::SeqAccess<'de>,
+ {
+ let mut values = Vec::new();
+
+ while let Some(value) = visitor.next_element()? {
+ values.push(value);
+ }
+
+ Ok(MyVec(values))
+ }
+ }
+
+ impl<'de, T> de::Deserialize<'de> for MyVec<T>
+ where
+ T: de::Deserialize<'de>,
+ {
+ fn deserialize<D>(deserializer: D) -> Result<MyVec<T>, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ deserializer.deserialize_map(Visitor {
+ marker: PhantomData,
+ })
+ }
+ }
+
+ let mut vec = Vec::new();
+ vec.push(MyVec(Vec::new()));
+ vec.push(MyVec(Vec::new()));
+ let vec: MyVec<MyVec<u32>> = MyVec(vec);
+
+ test_encode_ok(&[(vec.clone(), "[[],[]]")]);
+
+ let s = to_string_pretty(&vec).unwrap();
+ let expected = pretty_str!([[], []]);
+ assert_eq!(s, expected);
+}
+
+#[test]
+fn test_serialize_map_with_no_len() {
+ #[derive(Clone, Debug, PartialEq)]
+ struct MyMap<K, V>(BTreeMap<K, V>);
+
+ impl<K, V> ser::Serialize for MyMap<K, V>
+ where
+ K: ser::Serialize + Ord,
+ V: ser::Serialize,
+ {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: ser::Serializer,
+ {
+ let mut map = serializer.serialize_map(None)?;
+ for (k, v) in &self.0 {
+ map.serialize_entry(k, v)?;
+ }
+ map.end()
+ }
+ }
+
+ struct Visitor<K, V> {
+ marker: PhantomData<MyMap<K, V>>,
+ }
+
+ impl<'de, K, V> de::Visitor<'de> for Visitor<K, V>
+ where
+ K: de::Deserialize<'de> + Eq + Ord,
+ V: de::Deserialize<'de>,
+ {
+ type Value = MyMap<K, V>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("map")
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<MyMap<K, V>, E>
+ where
+ E: de::Error,
+ {
+ Ok(MyMap(BTreeMap::new()))
+ }
+
+ #[inline]
+ fn visit_map<Visitor>(self, mut visitor: Visitor) -> Result<MyMap<K, V>, Visitor::Error>
+ where
+ Visitor: de::MapAccess<'de>,
+ {
+ let mut values = BTreeMap::new();
+
+ while let Some((key, value)) = visitor.next_entry()? {
+ values.insert(key, value);
+ }
+
+ Ok(MyMap(values))
+ }
+ }
+
+ impl<'de, K, V> de::Deserialize<'de> for MyMap<K, V>
+ where
+ K: de::Deserialize<'de> + Eq + Ord,
+ V: de::Deserialize<'de>,
+ {
+ fn deserialize<D>(deserializer: D) -> Result<MyMap<K, V>, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ deserializer.deserialize_map(Visitor {
+ marker: PhantomData,
+ })
+ }
+ }
+
+ let mut map = BTreeMap::new();
+ map.insert("a", MyMap(BTreeMap::new()));
+ map.insert("b", MyMap(BTreeMap::new()));
+ let map: MyMap<_, MyMap<u32, u32>> = MyMap(map);
+
+ test_encode_ok(&[(map.clone(), "{\"a\":{},\"b\":{}}")]);
+
+ let s = to_string_pretty(&map).unwrap();
+ let expected = pretty_str!({
+ "a": {},
+ "b": {}
+ });
+ assert_eq!(s, expected);
+}
+
+#[cfg(not(miri))]
+#[test]
+fn test_deserialize_from_stream() {
+ use serde_json::to_writer;
+ use std::net::{TcpListener, TcpStream};
+ use std::thread;
+
+ #[derive(Debug, PartialEq, Serialize, Deserialize)]
+ struct Message {
+ message: String,
+ }
+
+ let l = TcpListener::bind("localhost:20000").unwrap();
+
+ thread::spawn(|| {
+ let l = l;
+ for stream in l.incoming() {
+ let mut stream = stream.unwrap();
+ let read_stream = stream.try_clone().unwrap();
+
+ let mut de = Deserializer::from_reader(read_stream);
+ let request = Message::deserialize(&mut de).unwrap();
+ let response = Message {
+ message: request.message,
+ };
+ to_writer(&mut stream, &response).unwrap();
+ }
+ });
+
+ let mut stream = TcpStream::connect("localhost:20000").unwrap();
+ let request = Message {
+ message: "hi there".to_string(),
+ };
+ to_writer(&mut stream, &request).unwrap();
+
+ let mut de = Deserializer::from_reader(stream);
+ let response = Message::deserialize(&mut de).unwrap();
+
+ assert_eq!(request, response);
+}
+
+#[test]
+fn test_serialize_rejects_bool_keys() {
+ let map = treemap!(
+ true => 2,
+ false => 4
+ );
+
+ let err = to_vec(&map).unwrap_err();
+ assert_eq!(err.to_string(), "key must be a string");
+}
+
+#[test]
+fn test_serialize_rejects_adt_keys() {
+ let map = treemap!(
+ Some("a") => 2,
+ Some("b") => 4,
+ None => 6
+ );
+
+ let err = to_vec(&map).unwrap_err();
+ assert_eq!(err.to_string(), "key must be a string");
+}
+
+#[test]
+fn test_bytes_ser() {
+ let buf = vec![];
+ let bytes = Bytes::new(&buf);
+ assert_eq!(to_string(&bytes).unwrap(), "[]".to_string());
+
+ let buf = vec![1, 2, 3];
+ let bytes = Bytes::new(&buf);
+ assert_eq!(to_string(&bytes).unwrap(), "[1,2,3]".to_string());
+}
+
+#[test]
+fn test_byte_buf_ser() {
+ let bytes = ByteBuf::new();
+ assert_eq!(to_string(&bytes).unwrap(), "[]".to_string());
+
+ let bytes = ByteBuf::from(vec![1, 2, 3]);
+ assert_eq!(to_string(&bytes).unwrap(), "[1,2,3]".to_string());
+}
+
+#[test]
+fn test_byte_buf_de() {
+ let bytes = ByteBuf::new();
+ let v: ByteBuf = from_str("[]").unwrap();
+ assert_eq!(v, bytes);
+
+ let bytes = ByteBuf::from(vec![1, 2, 3]);
+ let v: ByteBuf = from_str("[1, 2, 3]").unwrap();
+ assert_eq!(v, bytes);
+}
+
+#[test]
+fn test_byte_buf_de_lone_surrogate() {
+ let bytes = ByteBuf::from(vec![237, 160, 188]);
+ let v: ByteBuf = from_str(r#""\ud83c""#).unwrap();
+ assert_eq!(v, bytes);
+
+ let bytes = ByteBuf::from(vec![237, 160, 188, 10]);
+ let v: ByteBuf = from_str(r#""\ud83c\n""#).unwrap();
+ assert_eq!(v, bytes);
+
+ let bytes = ByteBuf::from(vec![237, 160, 188, 32]);
+ let v: ByteBuf = from_str(r#""\ud83c ""#).unwrap();
+ assert_eq!(v, bytes);
+
+ let bytes = ByteBuf::from(vec![237, 176, 129]);
+ let v: ByteBuf = from_str(r#""\udc01""#).unwrap();
+ assert_eq!(v, bytes);
+
+ let res = from_str::<ByteBuf>(r#""\ud83c\!""#);
+ assert!(res.is_err());
+
+ let res = from_str::<ByteBuf>(r#""\ud83c\u""#);
+ assert!(res.is_err());
+
+ let res = from_str::<ByteBuf>(r#""\ud83c\ud83c""#);
+ assert!(res.is_err());
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_raw_de_lone_surrogate() {
+ use serde_json::value::RawValue;
+
+ assert!(from_str::<Box<RawValue>>(r#""\ud83c""#).is_ok());
+ assert!(from_str::<Box<RawValue>>(r#""\ud83c\n""#).is_ok());
+ assert!(from_str::<Box<RawValue>>(r#""\ud83c ""#).is_ok());
+ assert!(from_str::<Box<RawValue>>(r#""\udc01 ""#).is_ok());
+ assert!(from_str::<Box<RawValue>>(r#""\udc01\!""#).is_err());
+ assert!(from_str::<Box<RawValue>>(r#""\udc01\u""#).is_err());
+ assert!(from_str::<Box<RawValue>>(r#""\ud83c\ud83c""#).is_ok());
+}
+
+#[test]
+fn test_byte_buf_de_multiple() {
+ let s: Vec<ByteBuf> = from_str(r#"["ab\nc", "cd\ne"]"#).unwrap();
+ let a = ByteBuf::from(b"ab\nc".to_vec());
+ let b = ByteBuf::from(b"cd\ne".to_vec());
+ assert_eq!(vec![a, b], s);
+}
+
+#[test]
+fn test_json_pointer() {
+ // Test case taken from https://tools.ietf.org/html/rfc6901#page-5
+ let data: Value = from_str(
+ r#"{
+ "foo": ["bar", "baz"],
+ "": 0,
+ "a/b": 1,
+ "c%d": 2,
+ "e^f": 3,
+ "g|h": 4,
+ "i\\j": 5,
+ "k\"l": 6,
+ " ": 7,
+ "m~n": 8
+ }"#,
+ )
+ .unwrap();
+ assert_eq!(data.pointer("").unwrap(), &data);
+ assert_eq!(data.pointer("/foo").unwrap(), &json!(["bar", "baz"]));
+ assert_eq!(data.pointer("/foo/0").unwrap(), &json!("bar"));
+ assert_eq!(data.pointer("/").unwrap(), &json!(0));
+ assert_eq!(data.pointer("/a~1b").unwrap(), &json!(1));
+ assert_eq!(data.pointer("/c%d").unwrap(), &json!(2));
+ assert_eq!(data.pointer("/e^f").unwrap(), &json!(3));
+ assert_eq!(data.pointer("/g|h").unwrap(), &json!(4));
+ assert_eq!(data.pointer("/i\\j").unwrap(), &json!(5));
+ assert_eq!(data.pointer("/k\"l").unwrap(), &json!(6));
+ assert_eq!(data.pointer("/ ").unwrap(), &json!(7));
+ assert_eq!(data.pointer("/m~0n").unwrap(), &json!(8));
+ // Invalid pointers
+ assert!(data.pointer("/unknown").is_none());
+ assert!(data.pointer("/e^f/ertz").is_none());
+ assert!(data.pointer("/foo/00").is_none());
+ assert!(data.pointer("/foo/01").is_none());
+}
+
+#[test]
+fn test_json_pointer_mut() {
+ // Test case taken from https://tools.ietf.org/html/rfc6901#page-5
+ let mut data: Value = from_str(
+ r#"{
+ "foo": ["bar", "baz"],
+ "": 0,
+ "a/b": 1,
+ "c%d": 2,
+ "e^f": 3,
+ "g|h": 4,
+ "i\\j": 5,
+ "k\"l": 6,
+ " ": 7,
+ "m~n": 8
+ }"#,
+ )
+ .unwrap();
+
+ // Basic pointer checks
+ assert_eq!(data.pointer_mut("/foo").unwrap(), &json!(["bar", "baz"]));
+ assert_eq!(data.pointer_mut("/foo/0").unwrap(), &json!("bar"));
+ assert_eq!(data.pointer_mut("/").unwrap(), 0);
+ assert_eq!(data.pointer_mut("/a~1b").unwrap(), 1);
+ assert_eq!(data.pointer_mut("/c%d").unwrap(), 2);
+ assert_eq!(data.pointer_mut("/e^f").unwrap(), 3);
+ assert_eq!(data.pointer_mut("/g|h").unwrap(), 4);
+ assert_eq!(data.pointer_mut("/i\\j").unwrap(), 5);
+ assert_eq!(data.pointer_mut("/k\"l").unwrap(), 6);
+ assert_eq!(data.pointer_mut("/ ").unwrap(), 7);
+ assert_eq!(data.pointer_mut("/m~0n").unwrap(), 8);
+
+ // Invalid pointers
+ assert!(data.pointer_mut("/unknown").is_none());
+ assert!(data.pointer_mut("/e^f/ertz").is_none());
+ assert!(data.pointer_mut("/foo/00").is_none());
+ assert!(data.pointer_mut("/foo/01").is_none());
+
+ // Mutable pointer checks
+ *data.pointer_mut("/").unwrap() = 100.into();
+ assert_eq!(data.pointer("/").unwrap(), 100);
+ *data.pointer_mut("/foo/0").unwrap() = json!("buzz");
+ assert_eq!(data.pointer("/foo/0").unwrap(), &json!("buzz"));
+
+ // Example of ownership stealing
+ assert_eq!(
+ data.pointer_mut("/a~1b")
+ .map(|m| mem::replace(m, json!(null)))
+ .unwrap(),
+ 1
+ );
+ assert_eq!(data.pointer("/a~1b").unwrap(), &json!(null));
+
+ // Need to compare against a clone so we don't anger the borrow checker
+ // by taking out two references to a mutable value
+ let mut d2 = data.clone();
+ assert_eq!(data.pointer_mut("").unwrap(), &mut d2);
+}
+
+#[test]
+fn test_stack_overflow() {
+ let brackets: String = iter::repeat('[')
+ .take(127)
+ .chain(iter::repeat(']').take(127))
+ .collect();
+ let _: Value = from_str(&brackets).unwrap();
+
+ let brackets = "[".repeat(129);
+ test_parse_err::<Value>(&[(&brackets, "recursion limit exceeded at line 1 column 128")]);
+}
+
+#[test]
+#[cfg(feature = "unbounded_depth")]
+fn test_disable_recursion_limit() {
+ let brackets: String = iter::repeat('[')
+ .take(140)
+ .chain(iter::repeat(']').take(140))
+ .collect();
+
+ let mut deserializer = Deserializer::from_str(&brackets);
+ deserializer.disable_recursion_limit();
+ Value::deserialize(&mut deserializer).unwrap();
+}
+
+#[test]
+fn test_integer_key() {
+ // map with integer keys
+ let map = treemap!(
+ 1 => 2,
+ -1 => 6
+ );
+ let j = r#"{"-1":6,"1":2}"#;
+ test_encode_ok(&[(&map, j)]);
+ test_parse_ok(vec![(j, map)]);
+
+ let j = r#"{"x":null}"#;
+ test_parse_err::<BTreeMap<i32, ()>>(&[(
+ j,
+ "invalid type: string \"x\", expected i32 at line 1 column 4",
+ )]);
+}
+
+#[test]
+fn test_integer128_key() {
+ let map = treemap! {
+ 100000000000000000000000000000000000000u128 => ()
+ };
+ let j = r#"{"100000000000000000000000000000000000000":null}"#;
+ assert_eq!(to_string(&map).unwrap(), j);
+ assert_eq!(from_str::<BTreeMap<u128, ()>>(j).unwrap(), map);
+}
+
+#[test]
+fn test_deny_float_key() {
+ #[derive(Eq, PartialEq, Ord, PartialOrd)]
+ struct Float;
+ impl Serialize for Float {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_f32(1.0)
+ }
+ }
+
+ // map with float key
+ let map = treemap!(Float => "x");
+ assert!(serde_json::to_value(&map).is_err());
+}
+
+#[test]
+fn test_borrowed_key() {
+ let map: BTreeMap<&str, ()> = from_str("{\"borrowed\":null}").unwrap();
+ let expected = treemap! { "borrowed" => () };
+ assert_eq!(map, expected);
+
+ #[derive(Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq)]
+ struct NewtypeStr<'a>(&'a str);
+
+ let map: BTreeMap<NewtypeStr, ()> = from_str("{\"borrowed\":null}").unwrap();
+ let expected = treemap! { NewtypeStr("borrowed") => () };
+ assert_eq!(map, expected);
+}
+
+#[test]
+fn test_effectively_string_keys() {
+ #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Serialize, Deserialize)]
+ enum Enum {
+ One,
+ Two,
+ }
+ let map = treemap! {
+ Enum::One => 1,
+ Enum::Two => 2
+ };
+ let expected = r#"{"One":1,"Two":2}"#;
+ test_encode_ok(&[(&map, expected)]);
+ test_parse_ok(vec![(expected, map)]);
+
+ #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Serialize, Deserialize)]
+ struct Wrapper(String);
+ let map = treemap! {
+ Wrapper("zero".to_owned()) => 0,
+ Wrapper("one".to_owned()) => 1
+ };
+ let expected = r#"{"one":1,"zero":0}"#;
+ test_encode_ok(&[(&map, expected)]);
+ test_parse_ok(vec![(expected, map)]);
+}
+
+#[test]
+fn test_json_macro() {
+ // This is tricky because the <...> is not a single TT and the comma inside
+ // looks like an array element separator.
+ let _ = json!([
+ <Result<(), ()> as Clone>::clone(&Ok(())),
+ <Result<(), ()> as Clone>::clone(&Err(()))
+ ]);
+
+ // Same thing but in the map values.
+ let _ = json!({
+ "ok": <Result<(), ()> as Clone>::clone(&Ok(())),
+ "err": <Result<(), ()> as Clone>::clone(&Err(()))
+ });
+
+ // It works in map keys but only if they are parenthesized.
+ let _ = json!({
+ (<Result<&str, ()> as Clone>::clone(&Ok("")).unwrap()): "ok",
+ (<Result<(), &str> as Clone>::clone(&Err("")).unwrap_err()): "err"
+ });
+
+ #[deny(unused_results)]
+ let _ = json!({ "architecture": [true, null] });
+}
+
+#[test]
+fn issue_220() {
+ #[derive(Debug, PartialEq, Eq, Deserialize)]
+ enum E {
+ V(u8),
+ }
+
+ assert!(from_str::<E>(r#" "V"0 "#).is_err());
+
+ assert_eq!(from_str::<E>(r#"{"V": 0}"#).unwrap(), E::V(0));
+}
+
+macro_rules! number_partialeq_ok {
+ ($($n:expr)*) => {
+ $(
+ let value = to_value($n).unwrap();
+ let s = $n.to_string();
+ assert_eq!(value, $n);
+ assert_eq!($n, value);
+ assert_ne!(value, s);
+ )*
+ }
+}
+
+#[test]
+fn test_partialeq_number() {
+ number_partialeq_ok!(0 1 100
+ i8::MIN i8::MAX i16::MIN i16::MAX i32::MIN i32::MAX i64::MIN i64::MAX
+ u8::MIN u8::MAX u16::MIN u16::MAX u32::MIN u32::MAX u64::MIN u64::MAX
+ f32::MIN f32::MAX f32::MIN_EXP f32::MAX_EXP f32::MIN_POSITIVE
+ f64::MIN f64::MAX f64::MIN_EXP f64::MAX_EXP f64::MIN_POSITIVE
+ f32::consts::E f32::consts::PI f32::consts::LN_2 f32::consts::LOG2_E
+ f64::consts::E f64::consts::PI f64::consts::LN_2 f64::consts::LOG2_E
+ );
+}
+
+#[test]
+#[cfg(integer128)]
+#[cfg(feature = "arbitrary_precision")]
+fn test_partialeq_integer128() {
+ number_partialeq_ok!(i128::MIN i128::MAX u128::MIN u128::MAX)
+}
+
+#[test]
+fn test_partialeq_string() {
+ let v = to_value("42").unwrap();
+ assert_eq!(v, "42");
+ assert_eq!("42", v);
+ assert_ne!(v, 42);
+ assert_eq!(v, String::from("42"));
+ assert_eq!(String::from("42"), v);
+}
+
+#[test]
+fn test_partialeq_bool() {
+ let v = to_value(true).unwrap();
+ assert_eq!(v, true);
+ assert_eq!(true, v);
+ assert_ne!(v, false);
+ assert_ne!(v, "true");
+ assert_ne!(v, 1);
+ assert_ne!(v, 0);
+}
+
+struct FailReader(io::ErrorKind);
+
+impl io::Read for FailReader {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ Err(io::Error::new(self.0, "oh no!"))
+ }
+}
+
+#[test]
+fn test_category() {
+ assert!(from_str::<String>("123").unwrap_err().is_data());
+
+ assert!(from_str::<String>("]").unwrap_err().is_syntax());
+
+ assert!(from_str::<String>("").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"\\").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"\\u").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"\\u0").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"\\u00").unwrap_err().is_eof());
+ assert!(from_str::<String>("\"\\u000").unwrap_err().is_eof());
+
+ assert!(from_str::<Vec<usize>>("[").unwrap_err().is_eof());
+ assert!(from_str::<Vec<usize>>("[0").unwrap_err().is_eof());
+ assert!(from_str::<Vec<usize>>("[0,").unwrap_err().is_eof());
+
+ assert!(from_str::<BTreeMap<String, usize>>("{")
+ .unwrap_err()
+ .is_eof());
+ assert!(from_str::<BTreeMap<String, usize>>("{\"k\"")
+ .unwrap_err()
+ .is_eof());
+ assert!(from_str::<BTreeMap<String, usize>>("{\"k\":")
+ .unwrap_err()
+ .is_eof());
+ assert!(from_str::<BTreeMap<String, usize>>("{\"k\":0")
+ .unwrap_err()
+ .is_eof());
+ assert!(from_str::<BTreeMap<String, usize>>("{\"k\":0,")
+ .unwrap_err()
+ .is_eof());
+
+ let fail = FailReader(io::ErrorKind::NotConnected);
+ assert!(from_reader::<_, String>(fail).unwrap_err().is_io());
+}
+
+#[test]
+// Clippy false positive: https://github.com/Manishearth/rust-clippy/issues/292
+#[allow(clippy::needless_lifetimes)]
+fn test_into_io_error() {
+ fn io_error<'de, T: Deserialize<'de> + Debug>(j: &'static str) -> io::Error {
+ from_str::<T>(j).unwrap_err().into()
+ }
+
+ assert_eq!(
+ io_error::<String>("\"\\u").kind(),
+ io::ErrorKind::UnexpectedEof
+ );
+ assert_eq!(io_error::<String>("0").kind(), io::ErrorKind::InvalidData);
+ assert_eq!(io_error::<String>("]").kind(), io::ErrorKind::InvalidData);
+
+ let fail = FailReader(io::ErrorKind::NotConnected);
+ let io_err: io::Error = from_reader::<_, u8>(fail).unwrap_err().into();
+ assert_eq!(io_err.kind(), io::ErrorKind::NotConnected);
+}
+
+#[test]
+fn test_borrow() {
+ let s: &str = from_str("\"borrowed\"").unwrap();
+ assert_eq!("borrowed", s);
+
+ let s: &str = from_slice(b"\"borrowed\"").unwrap();
+ assert_eq!("borrowed", s);
+}
+
+#[test]
+fn null_invalid_type() {
+ let err = serde_json::from_str::<String>("null").unwrap_err();
+ assert_eq!(
+ format!("{}", err),
+ String::from("invalid type: null, expected a string at line 1 column 4")
+ );
+}
+
+#[test]
+fn test_integer128() {
+ let signed = &[i128::min_value(), -1, 0, 1, i128::max_value()];
+ let unsigned = &[0, 1, u128::max_value()];
+
+ for integer128 in signed {
+ let expected = integer128.to_string();
+ assert_eq!(to_string(integer128).unwrap(), expected);
+ assert_eq!(from_str::<i128>(&expected).unwrap(), *integer128);
+ }
+
+ for integer128 in unsigned {
+ let expected = integer128.to_string();
+ assert_eq!(to_string(integer128).unwrap(), expected);
+ assert_eq!(from_str::<u128>(&expected).unwrap(), *integer128);
+ }
+
+ test_parse_err::<i128>(&[
+ (
+ "-170141183460469231731687303715884105729",
+ "number out of range at line 1 column 40",
+ ),
+ (
+ "170141183460469231731687303715884105728",
+ "number out of range at line 1 column 39",
+ ),
+ ]);
+
+ test_parse_err::<u128>(&[
+ ("-1", "number out of range at line 1 column 1"),
+ (
+ "340282366920938463463374607431768211456",
+ "number out of range at line 1 column 39",
+ ),
+ ]);
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_borrowed_raw_value() {
+ #[derive(Serialize, Deserialize)]
+ struct Wrapper<'a> {
+ a: i8,
+ #[serde(borrow)]
+ b: &'a RawValue,
+ c: i8,
+ }
+
+ let wrapper_from_str: Wrapper =
+ serde_json::from_str(r#"{"a": 1, "b": {"foo": 2}, "c": 3}"#).unwrap();
+ assert_eq!(r#"{"foo": 2}"#, wrapper_from_str.b.get());
+
+ let wrapper_to_string = serde_json::to_string(&wrapper_from_str).unwrap();
+ assert_eq!(r#"{"a":1,"b":{"foo": 2},"c":3}"#, wrapper_to_string);
+
+ let wrapper_to_value = serde_json::to_value(&wrapper_from_str).unwrap();
+ assert_eq!(json!({"a": 1, "b": {"foo": 2}, "c": 3}), wrapper_to_value);
+
+ let array_from_str: Vec<&RawValue> =
+ serde_json::from_str(r#"["a", 42, {"foo": "bar"}, null]"#).unwrap();
+ assert_eq!(r#""a""#, array_from_str[0].get());
+ assert_eq!(r#"42"#, array_from_str[1].get());
+ assert_eq!(r#"{"foo": "bar"}"#, array_from_str[2].get());
+ assert_eq!(r#"null"#, array_from_str[3].get());
+
+ let array_to_string = serde_json::to_string(&array_from_str).unwrap();
+ assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string);
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_raw_value_in_map_key() {
+ #[derive(RefCast)]
+ #[repr(transparent)]
+ struct RawMapKey(RawValue);
+
+ impl<'de> Deserialize<'de> for &'de RawMapKey {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let raw_value = <&RawValue>::deserialize(deserializer)?;
+ Ok(RawMapKey::ref_cast(raw_value))
+ }
+ }
+
+ impl PartialEq for RawMapKey {
+ fn eq(&self, other: &Self) -> bool {
+ self.0.get() == other.0.get()
+ }
+ }
+
+ impl Eq for RawMapKey {}
+
+ impl Hash for RawMapKey {
+ fn hash<H: Hasher>(&self, hasher: &mut H) {
+ self.0.get().hash(hasher);
+ }
+ }
+
+ let map_from_str: HashMap<&RawMapKey, &RawValue> =
+ serde_json::from_str(r#" {"\\k":"\\v"} "#).unwrap();
+ let (map_k, map_v) = map_from_str.into_iter().next().unwrap();
+ assert_eq!("\"\\\\k\"", map_k.0.get());
+ assert_eq!("\"\\\\v\"", map_v.get());
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_boxed_raw_value() {
+ #[derive(Serialize, Deserialize)]
+ struct Wrapper {
+ a: i8,
+ b: Box<RawValue>,
+ c: i8,
+ }
+
+ let wrapper_from_str: Wrapper =
+ serde_json::from_str(r#"{"a": 1, "b": {"foo": 2}, "c": 3}"#).unwrap();
+ assert_eq!(r#"{"foo": 2}"#, wrapper_from_str.b.get());
+
+ let wrapper_from_reader: Wrapper =
+ serde_json::from_reader(br#"{"a": 1, "b": {"foo": 2}, "c": 3}"#.as_ref()).unwrap();
+ assert_eq!(r#"{"foo": 2}"#, wrapper_from_reader.b.get());
+
+ let wrapper_from_value: Wrapper =
+ serde_json::from_value(json!({"a": 1, "b": {"foo": 2}, "c": 3})).unwrap();
+ assert_eq!(r#"{"foo":2}"#, wrapper_from_value.b.get());
+
+ let wrapper_to_string = serde_json::to_string(&wrapper_from_str).unwrap();
+ assert_eq!(r#"{"a":1,"b":{"foo": 2},"c":3}"#, wrapper_to_string);
+
+ let wrapper_to_value = serde_json::to_value(&wrapper_from_str).unwrap();
+ assert_eq!(json!({"a": 1, "b": {"foo": 2}, "c": 3}), wrapper_to_value);
+
+ let array_from_str: Vec<Box<RawValue>> =
+ serde_json::from_str(r#"["a", 42, {"foo": "bar"}, null]"#).unwrap();
+ assert_eq!(r#""a""#, array_from_str[0].get());
+ assert_eq!(r#"42"#, array_from_str[1].get());
+ assert_eq!(r#"{"foo": "bar"}"#, array_from_str[2].get());
+ assert_eq!(r#"null"#, array_from_str[3].get());
+
+ let array_from_reader: Vec<Box<RawValue>> =
+ serde_json::from_reader(br#"["a", 42, {"foo": "bar"}, null]"#.as_ref()).unwrap();
+ assert_eq!(r#""a""#, array_from_reader[0].get());
+ assert_eq!(r#"42"#, array_from_reader[1].get());
+ assert_eq!(r#"{"foo": "bar"}"#, array_from_reader[2].get());
+ assert_eq!(r#"null"#, array_from_reader[3].get());
+
+ let array_to_string = serde_json::to_string(&array_from_str).unwrap();
+ assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string);
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_raw_invalid_utf8() {
+ let j = &[b'"', b'\xCE', b'\xF8', b'"'];
+ let value_err = serde_json::from_slice::<Value>(j).unwrap_err();
+ let raw_value_err = serde_json::from_slice::<Box<RawValue>>(j).unwrap_err();
+
+ assert_eq!(
+ value_err.to_string(),
+ "invalid unicode code point at line 1 column 4",
+ );
+ assert_eq!(
+ raw_value_err.to_string(),
+ "invalid unicode code point at line 1 column 4",
+ );
+}
+
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_serialize_unsized_value_to_raw_value() {
+ assert_eq!(
+ serde_json::value::to_raw_value("foobar").unwrap().get(),
+ r#""foobar""#,
+ );
+}
+
+#[test]
+fn test_borrow_in_map_key() {
+ #[derive(Deserialize, Debug)]
+ struct Outer {
+ #[allow(dead_code)]
+ map: BTreeMap<MyMapKey, ()>,
+ }
+
+ #[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
+ struct MyMapKey(usize);
+
+ impl<'de> Deserialize<'de> for MyMapKey {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ let s = <&str>::deserialize(deserializer)?;
+ let n = s.parse().map_err(de::Error::custom)?;
+ Ok(MyMapKey(n))
+ }
+ }
+
+ let value = json!({ "map": { "1": null } });
+ Outer::deserialize(&value).unwrap();
+}
+
+#[test]
+fn test_value_into_deserializer() {
+ #[derive(Deserialize)]
+ struct Outer {
+ inner: Inner,
+ }
+
+ #[derive(Deserialize)]
+ struct Inner {
+ string: String,
+ }
+
+ let mut map = BTreeMap::new();
+ map.insert("inner", json!({ "string": "Hello World" }));
+
+ let outer = Outer::deserialize(map.into_deserializer()).unwrap();
+ assert_eq!(outer.inner.string, "Hello World");
+}
+
+#[test]
+fn hash_positive_and_negative_zero() {
+ fn hash(obj: impl Hash) -> u64 {
+ let mut hasher = DefaultHasher::new();
+ obj.hash(&mut hasher);
+ hasher.finish()
+ }
+
+ let k1 = serde_json::from_str::<Number>("0.0").unwrap();
+ let k2 = serde_json::from_str::<Number>("-0.0").unwrap();
+ if cfg!(feature = "arbitrary_precision") {
+ assert_ne!(k1, k2);
+ assert_ne!(hash(k1), hash(k2));
+ } else {
+ assert_eq!(k1, k2);
+ assert_eq!(hash(k1), hash(k2));
+ }
+}
diff --git a/tests/ui/missing_colon.rs b/tests/ui/missing_colon.rs
new file mode 100644
index 0000000..d93b7b9
--- /dev/null
+++ b/tests/ui/missing_colon.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" });
+}
diff --git a/tests/ui/missing_colon.stderr b/tests/ui/missing_colon.stderr
new file mode 100644
index 0000000..9b83c17
--- /dev/null
+++ b/tests/ui/missing_colon.stderr
@@ -0,0 +1,7 @@
+error: unexpected end of macro invocation
+ --> tests/ui/missing_colon.rs:4:5
+ |
+4 | json!({ "a" });
+ | ^^^^^^^^^^^^^^ missing tokens in macro arguments
+ |
+ = note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git a/tests/ui/missing_comma.rs b/tests/ui/missing_comma.rs
new file mode 100644
index 0000000..8818c3e
--- /dev/null
+++ b/tests/ui/missing_comma.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "1": "" "2": "" });
+}
diff --git a/tests/ui/missing_comma.stderr b/tests/ui/missing_comma.stderr
new file mode 100644
index 0000000..bd911d0
--- /dev/null
+++ b/tests/ui/missing_comma.stderr
@@ -0,0 +1,7 @@
+error: no rules expected the token `"2"`
+ --> tests/ui/missing_comma.rs:4:21
+ |
+4 | json!({ "1": "" "2": "" });
+ | -^^^ no rules expected this token in macro call
+ | |
+ | help: missing comma here
diff --git a/tests/ui/missing_value.rs b/tests/ui/missing_value.rs
new file mode 100644
index 0000000..0ba14e2
--- /dev/null
+++ b/tests/ui/missing_value.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : });
+}
diff --git a/tests/ui/missing_value.stderr b/tests/ui/missing_value.stderr
new file mode 100644
index 0000000..d538d96
--- /dev/null
+++ b/tests/ui/missing_value.stderr
@@ -0,0 +1,7 @@
+error: unexpected end of macro invocation
+ --> tests/ui/missing_value.rs:4:5
+ |
+4 | json!({ "a" : });
+ | ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
+ |
+ = note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git a/tests/ui/not_found.rs b/tests/ui/not_found.rs
new file mode 100644
index 0000000..2df6870
--- /dev/null
+++ b/tests/ui/not_found.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : x });
+}
diff --git a/tests/ui/not_found.stderr b/tests/ui/not_found.stderr
new file mode 100644
index 0000000..6fec180
--- /dev/null
+++ b/tests/ui/not_found.stderr
@@ -0,0 +1,5 @@
+error[E0425]: cannot find value `x` in this scope
+ --> tests/ui/not_found.rs:4:19
+ |
+4 | json!({ "a" : x });
+ | ^ not found in this scope
diff --git a/tests/ui/parse_expr.rs b/tests/ui/parse_expr.rs
new file mode 100644
index 0000000..e7f1805
--- /dev/null
+++ b/tests/ui/parse_expr.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : ~ });
+}
diff --git a/tests/ui/parse_expr.stderr b/tests/ui/parse_expr.stderr
new file mode 100644
index 0000000..6959673
--- /dev/null
+++ b/tests/ui/parse_expr.stderr
@@ -0,0 +1,5 @@
+error: no rules expected the token `~`
+ --> tests/ui/parse_expr.rs:4:19
+ |
+4 | json!({ "a" : ~ });
+ | ^ no rules expected this token in macro call
diff --git a/tests/ui/parse_key.rs b/tests/ui/parse_key.rs
new file mode 100644
index 0000000..858bd71
--- /dev/null
+++ b/tests/ui/parse_key.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "".s : true });
+}
diff --git a/tests/ui/parse_key.stderr b/tests/ui/parse_key.stderr
new file mode 100644
index 0000000..f10c218
--- /dev/null
+++ b/tests/ui/parse_key.stderr
@@ -0,0 +1,5 @@
+error[E0609]: no field `s` on type `&'static str`
+ --> tests/ui/parse_key.rs:4:16
+ |
+4 | json!({ "".s : true });
+ | ^
diff --git a/tests/ui/unexpected_after_array_element.rs b/tests/ui/unexpected_after_array_element.rs
new file mode 100644
index 0000000..226c58c
--- /dev/null
+++ b/tests/ui/unexpected_after_array_element.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!([ true => ]);
+}
diff --git a/tests/ui/unexpected_after_array_element.stderr b/tests/ui/unexpected_after_array_element.stderr
new file mode 100644
index 0000000..f745a21
--- /dev/null
+++ b/tests/ui/unexpected_after_array_element.stderr
@@ -0,0 +1,5 @@
+error: no rules expected the token `=>`
+ --> tests/ui/unexpected_after_array_element.rs:4:18
+ |
+4 | json!([ true => ]);
+ | ^^ no rules expected this token in macro call
diff --git a/tests/ui/unexpected_after_map_entry.rs b/tests/ui/unexpected_after_map_entry.rs
new file mode 100644
index 0000000..0dfb731
--- /dev/null
+++ b/tests/ui/unexpected_after_map_entry.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "k": true => });
+}
diff --git a/tests/ui/unexpected_after_map_entry.stderr b/tests/ui/unexpected_after_map_entry.stderr
new file mode 100644
index 0000000..a18c9b4
--- /dev/null
+++ b/tests/ui/unexpected_after_map_entry.stderr
@@ -0,0 +1,5 @@
+error: no rules expected the token `=>`
+ --> tests/ui/unexpected_after_map_entry.rs:4:23
+ |
+4 | json!({ "k": true => });
+ | ^^ no rules expected this token in macro call
diff --git a/tests/ui/unexpected_colon.rs b/tests/ui/unexpected_colon.rs
new file mode 100644
index 0000000..e767ea6
--- /dev/null
+++ b/tests/ui/unexpected_colon.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ : true });
+}
diff --git a/tests/ui/unexpected_colon.stderr b/tests/ui/unexpected_colon.stderr
new file mode 100644
index 0000000..ed038f6
--- /dev/null
+++ b/tests/ui/unexpected_colon.stderr
@@ -0,0 +1,5 @@
+error: no rules expected the token `:`
+ --> tests/ui/unexpected_colon.rs:4:13
+ |
+4 | json!({ : true });
+ | ^ no rules expected this token in macro call
diff --git a/tests/ui/unexpected_comma.rs b/tests/ui/unexpected_comma.rs
new file mode 100644
index 0000000..338874e
--- /dev/null
+++ b/tests/ui/unexpected_comma.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" , "b": true });
+}
diff --git a/tests/ui/unexpected_comma.stderr b/tests/ui/unexpected_comma.stderr
new file mode 100644
index 0000000..a4309c4
--- /dev/null
+++ b/tests/ui/unexpected_comma.stderr
@@ -0,0 +1,5 @@
+error: no rules expected the token `,`
+ --> tests/ui/unexpected_comma.rs:4:17
+ |
+4 | json!({ "a" , "b": true });
+ | ^ no rules expected this token in macro call