summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_json
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/serde_json
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_json')
-rw-r--r--third_party/rust/serde_json/.cargo-checksum.json1
-rw-r--r--third_party/rust/serde_json/CONTRIBUTING.md46
-rw-r--r--third_party/rust/serde_json/Cargo.toml109
-rw-r--r--third_party/rust/serde_json/LICENSE-APACHE201
-rw-r--r--third_party/rust/serde_json/LICENSE-MIT23
-rw-r--r--third_party/rust/serde_json/README.md390
-rw-r--r--third_party/rust/serde_json/build.rs54
-rw-r--r--third_party/rust/serde_json/src/de.rs2606
-rw-r--r--third_party/rust/serde_json/src/error.rs445
-rw-r--r--third_party/rust/serde_json/src/features_check/error.rs1
-rw-r--r--third_party/rust/serde_json/src/features_check/mod.rs13
-rw-r--r--third_party/rust/serde_json/src/io/core.rs79
-rw-r--r--third_party/rust/serde_json/src/io/mod.rs20
-rw-r--r--third_party/rust/serde_json/src/iter.rs70
-rw-r--r--third_party/rust/serde_json/src/lexical/algorithm.rs193
-rw-r--r--third_party/rust/serde_json/src/lexical/bhcomp.rs218
-rw-r--r--third_party/rust/serde_json/src/lexical/bignum.rs33
-rw-r--r--third_party/rust/serde_json/src/lexical/cached.rs82
-rw-r--r--third_party/rust/serde_json/src/lexical/cached_float80.rs206
-rw-r--r--third_party/rust/serde_json/src/lexical/digit.rs15
-rw-r--r--third_party/rust/serde_json/src/lexical/errors.rs133
-rw-r--r--third_party/rust/serde_json/src/lexical/exponent.rs50
-rw-r--r--third_party/rust/serde_json/src/lexical/float.rs183
-rw-r--r--third_party/rust/serde_json/src/lexical/large_powers.rs9
-rw-r--r--third_party/rust/serde_json/src/lexical/large_powers32.rs183
-rw-r--r--third_party/rust/serde_json/src/lexical/large_powers64.rs625
-rw-r--r--third_party/rust/serde_json/src/lexical/math.rs886
-rw-r--r--third_party/rust/serde_json/src/lexical/mod.rs38
-rw-r--r--third_party/rust/serde_json/src/lexical/num.rs440
-rw-r--r--third_party/rust/serde_json/src/lexical/parse.rs83
-rw-r--r--third_party/rust/serde_json/src/lexical/rounding.rs231
-rw-r--r--third_party/rust/serde_json/src/lexical/shift.rs46
-rw-r--r--third_party/rust/serde_json/src/lexical/small_powers.rs70
-rw-r--r--third_party/rust/serde_json/src/lib.rs421
-rw-r--r--third_party/rust/serde_json/src/macros.rs303
-rw-r--r--third_party/rust/serde_json/src/map.rs940
-rw-r--r--third_party/rust/serde_json/src/number.rs745
-rw-r--r--third_party/rust/serde_json/src/raw.rs531
-rw-r--r--third_party/rust/serde_json/src/read.rs1004
-rw-r--r--third_party/rust/serde_json/src/ser.rs2179
-rw-r--r--third_party/rust/serde_json/src/value/de.rs1377
-rw-r--r--third_party/rust/serde_json/src/value/from.rs277
-rw-r--r--third_party/rust/serde_json/src/value/index.rs258
-rw-r--r--third_party/rust/serde_json/src/value/mod.rs987
-rw-r--r--third_party/rust/serde_json/src/value/partial_eq.rs95
-rw-r--r--third_party/rust/serde_json/src/value/ser.rs1018
-rw-r--r--third_party/rust/serde_json/tests/compiletest.rs7
-rw-r--r--third_party/rust/serde_json/tests/debug.rs81
-rw-r--r--third_party/rust/serde_json/tests/lexical.rs52
-rw-r--r--third_party/rust/serde_json/tests/lexical/algorithm.rs110
-rw-r--r--third_party/rust/serde_json/tests/lexical/exponent.rs54
-rw-r--r--third_party/rust/serde_json/tests/lexical/float.rs581
-rw-r--r--third_party/rust/serde_json/tests/lexical/math.rs211
-rw-r--r--third_party/rust/serde_json/tests/lexical/num.rs76
-rw-r--r--third_party/rust/serde_json/tests/lexical/parse.rs204
-rw-r--r--third_party/rust/serde_json/tests/lexical/rounding.rs316
-rw-r--r--third_party/rust/serde_json/tests/macros/mod.rs61
-rw-r--r--third_party/rust/serde_json/tests/map.rs47
-rw-r--r--third_party/rust/serde_json/tests/regression.rs3
-rw-r--r--third_party/rust/serde_json/tests/regression/issue520.rs20
-rw-r--r--third_party/rust/serde_json/tests/regression/issue795.rs59
-rw-r--r--third_party/rust/serde_json/tests/regression/issue845.rs74
-rw-r--r--third_party/rust/serde_json/tests/regression/issue953.rs9
-rw-r--r--third_party/rust/serde_json/tests/stream.rs183
-rw-r--r--third_party/rust/serde_json/tests/test.rs2387
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_colon.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_colon.stderr12
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_comma.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_comma.stderr13
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_value.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/missing_value.stderr12
-rw-r--r--third_party/rust/serde_json/tests/ui/not_found.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/not_found.stderr5
-rw-r--r--third_party/rust/serde_json/tests/ui/parse_expr.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/parse_expr.stderr11
-rw-r--r--third_party/rust/serde_json/tests/ui/parse_key.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/parse_key.stderr5
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr7
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr7
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_colon.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_colon.stderr7
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_comma.rs5
-rw-r--r--third_party/rust/serde_json/tests/ui/unexpected_comma.stderr7
85 files changed, 22588 insertions, 0 deletions
diff --git a/third_party/rust/serde_json/.cargo-checksum.json b/third_party/rust/serde_json/.cargo-checksum.json
new file mode 100644
index 0000000000..023247168d
--- /dev/null
+++ b/third_party/rust/serde_json/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"CONTRIBUTING.md":"f5270cafba66223a7b51ffc0d286075a17bb7cd88762fc80d333d3102629f4d8","Cargo.toml":"ef162b674028e85093b301f43972c272bc879f5f27f18011fe7ac620c4e9632a","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"96ca6cef9a39d6ba25578b28e5ae10b49fea2d6d4a00f088afc1d4656f087540","build.rs":"0dd97b63a07aa2d4bbb4c7d0e73b521da130769da9f49f28a7b63332781eb3de","src/de.rs":"5dba95fc6a564917289bf1e404d59c74f95772f22ec92cb91d55053b65e93032","src/error.rs":"abb92a9bf62cb7f47141a733a9fd66ec8c369615d7e6f633d3536fd2a5ac74a2","src/features_check/error.rs":"d7359f864afbfe105a38abea9f563dc423036ebc4c956a5695a4beef144dc7ec","src/features_check/mod.rs":"2209f8d5c46b50c8a3b8dc22338dcaf0135d192e8b05d2f456cbe6a73104e958","src/io/core.rs":"9a4146802391fd202a36bebbf3b14b715ae09d8828cbe8e06a01214c470ebf5c","src/io/mod.rs":"fd1ed5080495cab21117f6f7d3c2c9e3687cad0c69a0cd087b08a145a9e672da","src/iter.rs":"f832c469cd7999d26ba9b76baa69b257a212a7edb3dfdf9b1d1bb35e8da85fa9","src/lexical/algorithm.rs":"4fbeb1994049348d1fc388dd1a29e481f8abb8fe1e28bfebf50f3bbce5fa5fbe","src/lexical/bhcomp.rs":"b7c68d74c0055eb67ec2c1bcf27bbc28bef8f1bbc43db8eb94ba69892230add6","src/lexical/bignum.rs":"4230cde10dc8eae456a713cf90ec4e48dff4b1d0c542621ce7f00f39ade2645e","src/lexical/cached.rs":"0e127398691f8042c19cde209e7f4b0161f0f3150342430145929f711e6fdac8","src/lexical/cached_float80.rs":"0f8f74a22cb7d871322a9893bffd0255ca10bf9dffd13afb2462dd3d7f51805f","src/lexical/digit.rs":"a265b9072194a62a67dfc4df3c86d4213097cf3f82280d025e0012a5a262fd9d","src/lexical/errors.rs":"6bc993febceb7dd96ac1c8c5c53b5f5a30297016c0f813ed8ff8d7938d01534d","src/lexical/exponent.rs":"387e945b97dc7ba48a7091c50d228a0dde3a1c4145703d4ab9c31191a91693b0","src/lexical/float.rs":"fe356213c92a049f4bef2f58bc0e3a26866ca06b8c1d74d0f961c5b883852cad","src/lexical/large_powers.rs":"34537f5c701afce1ec2a1fd3c14950381b2e27c9ad74f002c91f3708e8da9ca5","src/lexical/large_powers32.rs":"d533037c6141e6671102aee490c9cdeaba81e667ddca781b2b99db2c455e4a1a","src/lexical/large_powers64.rs":"745dd7c0cbe499eec027ef586248881011d9df20c7efab7929c1807b59886ba1","src/lexical/math.rs":"27e22b724cdf990cdacd0ccfc3749e6e2eb7529d43ebf6e95b1999560b9e199a","src/lexical/mod.rs":"4b4c5228779c0f135a4cb018700e3bcd495da48b74421a86f6b8b304acdef924","src/lexical/num.rs":"cf705c62612e31d704f43d94a633ea1243c6befad7ef5792e2e881a7fd21e809","src/lexical/parse.rs":"c2bfac4c70a19938ced61e991f4ec606764887cf12bac1a0978b5b5318a56aac","src/lexical/rounding.rs":"697207248ba17b7f4965aedb11d276261ada5b06d9c6265d8fd6246664ff6e3c","src/lexical/shift.rs":"bc1ed053dd63d45ac9c35302f18de9f00d94027f28af4ab749c9248439de832a","src/lexical/small_powers.rs":"4608dd218b8002435db7e1ec79d2d0fef5f47ae257b93353326d52ecc80cccda","src/lib.rs":"61e0660b777318b649ada8e40583e668b80796b9a1b2013f77d0bdac3f8ec0eb","src/macros.rs":"c9f23156faec8d5216d72b6a97eebd768efb3f75870a6e2beed824308587b998","src/map.rs":"14fa16650b462ef138bebe1d18cb296b0e1ff404f12f2c212f72ed7c969b3a12","src/number.rs":"425f528c5ec2fcb4dd3c87a633d0b2cd505f7a305a40dffa1f022a643eda938b","src/raw.rs":"ee2b4ed085d8488e72c5a19791aae9de569d6c1381e9e64ff71afa03d5cf902a","src/read.rs":"49b4b1d067b6485cbded28fb961666ab5df82c36661af722dbae756efb6b2891","src/ser.rs":"566ae0b1860861ad70efd17fb0f6ae326e76d60453b6538c783a40d4ce616851","src/value/de.rs":"78f938d960e285f671f3b86ed173d598a815690a14512d6daa94dee43d3ce4cf","src/value/from.rs":"2dcfed837b040447a61eae50bd3938106b090f8a292206aea686022767006625","src/value/index.rs":"8a99d8d50f5674181ced22f6e81dc529eaecb01e543e30346e51fe42cb4b8a5f","src/value/mod.rs":"47ff472a2426a135c7acdf59c4c83c7b7ce986269f10ec31c809a2b35152beda","src/value/partial_eq.rs":"95de799d57f7f4310b64a9488c0a7286dee76dba4329cb69a96298a887e58586","src/value/ser.rs":"5f8eeff57e283527e1aabd89709ef8d6e006682be5929cbdb1d7d182fe185166","tests/compiletest.rs":"022a8e400ef813d7ea1875b944549cee5125f6a995dc33e93b48cba3e1b57bd1","tests/debug.rs":"a8451217c1e127ad6e653ef11e0513525ee350e1e37dd575758a8ee9301b28fb","tests/lexical.rs":"383e06283e15e1db7c6b818da3c84a3afa0059f6e9fc994b069919d81346ab91","tests/lexical/algorithm.rs":"da378df9ee24bfa033968d5c94e91b58e52c39bf6c825dec51c3eb7250cc5874","tests/lexical/exponent.rs":"26ea92abc654a6a88a8281552bca2f76ea1fa4c17d66a1dd6defe14f7d89b666","tests/lexical/float.rs":"0440f2d85c993bcccd925096d7f4136bf624ffd66b3c7ee565d158390685eb11","tests/lexical/math.rs":"4874be2103be5fbe8b8015354414df271ffa00fd815546fc077f15fb4d7a5a37","tests/lexical/num.rs":"6e650c40de85ed72ac06b6bf1487ba161f3824e26d827df6cfdf2bbdb8d05a05","tests/lexical/parse.rs":"17c73e0a59d462716d974b8dd23a291eb6efdc3a933248874e5eab7e7209d67f","tests/lexical/rounding.rs":"6c56e39ba534616c1b2146e8efa6eb57aed322e683bf23183cd32a61fae6447e","tests/macros/mod.rs":"93aa1d54af20bc2c55b6ae8db73c1414cda2626eb9fa7bd57b9d613a3c6e6a19","tests/map.rs":"dcc5212242e4e93703c4335d54f5603b0211b33d6fb5ab410bb630cda6d46b09","tests/regression.rs":"86731134bfb9bb693d9a4fc62393027de80a8bf031109ea6c7ea475b1ebdde8d","tests/regression/issue520.rs":"d146be3472db902b48127d65fe83aa9f698143aca9074c83cd1a9d5dd28e3ec3","tests/regression/issue795.rs":"582e2e7c68113f05a4b1d2cb556a2df7cc77f2ce8164a32c5cc58ae68abb60ec","tests/regression/issue845.rs":"8bd64588fc344e119d0e9e5e7604236e7c168c574b0692033f15278e216a6b9c","tests/regression/issue953.rs":"b2cddc761f5ca6639900c173765a8a5868528a896924e5e925db2696469208f7","tests/stream.rs":"c7d91014538ecd8f495b196d40e999ab2745f2e69fa2ff9e52521605dc6ce856","tests/test.rs":"cde57e6a0b2f23ca8b4ef83673955a0ba85da04db7875f47d37cc23773f32d28","tests/ui/missing_colon.rs":"d07e0c34d98eb43465f0a0310f2c0b5d5b0d26d243b352a1c6bbe6ad3b27eda9","tests/ui/missing_colon.stderr":"3732fd8f4e57b84efc07170cda5f9c5b2b17c707e23c1659222b5a46f652a8d8","tests/ui/missing_comma.rs":"b8a9662f99c3e6dd2b6417892c37640578ce91d3a8365bf10c1f686a3227aa87","tests/ui/missing_comma.stderr":"eae626cf93c97abd105066e624ca4e8cb096784413b9d2564cf9414a8492bc4d","tests/ui/missing_value.rs":"bca25d67127fb88e7c191c7b03af5a4ce8a9abb630f3d2e6a6c1e77e213dc9a4","tests/ui/missing_value.stderr":"b0df8add5cf74e5df30eedd3ca347e4862c04a01c54d802ff45392f2032065b1","tests/ui/not_found.rs":"d0a7adb309879ff65aee115b52cc33d36f4bad353cf97c4effc34a6128c2bee3","tests/ui/not_found.stderr":"359b751c0c21fab6d460daef4d5f73a265f7769c9b578f98ea3cb6cbf2387643","tests/ui/parse_expr.rs":"32e6d51f528db3d1ab0ed1e24765b865be393565c26f77413c5aa39d601ac563","tests/ui/parse_expr.stderr":"4fcd0a014fbce31c9266bab8527d6e6b6806a0e21d9e0275ce713137856073ce","tests/ui/parse_key.rs":"18829b2af320d5cf8a0a5cd3aaf84c7e92cc874651c30e45a3acafb76c2d8b93","tests/ui/parse_key.stderr":"fcb44e060b804a4762b7291e128c41d7010ffa8ab820b8828fd13fbe6d405ca6","tests/ui/unexpected_after_array_element.rs":"a343fc3104431720bdfcf330bcc3cfcd98c8dec3e951133b495242478b0b7eb3","tests/ui/unexpected_after_array_element.stderr":"8df615998fa3057bb9ed865981a35cdbb771625337048f0ad3fba7734e607adf","tests/ui/unexpected_after_map_entry.rs":"6e3bd2def435ca610e346bbc75cdbaf61963eb2ef1885bb5f76781ba1fac37ef","tests/ui/unexpected_after_map_entry.stderr":"b1985c89075ab48b2158bd1705ed766d37854b3d4620ab257cc8bc319d224f17","tests/ui/unexpected_colon.rs":"a313cff3fed4be4c33f1eda5d0c5c98147fb835a56d36470d9f367352c1d61ef","tests/ui/unexpected_colon.stderr":"b2288742fa6a4a7eb65d2ae899bcfed8795b57bd04958da227d60928a8df26c5","tests/ui/unexpected_comma.rs":"55a8b684bde1ce905837cce719fd457d8898b61cebc27e5b420d05cb6be97256","tests/ui/unexpected_comma.stderr":"4c103ca63ff15e2ca659242cc0eae0612bf050e7580da62f1cf50de8082aa7dc"},"package":"877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"} \ No newline at end of file
diff --git a/third_party/rust/serde_json/CONTRIBUTING.md b/third_party/rust/serde_json/CONTRIBUTING.md
new file mode 100644
index 0000000000..26e15783a1
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/Cargo.toml b/third_party/rust/serde_json/Cargo.toml
new file mode 100644
index 0000000000..08c94aacbd
--- /dev/null
+++ b/third_party/rust/serde_json/Cargo.toml
@@ -0,0 +1,109 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2018"
+rust-version = "1.36"
+name = "serde_json"
+version = "1.0.91"
+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",
+]
+categories = [
+ "encoding",
+ "parser-implementations",
+ "no-std",
+]
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/serde-rs/json"
+
+[package.metadata.docs.rs]
+features = [
+ "raw_value",
+ "unbounded_depth",
+]
+targets = ["x86_64-unknown-linux-gnu"]
+rustdoc-args = [
+ "--cfg",
+ "docsrs",
+]
+
+[package.metadata.playground]
+features = ["raw_value"]
+
+[lib]
+doc-scrape-examples = false
+
+[dependencies.indexmap]
+version = "1.5.2"
+features = ["std"]
+optional = true
+
+[dependencies.itoa]
+version = "1.0"
+
+[dependencies.ryu]
+version = "1.0"
+
+[dependencies.serde]
+version = "1.0.100"
+default-features = false
+
+[dev-dependencies.automod]
+version = "1.0"
+
+[dev-dependencies.indoc]
+version = "1.0"
+
+[dev-dependencies.ref-cast]
+version = "1.0"
+
+[dev-dependencies.rustversion]
+version = "1.0"
+
+[dev-dependencies.serde]
+version = "1.0.100"
+features = ["derive"]
+
+[dev-dependencies.serde_bytes]
+version = "0.11"
+
+[dev-dependencies.serde_derive]
+version = "1.0"
+
+[dev-dependencies.serde_stacker]
+version = "0.1"
+
+[dev-dependencies.trybuild]
+version = "1.0.49"
+features = ["diff"]
+
+[features]
+alloc = ["serde/alloc"]
+arbitrary_precision = []
+default = ["std"]
+float_roundtrip = []
+preserve_order = [
+ "indexmap",
+ "std",
+]
+raw_value = []
+std = ["serde/std"]
+unbounded_depth = []
diff --git a/third_party/rust/serde_json/LICENSE-APACHE b/third_party/rust/serde_json/LICENSE-APACHE
new file mode 100644
index 0000000000..16fe87b06e
--- /dev/null
+++ b/third_party/rust/serde_json/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/third_party/rust/serde_json/LICENSE-MIT b/third_party/rust/serde_json/LICENSE-MIT
new file mode 100644
index 0000000000..31aa79387f
--- /dev/null
+++ b/third_party/rust/serde_json/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/third_party/rust/serde_json/README.md b/third_party/rust/serde_json/README.md
new file mode 100644
index 0000000000..50b5d458d0
--- /dev/null
+++ b/third_party/rust/serde_json/README.md
@@ -0,0 +1,390 @@
+# Serde JSON &emsp; [![Build Status]][actions] [![Latest Version]][crates.io] [![Rustc Version 1.36+]][rustc]
+
+[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/json/ci.yml?branch=master
+[actions]: 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.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.**
+
+---
+
+```toml
+[dependencies]
+serde_json = "1.0"
+```
+
+You may be looking for:
+
+- [JSON API documentation](https://docs.serde.rs/serde_json/)
+- [Serde API documentation](https://docs.serde.rs/serde/)
+- [Detailed documentation about Serde](https://serde.rs/)
+- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
+- [Release notes](https://github.com/serde-rs/json/releases)
+
+JSON is a ubiquitous open-standard format that uses human-readable text to
+transmit data objects consisting of key-value pairs.
+
+```json
+{
+ "name": "John Doe",
+ "age": 43,
+ "address": {
+ "street": "10 Downing Street",
+ "city": "London"
+ },
+ "phones": [
+ "+44 1234567",
+ "+44 2345678"
+ ]
+}
+```
+
+There are three common ways that you might find yourself needing to work with
+JSON data in Rust.
+
+ - **As text data.** An unprocessed string of JSON data that you receive on an
+ HTTP endpoint, read from a file, or prepare to send to a remote server.
+ - **As an untyped or loosely typed representation.** Maybe you want to check
+ that some JSON data is valid before passing it on, but without knowing the
+ structure of what it contains. Or you want to do very basic manipulations
+ like insert a key in a particular spot.
+ - **As a strongly typed Rust data structure.** When you expect all or most of
+ your data to conform to a particular structure and want to get real work done
+ without JSON's loosey-goosey nature tripping you up.
+
+Serde JSON provides efficient, flexible, safe ways of converting data between
+each of these representations.
+
+## Operating on untyped JSON values
+
+Any valid JSON data can be manipulated in the following recursive enum
+representation. This data structure is [`serde_json::Value`][value].
+
+```rust
+enum Value {
+ Null,
+ Bool(bool),
+ Number(Number),
+ String(String),
+ Array(Vec<Value>),
+ Object(Map<String, Value>),
+}
+```
+
+A string of JSON data can be parsed into a `serde_json::Value` by the
+[`serde_json::from_str`][from_str] function. There is also
+[`from_slice`][from_slice] for parsing from a byte slice &[u8] and
+[`from_reader`][from_reader] for parsing from any `io::Read` like a File or a
+TCP stream.
+
+<div align="right">
+<a href="https://play.rust-lang.org/?edition=2018&gist=d69d8e3156d4bb81c4461b60b772ab72" target="_blank">
+<img align="center" width="85" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/runtab.png">
+</a>
+</div>
+
+```rust
+use serde_json::{Result, Value};
+
+fn untyped_example() -> Result<()> {
+ // Some JSON input data as a &str. Maybe this comes from the user.
+ let data = r#"
+ {
+ "name": "John Doe",
+ "age": 43,
+ "phones": [
+ "+44 1234567",
+ "+44 2345678"
+ ]
+ }"#;
+
+ // Parse the string of data into serde_json::Value.
+ let v: Value = serde_json::from_str(data)?;
+
+ // Access parts of the data by indexing with square brackets.
+ println!("Please call {} at the number {}", v["name"], v["phones"][0]);
+
+ Ok(())
+}
+```
+
+The result of square bracket indexing like `v["name"]` is a borrow of the data
+at that index, so the type is `&Value`. A JSON map can be indexed with string
+keys, while a JSON array can be indexed with integer keys. If the type of the
+data is not right for the type with which it is being indexed, or if a map does
+not contain the key being indexed, or if the index into a vector is out of
+bounds, the returned element is `Value::Null`.
+
+When a `Value` is printed, it is printed as a JSON string. So in the code above,
+the output looks like `Please call "John Doe" at the number "+44 1234567"`. The
+quotation marks appear because `v["name"]` is a `&Value` containing a JSON
+string and its JSON representation is `"John Doe"`. Printing as a plain string
+without quotation marks involves converting from a JSON string to a Rust string
+with [`as_str()`] or avoiding the use of `Value` as described in the following
+section.
+
+[`as_str()`]: https://docs.serde.rs/serde_json/enum.Value.html#method.as_str
+
+The `Value` representation is sufficient for very basic tasks but can be tedious
+to work with for anything more significant. Error handling is verbose to
+implement correctly, for example imagine trying to detect the presence of
+unrecognized fields in the input data. The compiler is powerless to help you
+when you make a mistake, for example imagine typoing `v["name"]` as `v["nmae"]`
+in one of the dozens of places it is used in your code.
+
+## Parsing JSON as strongly typed data structures
+
+Serde provides a powerful way of mapping JSON data into Rust data structures
+largely automatically.
+
+<div align="right">
+<a href="https://play.rust-lang.org/?edition=2018&gist=15cfab66d38ff8a15a9cf1d8d897ac68" target="_blank">
+<img align="center" width="85" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/runtab.png">
+</a>
+</div>
+
+```rust
+use serde::{Deserialize, Serialize};
+use serde_json::Result;
+
+#[derive(Serialize, Deserialize)]
+struct Person {
+ name: String,
+ age: u8,
+ phones: Vec<String>,
+}
+
+fn typed_example() -> Result<()> {
+ // Some JSON input data as a &str. Maybe this comes from the user.
+ let data = r#"
+ {
+ "name": "John Doe",
+ "age": 43,
+ "phones": [
+ "+44 1234567",
+ "+44 2345678"
+ ]
+ }"#;
+
+ // Parse the string of data into a Person object. This is exactly the
+ // same function as the one that produced serde_json::Value above, but
+ // now we are asking it for a Person as output.
+ let p: Person = serde_json::from_str(data)?;
+
+ // Do things just like with any other Rust data structure.
+ println!("Please call {} at the number {}", p.name, p.phones[0]);
+
+ Ok(())
+}
+```
+
+This is the same `serde_json::from_str` function as before, but this time we
+assign the return value to a variable of type `Person` so Serde will
+automatically interpret the input data as a `Person` and produce informative
+error messages if the layout does not conform to what a `Person` is expected to
+look like.
+
+Any type that implements Serde's `Deserialize` trait can be deserialized this
+way. This includes built-in Rust standard library types like `Vec<T>` and
+`HashMap<K, V>`, as well as any structs or enums annotated with
+`#[derive(Deserialize)]`.
+
+Once we have `p` of type `Person`, our IDE and the Rust compiler can help us use
+it correctly like they do for any other Rust code. The IDE can autocomplete
+field names to prevent typos, which was impossible in the `serde_json::Value`
+representation. And the Rust compiler can check that when we write
+`p.phones[0]`, then `p.phones` is guaranteed to be a `Vec<String>` so indexing
+into it makes sense and produces a `String`.
+
+The necessary setup for using Serde's derive macros is explained on the *[Using
+derive]* page of the Serde site.
+
+[Using derive]: https://serde.rs/derive.html
+
+## Constructing JSON values
+
+Serde JSON provides a [`json!` macro][macro] to build `serde_json::Value`
+objects with very natural JSON syntax.
+
+<div align="right">
+<a href="https://play.rust-lang.org/?edition=2018&gist=6ccafad431d72b62e77cc34c8e879b24" target="_blank">
+<img align="center" width="85" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/runtab.png">
+</a>
+</div>
+
+```rust
+use serde_json::json;
+
+fn main() {
+ // The type of `john` is `serde_json::Value`
+ let john = json!({
+ "name": "John Doe",
+ "age": 43,
+ "phones": [
+ "+44 1234567",
+ "+44 2345678"
+ ]
+ });
+
+ println!("first phone number: {}", john["phones"][0]);
+
+ // Convert to a string of JSON and print it out
+ println!("{}", john.to_string());
+}
+```
+
+The `Value::to_string()` function converts a `serde_json::Value` into a `String`
+of JSON text.
+
+One neat thing about the `json!` macro is that variables and expressions can be
+interpolated directly into the JSON value as you are building it. Serde will
+check at compile time that the value you are interpolating is able to be
+represented as JSON.
+
+<div align="right">
+<a href="https://play.rust-lang.org/?edition=2018&gist=f9101a6e61dfc9e02c6a67f315ed24f2" target="_blank">
+<img align="center" width="85" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/runtab.png">
+</a>
+</div>
+
+```rust
+let full_name = "John Doe";
+let age_last_year = 42;
+
+// The type of `john` is `serde_json::Value`
+let john = json!({
+ "name": full_name,
+ "age": age_last_year + 1,
+ "phones": [
+ format!("+44 {}", random_phone())
+ ]
+});
+```
+
+This is amazingly convenient, but we have the problem we had before with
+`Value`: the IDE and Rust compiler cannot help us if we get it wrong. Serde JSON
+provides a better way of serializing strongly-typed data structures into JSON
+text.
+
+## Creating JSON by serializing data structures
+
+A data structure can be converted to a JSON string by
+[`serde_json::to_string`][to_string]. There is also
+[`serde_json::to_vec`][to_vec] which serializes to a `Vec<u8>` and
+[`serde_json::to_writer`][to_writer] which serializes to any `io::Write`
+such as a File or a TCP stream.
+
+<div align="right">
+<a href="https://play.rust-lang.org/?edition=2018&gist=3472242a08ed2ff88a944f2a2283b0ee" target="_blank">
+<img align="center" width="85" src="https://raw.githubusercontent.com/serde-rs/serde-rs.github.io/master/img/runtab.png">
+</a>
+</div>
+
+```rust
+use serde::{Deserialize, Serialize};
+use serde_json::Result;
+
+#[derive(Serialize, Deserialize)]
+struct Address {
+ street: String,
+ city: String,
+}
+
+fn print_an_address() -> Result<()> {
+ // Some data structure.
+ let address = Address {
+ street: "10 Downing Street".to_owned(),
+ city: "London".to_owned(),
+ };
+
+ // Serialize it to a JSON string.
+ let j = serde_json::to_string(&address)?;
+
+ // Print, write to a file, or send to an HTTP server.
+ println!("{}", j);
+
+ Ok(())
+}
+```
+
+Any type that implements Serde's `Serialize` trait can be serialized this way.
+This includes built-in Rust standard library types like `Vec<T>` and `HashMap<K,
+V>`, as well as any structs or enums annotated with `#[derive(Serialize)]`.
+
+## Performance
+
+It is fast. You should expect in the ballpark of 500 to 1000 megabytes per
+second deserialization and 600 to 900 megabytes per second serialization,
+depending on the characteristics of your data. This is competitive with the
+fastest C and C++ JSON libraries or even 30% faster for many use cases.
+Benchmarks live in the [serde-rs/json-benchmark] repo.
+
+[serde-rs/json-benchmark]: https://github.com/serde-rs/json-benchmark
+
+## Getting help
+
+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 [#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
+[discourse]: https://users.rust-lang.org
+
+## No-std support
+
+As long as there is a memory allocator, it is possible to use serde_json without
+the rest of the Rust standard library. Disable the default "std" feature and
+enable the "alloc" feature:
+
+```toml
+[dependencies]
+serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
+```
+
+For JSON support in Serde without a memory allocator, please see the
+[`serde-json-core`] crate.
+
+[`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core
+
+[value]: https://docs.serde.rs/serde_json/value/enum.Value.html
+[from_str]: https://docs.serde.rs/serde_json/de/fn.from_str.html
+[from_slice]: https://docs.serde.rs/serde_json/de/fn.from_slice.html
+[from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html
+[to_string]: https://docs.serde.rs/serde_json/ser/fn.to_string.html
+[to_vec]: https://docs.serde.rs/serde_json/ser/fn.to_vec.html
+[to_writer]: https://docs.serde.rs/serde_json/ser/fn.to_writer.html
+[macro]: https://docs.serde.rs/serde_json/macro.json.html
+
+<br>
+
+#### License
+
+<sup>
+Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
+2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
+</sup>
+
+<br>
+
+<sub>
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
+be dual licensed as above, without any additional terms or conditions.
+</sub>
diff --git a/third_party/rust/serde_json/build.rs b/third_party/rust/serde_json/build.rs
new file mode 100644
index 0000000000..0e12602e46
--- /dev/null
+++ b/third_party/rust/serde_json/build.rs
@@ -0,0 +1,54 @@
+use std::env;
+use std::process::Command;
+use std::str::{self, FromStr};
+
+fn main() {
+ println!("cargo:rerun-if-changed=build.rs");
+
+ // Decide ideal limb width for arithmetic in the float parser. Refer to
+ // src/lexical/math.rs for where this has an effect.
+ let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
+ match target_arch.as_str() {
+ "aarch64" | "mips64" | "powerpc64" | "x86_64" => {
+ println!("cargo:rustc-cfg=limb_width_64");
+ }
+ _ => {
+ println!("cargo:rustc-cfg=limb_width_32");
+ }
+ }
+
+ let minor = match rustc_minor_version() {
+ Some(minor) => minor,
+ None => return,
+ };
+
+ // BTreeMap::get_key_value
+ // https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html#additions-to-the-standard-library
+ if minor < 40 {
+ println!("cargo:rustc-cfg=no_btreemap_get_key_value");
+ }
+
+ // BTreeMap::remove_entry
+ // https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#library-changes
+ 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> {
+ let rustc = env::var_os("RUSTC")?;
+ let output = Command::new(rustc).arg("--version").output().ok()?;
+ let version = str::from_utf8(&output.stdout).ok()?;
+ let mut pieces = version.split('.');
+ if pieces.next() != Some("rustc 1") {
+ return None;
+ }
+ let next = pieces.next()?;
+ u32::from_str(next).ok()
+}
diff --git a/third_party/rust/serde_json/src/de.rs b/third_party/rust/serde_json/src/de.rs
new file mode 100644
index 0000000000..88d0f2624d
--- /dev/null
+++ b/third_party/rust/serde_json/src/de.rs
@@ -0,0 +1,2606 @@
+//! Deserialize JSON data to a Rust data structure.
+
+use crate::error::{Error, ErrorCode, Result};
+#[cfg(feature = "float_roundtrip")]
+use crate::lexical;
+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;
+
+#[cfg(feature = "arbitrary_precision")]
+use crate::number::NumberDeserializer;
+
+pub use crate::read::{Read, SliceRead, StrRead};
+
+#[cfg(feature = "std")]
+pub use crate::read::IoRead;
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// A structure that deserializes JSON into Rust values.
+pub struct Deserializer<R> {
+ read: R,
+ scratch: Vec<u8>,
+ remaining_depth: u8,
+ #[cfg(feature = "float_roundtrip")]
+ single_precision: bool,
+ #[cfg(feature = "unbounded_depth")]
+ disable_recursion_limit: bool,
+}
+
+impl<'de, R> Deserializer<R>
+where
+ R: read::Read<'de>,
+{
+ /// Create a JSON deserializer from one of the possible serde_json input
+ /// sources.
+ ///
+ /// Typically it is more convenient to use one of these methods instead:
+ ///
+ /// - Deserializer::from_str
+ /// - Deserializer::from_slice
+ /// - Deserializer::from_reader
+ pub fn new(read: R) -> Self {
+ Deserializer {
+ read,
+ scratch: Vec::new(),
+ remaining_depth: 128,
+ #[cfg(feature = "float_roundtrip")]
+ single_precision: false,
+ #[cfg(feature = "unbounded_depth")]
+ disable_recursion_limit: false,
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<R> Deserializer<read::IoRead<R>>
+where
+ R: crate::io::Read,
+{
+ /// Creates a JSON deserializer from an `io::Read`.
+ ///
+ /// Reader-based deserializers do not support deserializing borrowed types
+ /// like `&str`, since the `std::io::Read` trait has no non-copying methods
+ /// -- everything it does involves copying bytes out of the data source.
+ pub fn from_reader(reader: R) -> Self {
+ Deserializer::new(read::IoRead::new(reader))
+ }
+}
+
+impl<'a> Deserializer<read::SliceRead<'a>> {
+ /// Creates a JSON deserializer from a `&[u8]`.
+ pub fn from_slice(bytes: &'a [u8]) -> Self {
+ Deserializer::new(read::SliceRead::new(bytes))
+ }
+}
+
+impl<'a> Deserializer<read::StrRead<'a>> {
+ /// Creates a JSON deserializer from a `&str`.
+ pub fn from_str(s: &'a str) -> Self {
+ Deserializer::new(read::StrRead::new(s))
+ }
+}
+
+macro_rules! overflow {
+ ($a:ident * 10 + $b:ident, $c:expr) => {
+ match $c {
+ c => $a >= c / 10 && ($a > c / 10 || $b > c % 10),
+ }
+ };
+}
+
+pub(crate) enum ParserNumber {
+ F64(f64),
+ U64(u64),
+ I64(i64),
+ #[cfg(feature = "arbitrary_precision")]
+ String(String),
+}
+
+impl ParserNumber {
+ fn visit<'de, V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ match self {
+ ParserNumber::F64(x) => visitor.visit_f64(x),
+ ParserNumber::U64(x) => visitor.visit_u64(x),
+ ParserNumber::I64(x) => visitor.visit_i64(x),
+ #[cfg(feature = "arbitrary_precision")]
+ ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }),
+ }
+ }
+
+ fn invalid_type(self, exp: &dyn Expected) -> Error {
+ match self {
+ ParserNumber::F64(x) => de::Error::invalid_type(Unexpected::Float(x), exp),
+ ParserNumber::U64(x) => de::Error::invalid_type(Unexpected::Unsigned(x), exp),
+ ParserNumber::I64(x) => de::Error::invalid_type(Unexpected::Signed(x), exp),
+ #[cfg(feature = "arbitrary_precision")]
+ ParserNumber::String(_) => de::Error::invalid_type(Unexpected::Other("number"), exp),
+ }
+ }
+}
+
+impl<'de, R: Read<'de>> Deserializer<R> {
+ /// The `Deserializer::end` method should be called after a value has been fully deserialized.
+ /// This allows the `Deserializer` to validate that the input stream is at the end or that it
+ /// only has trailing whitespace.
+ pub fn end(&mut self) -> Result<()> {
+ match tri!(self.parse_whitespace()) {
+ Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
+ None => Ok(()),
+ }
+ }
+
+ /// Turn a JSON deserializer into an iterator over values of type T.
+ pub fn into_iter<T>(self) -> StreamDeserializer<'de, R, T>
+ where
+ T: de::Deserialize<'de>,
+ {
+ // This cannot be an implementation of std::iter::IntoIterator because
+ // we need the caller to choose what T is.
+ let offset = self.read.byte_offset();
+ StreamDeserializer {
+ de: self,
+ offset,
+ failed: false,
+ output: PhantomData,
+ lifetime: PhantomData,
+ }
+ }
+
+ /// Parse arbitrarily deep JSON structures without any consideration for
+ /// overflowing the stack.
+ ///
+ /// You will want to provide some other way to protect against stack
+ /// overflows, such as by wrapping your Deserializer in the dynamically
+ /// growing stack adapter provided by the serde_stacker crate. Additionally
+ /// you will need to be careful around other recursive operations on the
+ /// parsed result which may overflow the stack after deserialization has
+ /// completed, including, but not limited to, Display and Debug and Drop
+ /// impls.
+ ///
+ /// *This method is only available if serde_json is built with the
+ /// `"unbounded_depth"` feature.*
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde::Deserialize;
+ /// use serde_json::Value;
+ ///
+ /// fn main() {
+ /// let mut json = String::new();
+ /// for _ in 0..10000 {
+ /// json = format!("[{}]", json);
+ /// }
+ ///
+ /// let mut deserializer = serde_json::Deserializer::from_str(&json);
+ /// deserializer.disable_recursion_limit();
+ /// let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
+ /// let value = Value::deserialize(deserializer).unwrap();
+ ///
+ /// carefully_drop_nested_arrays(value);
+ /// }
+ ///
+ /// fn carefully_drop_nested_arrays(value: Value) {
+ /// let mut stack = vec![value];
+ /// while let Some(value) = stack.pop() {
+ /// if let Value::Array(array) = value {
+ /// stack.extend(array);
+ /// }
+ /// }
+ /// }
+ /// ```
+ #[cfg(feature = "unbounded_depth")]
+ #[cfg_attr(docsrs, doc(cfg(feature = "unbounded_depth")))]
+ pub fn disable_recursion_limit(&mut self) {
+ self.disable_recursion_limit = true;
+ }
+
+ fn peek(&mut self) -> Result<Option<u8>> {
+ self.read.peek()
+ }
+
+ fn peek_or_null(&mut self) -> Result<u8> {
+ Ok(tri!(self.peek()).unwrap_or(b'\x00'))
+ }
+
+ fn eat_char(&mut self) {
+ self.read.discard();
+ }
+
+ fn next_char(&mut self) -> Result<Option<u8>> {
+ self.read.next()
+ }
+
+ fn next_char_or_null(&mut self) -> Result<u8> {
+ Ok(tri!(self.next_char()).unwrap_or(b'\x00'))
+ }
+
+ /// Error caused by a byte from next_char().
+ #[cold]
+ fn error(&self, reason: ErrorCode) -> Error {
+ let position = self.read.position();
+ Error::syntax(reason, position.line, position.column)
+ }
+
+ /// Error caused by a byte from peek().
+ #[cold]
+ fn peek_error(&self, reason: ErrorCode) -> Error {
+ let position = self.read.peek_position();
+ Error::syntax(reason, position.line, position.column)
+ }
+
+ /// Returns the first non-whitespace byte without consuming it, or `None` if
+ /// EOF is encountered.
+ fn parse_whitespace(&mut self) -> Result<Option<u8>> {
+ loop {
+ match tri!(self.peek()) {
+ Some(b' ') | Some(b'\n') | Some(b'\t') | Some(b'\r') => {
+ self.eat_char();
+ }
+ other => {
+ return Ok(other);
+ }
+ }
+ }
+ }
+
+ #[cold]
+ fn peek_invalid_type(&mut self, exp: &dyn Expected) -> Error {
+ let err = match self.peek_or_null().unwrap_or(b'\x00') {
+ b'n' => {
+ self.eat_char();
+ if let Err(err) = self.parse_ident(b"ull") {
+ return err;
+ }
+ de::Error::invalid_type(Unexpected::Unit, exp)
+ }
+ b't' => {
+ self.eat_char();
+ if let Err(err) = self.parse_ident(b"rue") {
+ return err;
+ }
+ de::Error::invalid_type(Unexpected::Bool(true), exp)
+ }
+ b'f' => {
+ self.eat_char();
+ if let Err(err) = self.parse_ident(b"alse") {
+ return err;
+ }
+ de::Error::invalid_type(Unexpected::Bool(false), exp)
+ }
+ b'-' => {
+ self.eat_char();
+ match self.parse_any_number(false) {
+ Ok(n) => n.invalid_type(exp),
+ Err(err) => return err,
+ }
+ }
+ b'0'..=b'9' => match self.parse_any_number(true) {
+ Ok(n) => n.invalid_type(exp),
+ Err(err) => return err,
+ },
+ b'"' => {
+ self.eat_char();
+ self.scratch.clear();
+ match self.read.parse_str(&mut self.scratch) {
+ Ok(s) => de::Error::invalid_type(Unexpected::Str(&s), exp),
+ Err(err) => return err,
+ }
+ }
+ b'[' => de::Error::invalid_type(Unexpected::Seq, exp),
+ b'{' => de::Error::invalid_type(Unexpected::Map, exp),
+ _ => self.peek_error(ErrorCode::ExpectedSomeValue),
+ };
+
+ self.fix_position(err)
+ }
+
+ fn deserialize_number<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'-' => {
+ self.eat_char();
+ tri!(self.parse_integer(false)).visit(visitor)
+ }
+ b'0'..=b'9' => tri!(self.parse_integer(true)).visit(visitor),
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn scan_integer128(&mut self, buf: &mut String) -> Result<()> {
+ match tri!(self.next_char_or_null()) {
+ b'0' => {
+ buf.push('0');
+ // There can be only one leading '0'.
+ match tri!(self.peek_or_null()) {
+ b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
+ _ => Ok(()),
+ }
+ }
+ c @ b'1'..=b'9' => {
+ buf.push(c as char);
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ buf.push(c as char);
+ }
+ Ok(())
+ }
+ _ => Err(self.error(ErrorCode::InvalidNumber)),
+ }
+ }
+
+ #[cold]
+ fn fix_position(&self, err: Error) -> Error {
+ err.fix_position(move |code| self.error(code))
+ }
+
+ fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
+ for expected in ident {
+ match tri!(self.next_char()) {
+ None => {
+ return Err(self.error(ErrorCode::EofWhileParsingValue));
+ }
+ Some(next) => {
+ if next != *expected {
+ return Err(self.error(ErrorCode::ExpectedSomeIdent));
+ }
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
+ let next = match tri!(self.next_char()) {
+ Some(b) => b,
+ None => {
+ return Err(self.error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ match next {
+ b'0' => {
+ // There can be only one leading '0'.
+ match tri!(self.peek_or_null()) {
+ b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
+ _ => self.parse_number(positive, 0),
+ }
+ }
+ c @ b'1'..=b'9' => {
+ let mut significand = (c - b'0') as u64;
+
+ loop {
+ match tri!(self.peek_or_null()) {
+ c @ b'0'..=b'9' => {
+ let digit = (c - b'0') as u64;
+
+ // We need to be careful with overflow. If we can,
+ // try to keep the number as a `u64` until we grow
+ // too large. At that point, switch to parsing the
+ // value as a `f64`.
+ if overflow!(significand * 10 + digit, u64::max_value()) {
+ return Ok(ParserNumber::F64(tri!(
+ self.parse_long_integer(positive, significand),
+ )));
+ }
+
+ self.eat_char();
+ significand = significand * 10 + digit;
+ }
+ _ => {
+ return self.parse_number(positive, significand);
+ }
+ }
+ }
+ }
+ _ => Err(self.error(ErrorCode::InvalidNumber)),
+ }
+ }
+
+ fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> {
+ Ok(match tri!(self.peek_or_null()) {
+ b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))),
+ b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))),
+ _ => {
+ if positive {
+ ParserNumber::U64(significand)
+ } else {
+ let neg = (significand as i64).wrapping_neg();
+
+ // Convert into a float if we underflow, or on `-0`.
+ if neg >= 0 {
+ ParserNumber::F64(-(significand as f64))
+ } else {
+ ParserNumber::I64(neg)
+ }
+ }
+ }
+ })
+ }
+
+ fn parse_decimal(
+ &mut self,
+ positive: bool,
+ mut significand: u64,
+ exponent_before_decimal_point: i32,
+ ) -> Result<f64> {
+ self.eat_char();
+
+ let mut exponent_after_decimal_point = 0;
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ let digit = (c - b'0') as u64;
+
+ if overflow!(significand * 10 + digit, u64::max_value()) {
+ let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
+ return self.parse_decimal_overflow(positive, significand, exponent);
+ }
+
+ self.eat_char();
+ significand = significand * 10 + digit;
+ exponent_after_decimal_point -= 1;
+ }
+
+ // Error if there is not at least one digit after the decimal point.
+ if exponent_after_decimal_point == 0 {
+ match tri!(self.peek()) {
+ Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
+ None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
+ match tri!(self.peek_or_null()) {
+ b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
+ _ => self.f64_from_parts(positive, significand, exponent),
+ }
+ }
+
+ fn parse_exponent(
+ &mut self,
+ positive: bool,
+ significand: u64,
+ starting_exp: i32,
+ ) -> Result<f64> {
+ self.eat_char();
+
+ let positive_exp = match tri!(self.peek_or_null()) {
+ b'+' => {
+ self.eat_char();
+ true
+ }
+ b'-' => {
+ self.eat_char();
+ false
+ }
+ _ => true,
+ };
+
+ let next = match tri!(self.next_char()) {
+ Some(b) => b,
+ None => {
+ return Err(self.error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ // Make sure a digit follows the exponent place.
+ let mut exp = match next {
+ c @ b'0'..=b'9' => (c - b'0') as i32,
+ _ => {
+ return Err(self.error(ErrorCode::InvalidNumber));
+ }
+ };
+
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ let digit = (c - b'0') as i32;
+
+ if overflow!(exp * 10 + digit, i32::max_value()) {
+ let zero_significand = significand == 0;
+ return self.parse_exponent_overflow(positive, zero_significand, positive_exp);
+ }
+
+ exp = exp * 10 + digit;
+ }
+
+ let final_exp = if positive_exp {
+ starting_exp.saturating_add(exp)
+ } else {
+ starting_exp.saturating_sub(exp)
+ };
+
+ self.f64_from_parts(positive, significand, final_exp)
+ }
+
+ #[cfg(feature = "float_roundtrip")]
+ fn f64_from_parts(&mut self, positive: bool, significand: u64, exponent: i32) -> Result<f64> {
+ let f = if self.single_precision {
+ lexical::parse_concise_float::<f32>(significand, exponent) as f64
+ } else {
+ lexical::parse_concise_float::<f64>(significand, exponent)
+ };
+
+ if f.is_infinite() {
+ Err(self.error(ErrorCode::NumberOutOfRange))
+ } else {
+ Ok(if positive { f } else { -f })
+ }
+ }
+
+ #[cfg(not(feature = "float_roundtrip"))]
+ fn f64_from_parts(
+ &mut self,
+ positive: bool,
+ significand: u64,
+ mut exponent: i32,
+ ) -> Result<f64> {
+ let mut f = significand as f64;
+ loop {
+ match POW10.get(exponent.wrapping_abs() as usize) {
+ Some(&pow) => {
+ if exponent >= 0 {
+ f *= pow;
+ if f.is_infinite() {
+ return Err(self.error(ErrorCode::NumberOutOfRange));
+ }
+ } else {
+ f /= pow;
+ }
+ break;
+ }
+ None => {
+ if f == 0.0 {
+ break;
+ }
+ if exponent >= 0 {
+ return Err(self.error(ErrorCode::NumberOutOfRange));
+ }
+ f /= 1e308;
+ exponent += 308;
+ }
+ }
+ }
+ Ok(if positive { f } else { -f })
+ }
+
+ #[cfg(feature = "float_roundtrip")]
+ #[cold]
+ #[inline(never)]
+ fn parse_long_integer(&mut self, positive: bool, partial_significand: u64) -> Result<f64> {
+ // To deserialize floats we'll first push the integer and fraction
+ // parts, both as byte strings, into the scratch buffer and then feed
+ // both slices to lexical's parser. For example if the input is
+ // `12.34e5` we'll push b"1234" into scratch and then pass b"12" and
+ // b"34" to lexical. `integer_end` will be used to track where to split
+ // the scratch buffer.
+ //
+ // Note that lexical expects the integer part to contain *no* leading
+ // zeroes and the fraction part to contain *no* trailing zeroes. The
+ // first requirement is already handled by the integer parsing logic.
+ // The second requirement will be enforced just before passing the
+ // slices to lexical in f64_long_from_parts.
+ self.scratch.clear();
+ self.scratch
+ .extend_from_slice(itoa::Buffer::new().format(partial_significand).as_bytes());
+
+ loop {
+ match tri!(self.peek_or_null()) {
+ c @ b'0'..=b'9' => {
+ self.scratch.push(c);
+ self.eat_char();
+ }
+ b'.' => {
+ self.eat_char();
+ return self.parse_long_decimal(positive, self.scratch.len());
+ }
+ b'e' | b'E' => {
+ return self.parse_long_exponent(positive, self.scratch.len());
+ }
+ _ => {
+ return self.f64_long_from_parts(positive, self.scratch.len(), 0);
+ }
+ }
+ }
+ }
+
+ #[cfg(not(feature = "float_roundtrip"))]
+ #[cold]
+ #[inline(never)]
+ fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> {
+ let mut exponent = 0;
+ loop {
+ match tri!(self.peek_or_null()) {
+ b'0'..=b'9' => {
+ self.eat_char();
+ // This could overflow... if your integer is gigabytes long.
+ // Ignore that possibility.
+ exponent += 1;
+ }
+ b'.' => {
+ return self.parse_decimal(positive, significand, exponent);
+ }
+ b'e' | b'E' => {
+ return self.parse_exponent(positive, significand, exponent);
+ }
+ _ => {
+ return self.f64_from_parts(positive, significand, exponent);
+ }
+ }
+ }
+ }
+
+ #[cfg(feature = "float_roundtrip")]
+ #[cold]
+ fn parse_long_decimal(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
+ let mut at_least_one_digit = integer_end < self.scratch.len();
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.scratch.push(c);
+ self.eat_char();
+ at_least_one_digit = true;
+ }
+
+ if !at_least_one_digit {
+ match tri!(self.peek()) {
+ Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
+ None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ match tri!(self.peek_or_null()) {
+ b'e' | b'E' => self.parse_long_exponent(positive, integer_end),
+ _ => self.f64_long_from_parts(positive, integer_end, 0),
+ }
+ }
+
+ #[cfg(feature = "float_roundtrip")]
+ fn parse_long_exponent(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
+ self.eat_char();
+
+ let positive_exp = match tri!(self.peek_or_null()) {
+ b'+' => {
+ self.eat_char();
+ true
+ }
+ b'-' => {
+ self.eat_char();
+ false
+ }
+ _ => true,
+ };
+
+ let next = match tri!(self.next_char()) {
+ Some(b) => b,
+ None => {
+ return Err(self.error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ // Make sure a digit follows the exponent place.
+ let mut exp = match next {
+ c @ b'0'..=b'9' => (c - b'0') as i32,
+ _ => {
+ return Err(self.error(ErrorCode::InvalidNumber));
+ }
+ };
+
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ let digit = (c - b'0') as i32;
+
+ if overflow!(exp * 10 + digit, i32::max_value()) {
+ let zero_significand = self.scratch.iter().all(|&digit| digit == b'0');
+ return self.parse_exponent_overflow(positive, zero_significand, positive_exp);
+ }
+
+ exp = exp * 10 + digit;
+ }
+
+ let final_exp = if positive_exp { exp } else { -exp };
+
+ self.f64_long_from_parts(positive, integer_end, final_exp)
+ }
+
+ // This cold code should not be inlined into the middle of the hot
+ // decimal-parsing loop above.
+ #[cfg(feature = "float_roundtrip")]
+ #[cold]
+ #[inline(never)]
+ fn parse_decimal_overflow(
+ &mut self,
+ positive: bool,
+ significand: u64,
+ exponent: i32,
+ ) -> Result<f64> {
+ let mut buffer = itoa::Buffer::new();
+ let significand = buffer.format(significand);
+ let fraction_digits = -exponent as usize;
+ self.scratch.clear();
+ if let Some(zeros) = fraction_digits.checked_sub(significand.len() + 1) {
+ self.scratch.extend(iter::repeat(b'0').take(zeros + 1));
+ }
+ self.scratch.extend_from_slice(significand.as_bytes());
+ let integer_end = self.scratch.len() - fraction_digits;
+ self.parse_long_decimal(positive, integer_end)
+ }
+
+ #[cfg(not(feature = "float_roundtrip"))]
+ #[cold]
+ #[inline(never)]
+ fn parse_decimal_overflow(
+ &mut self,
+ positive: bool,
+ significand: u64,
+ exponent: i32,
+ ) -> Result<f64> {
+ // The next multiply/add would overflow, so just ignore all further
+ // digits.
+ while let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ }
+
+ match tri!(self.peek_or_null()) {
+ b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
+ _ => self.f64_from_parts(positive, significand, exponent),
+ }
+ }
+
+ // This cold code should not be inlined into the middle of the hot
+ // exponent-parsing loop above.
+ #[cold]
+ #[inline(never)]
+ fn parse_exponent_overflow(
+ &mut self,
+ positive: bool,
+ zero_significand: bool,
+ positive_exp: bool,
+ ) -> Result<f64> {
+ // Error instead of +/- infinity.
+ if !zero_significand && positive_exp {
+ return Err(self.error(ErrorCode::NumberOutOfRange));
+ }
+
+ while let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ }
+ Ok(if positive { 0.0 } else { -0.0 })
+ }
+
+ #[cfg(feature = "float_roundtrip")]
+ fn f64_long_from_parts(
+ &mut self,
+ positive: bool,
+ integer_end: usize,
+ exponent: i32,
+ ) -> Result<f64> {
+ let integer = &self.scratch[..integer_end];
+ let fraction = &self.scratch[integer_end..];
+
+ let f = if self.single_precision {
+ lexical::parse_truncated_float::<f32>(integer, fraction, exponent) as f64
+ } else {
+ lexical::parse_truncated_float::<f64>(integer, fraction, exponent)
+ };
+
+ if f.is_infinite() {
+ Err(self.error(ErrorCode::NumberOutOfRange))
+ } else {
+ Ok(if positive { f } else { -f })
+ }
+ }
+
+ fn parse_any_signed_number(&mut self) -> Result<ParserNumber> {
+ let peek = match tri!(self.peek()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'-' => {
+ self.eat_char();
+ self.parse_any_number(false)
+ }
+ b'0'..=b'9' => self.parse_any_number(true),
+ _ => Err(self.peek_error(ErrorCode::InvalidNumber)),
+ };
+
+ let value = match tri!(self.peek()) {
+ Some(_) => Err(self.peek_error(ErrorCode::InvalidNumber)),
+ None => value,
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ // The de::Error impl creates errors with unknown line and column.
+ // Fill in the position here by looking at the current index in the
+ // input. There is no way to tell whether this should call `error`
+ // or `peek_error` so pick the one that seems correct more often.
+ // Worst case, the position is off by one character.
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ #[cfg(not(feature = "arbitrary_precision"))]
+ fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
+ self.parse_integer(positive)
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn parse_any_number(&mut self, positive: bool) -> Result<ParserNumber> {
+ let mut buf = String::with_capacity(16);
+ if !positive {
+ 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))
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> {
+ match tri!(self.next_char()) {
+ Some(b) => {
+ buf.push(b as char);
+ Ok(b)
+ }
+ None => Err(self.error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn scan_integer(&mut self, buf: &mut String) -> Result<()> {
+ match tri!(self.scan_or_eof(buf)) {
+ b'0' => {
+ // There can be only one leading '0'.
+ match tri!(self.peek_or_null()) {
+ b'0'..=b'9' => Err(self.peek_error(ErrorCode::InvalidNumber)),
+ _ => self.scan_number(buf),
+ }
+ }
+ b'1'..=b'9' => loop {
+ match tri!(self.peek_or_null()) {
+ c @ b'0'..=b'9' => {
+ self.eat_char();
+ buf.push(c as char);
+ }
+ _ => {
+ return self.scan_number(buf);
+ }
+ }
+ },
+ _ => Err(self.error(ErrorCode::InvalidNumber)),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn scan_number(&mut self, buf: &mut String) -> Result<()> {
+ match tri!(self.peek_or_null()) {
+ b'.' => self.scan_decimal(buf),
+ e @ b'e' | e @ b'E' => self.scan_exponent(e as char, buf),
+ _ => Ok(()),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn scan_decimal(&mut self, buf: &mut String) -> Result<()> {
+ self.eat_char();
+ buf.push('.');
+
+ let mut at_least_one_digit = false;
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ buf.push(c as char);
+ at_least_one_digit = true;
+ }
+
+ if !at_least_one_digit {
+ match tri!(self.peek()) {
+ Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
+ None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ match tri!(self.peek_or_null()) {
+ e @ b'e' | e @ b'E' => self.scan_exponent(e as char, buf),
+ _ => Ok(()),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> {
+ self.eat_char();
+ buf.push(e);
+
+ match tri!(self.peek_or_null()) {
+ b'+' => {
+ self.eat_char();
+ buf.push('+');
+ }
+ b'-' => {
+ self.eat_char();
+ buf.push('-');
+ }
+ _ => {}
+ }
+
+ // Make sure a digit follows the exponent place.
+ match tri!(self.scan_or_eof(buf)) {
+ b'0'..=b'9' => {}
+ _ => {
+ return Err(self.error(ErrorCode::InvalidNumber));
+ }
+ }
+
+ while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ buf.push(c as char);
+ }
+
+ Ok(())
+ }
+
+ fn parse_object_colon(&mut self) -> Result<()> {
+ match tri!(self.parse_whitespace()) {
+ Some(b':') => {
+ self.eat_char();
+ Ok(())
+ }
+ Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)),
+ None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
+ }
+ }
+
+ fn end_seq(&mut self) -> Result<()> {
+ match tri!(self.parse_whitespace()) {
+ Some(b']') => {
+ self.eat_char();
+ Ok(())
+ }
+ Some(b',') => {
+ self.eat_char();
+ match self.parse_whitespace() {
+ Ok(Some(b']')) => Err(self.peek_error(ErrorCode::TrailingComma)),
+ _ => Err(self.peek_error(ErrorCode::TrailingCharacters)),
+ }
+ }
+ Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
+ None => Err(self.peek_error(ErrorCode::EofWhileParsingList)),
+ }
+ }
+
+ fn end_map(&mut self) -> Result<()> {
+ match tri!(self.parse_whitespace()) {
+ Some(b'}') => {
+ self.eat_char();
+ Ok(())
+ }
+ Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)),
+ Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
+ None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
+ }
+ }
+
+ fn ignore_value(&mut self) -> Result<()> {
+ self.scratch.clear();
+ let mut enclosing = None;
+
+ loop {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let frame = match peek {
+ b'n' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"ull"));
+ None
+ }
+ b't' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"rue"));
+ None
+ }
+ b'f' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"alse"));
+ None
+ }
+ b'-' => {
+ self.eat_char();
+ tri!(self.ignore_integer());
+ None
+ }
+ b'0'..=b'9' => {
+ tri!(self.ignore_integer());
+ None
+ }
+ b'"' => {
+ self.eat_char();
+ tri!(self.read.ignore_str());
+ None
+ }
+ frame @ b'[' | frame @ b'{' => {
+ self.scratch.extend(enclosing.take());
+ self.eat_char();
+ Some(frame)
+ }
+ _ => return Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
+ };
+
+ let (mut accept_comma, mut frame) = match frame {
+ Some(frame) => (false, frame),
+ None => match enclosing.take() {
+ Some(frame) => (true, frame),
+ None => match self.scratch.pop() {
+ Some(frame) => (true, frame),
+ None => return Ok(()),
+ },
+ },
+ };
+
+ loop {
+ match tri!(self.parse_whitespace()) {
+ Some(b',') if accept_comma => {
+ self.eat_char();
+ break;
+ }
+ Some(b']') if frame == b'[' => {}
+ Some(b'}') if frame == b'{' => {}
+ Some(_) => {
+ if accept_comma {
+ return Err(self.peek_error(match frame {
+ b'[' => ErrorCode::ExpectedListCommaOrEnd,
+ b'{' => ErrorCode::ExpectedObjectCommaOrEnd,
+ _ => unreachable!(),
+ }));
+ } else {
+ break;
+ }
+ }
+ None => {
+ return Err(self.peek_error(match frame {
+ b'[' => ErrorCode::EofWhileParsingList,
+ b'{' => ErrorCode::EofWhileParsingObject,
+ _ => unreachable!(),
+ }));
+ }
+ }
+
+ self.eat_char();
+ frame = match self.scratch.pop() {
+ Some(frame) => frame,
+ None => return Ok(()),
+ };
+ accept_comma = true;
+ }
+
+ if frame == b'{' {
+ match tri!(self.parse_whitespace()) {
+ Some(b'"') => self.eat_char(),
+ Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
+ None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
+ }
+ tri!(self.read.ignore_str());
+ match tri!(self.parse_whitespace()) {
+ Some(b':') => self.eat_char(),
+ Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)),
+ None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
+ }
+ }
+
+ enclosing = Some(frame);
+ }
+ }
+
+ fn ignore_integer(&mut self) -> Result<()> {
+ match tri!(self.next_char_or_null()) {
+ b'0' => {
+ // There can be only one leading '0'.
+ if let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ return Err(self.peek_error(ErrorCode::InvalidNumber));
+ }
+ }
+ b'1'..=b'9' => {
+ while let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ }
+ }
+ _ => {
+ return Err(self.error(ErrorCode::InvalidNumber));
+ }
+ }
+
+ match tri!(self.peek_or_null()) {
+ b'.' => self.ignore_decimal(),
+ b'e' | b'E' => self.ignore_exponent(),
+ _ => Ok(()),
+ }
+ }
+
+ fn ignore_decimal(&mut self) -> Result<()> {
+ self.eat_char();
+
+ let mut at_least_one_digit = false;
+ while let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ at_least_one_digit = true;
+ }
+
+ if !at_least_one_digit {
+ return Err(self.peek_error(ErrorCode::InvalidNumber));
+ }
+
+ match tri!(self.peek_or_null()) {
+ b'e' | b'E' => self.ignore_exponent(),
+ _ => Ok(()),
+ }
+ }
+
+ fn ignore_exponent(&mut self) -> Result<()> {
+ self.eat_char();
+
+ match tri!(self.peek_or_null()) {
+ b'+' | b'-' => self.eat_char(),
+ _ => {}
+ }
+
+ // Make sure a digit follows the exponent place.
+ match tri!(self.next_char_or_null()) {
+ b'0'..=b'9' => {}
+ _ => {
+ return Err(self.error(ErrorCode::InvalidNumber));
+ }
+ }
+
+ while let b'0'..=b'9' = tri!(self.peek_or_null()) {
+ self.eat_char();
+ }
+
+ Ok(())
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn deserialize_raw_value<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.parse_whitespace()?;
+ self.read.begin_raw_buffering();
+ self.ignore_value()?;
+ self.read.end_raw_buffering(visitor)
+ }
+}
+
+impl FromStr for Number {
+ type Err = Error;
+
+ fn from_str(s: &str) -> result::Result<Self, Self::Err> {
+ Deserializer::from_str(s)
+ .parse_any_signed_number()
+ .map(Into::into)
+ }
+}
+
+#[cfg(not(feature = "float_roundtrip"))]
+static POW10: [f64; 309] = [
+ 1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009, //
+ 1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019, //
+ 1e020, 1e021, 1e022, 1e023, 1e024, 1e025, 1e026, 1e027, 1e028, 1e029, //
+ 1e030, 1e031, 1e032, 1e033, 1e034, 1e035, 1e036, 1e037, 1e038, 1e039, //
+ 1e040, 1e041, 1e042, 1e043, 1e044, 1e045, 1e046, 1e047, 1e048, 1e049, //
+ 1e050, 1e051, 1e052, 1e053, 1e054, 1e055, 1e056, 1e057, 1e058, 1e059, //
+ 1e060, 1e061, 1e062, 1e063, 1e064, 1e065, 1e066, 1e067, 1e068, 1e069, //
+ 1e070, 1e071, 1e072, 1e073, 1e074, 1e075, 1e076, 1e077, 1e078, 1e079, //
+ 1e080, 1e081, 1e082, 1e083, 1e084, 1e085, 1e086, 1e087, 1e088, 1e089, //
+ 1e090, 1e091, 1e092, 1e093, 1e094, 1e095, 1e096, 1e097, 1e098, 1e099, //
+ 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109, //
+ 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119, //
+ 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129, //
+ 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, //
+ 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, //
+ 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, //
+ 1e160, 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169, //
+ 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178, 1e179, //
+ 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187, 1e188, 1e189, //
+ 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196, 1e197, 1e198, 1e199, //
+ 1e200, 1e201, 1e202, 1e203, 1e204, 1e205, 1e206, 1e207, 1e208, 1e209, //
+ 1e210, 1e211, 1e212, 1e213, 1e214, 1e215, 1e216, 1e217, 1e218, 1e219, //
+ 1e220, 1e221, 1e222, 1e223, 1e224, 1e225, 1e226, 1e227, 1e228, 1e229, //
+ 1e230, 1e231, 1e232, 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239, //
+ 1e240, 1e241, 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249, //
+ 1e250, 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259, //
+ 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268, 1e269, //
+ 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277, 1e278, 1e279, //
+ 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286, 1e287, 1e288, 1e289, //
+ 1e290, 1e291, 1e292, 1e293, 1e294, 1e295, 1e296, 1e297, 1e298, 1e299, //
+ 1e300, 1e301, 1e302, 1e303, 1e304, 1e305, 1e306, 1e307, 1e308,
+];
+
+macro_rules! deserialize_number {
+ ($method:ident) => {
+ fn $method<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_number(visitor)
+ }
+ };
+}
+
+#[cfg(not(feature = "unbounded_depth"))]
+macro_rules! if_checking_recursion_limit {
+ ($($body:tt)*) => {
+ $($body)*
+ };
+}
+
+#[cfg(feature = "unbounded_depth")]
+macro_rules! if_checking_recursion_limit {
+ ($this:ident $($body:tt)*) => {
+ if !$this.disable_recursion_limit {
+ $this $($body)*
+ }
+ };
+}
+
+macro_rules! check_recursion {
+ ($this:ident $($body:tt)*) => {
+ if_checking_recursion_limit! {
+ $this.remaining_depth -= 1;
+ if $this.remaining_depth == 0 {
+ return Err($this.peek_error(ErrorCode::RecursionLimitExceeded));
+ }
+ }
+
+ $this $($body)*
+
+ if_checking_recursion_limit! {
+ $this.remaining_depth += 1;
+ }
+ };
+}
+
+impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
+ type Error = Error;
+
+ #[inline]
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'n' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"ull"));
+ visitor.visit_unit()
+ }
+ b't' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"rue"));
+ visitor.visit_bool(true)
+ }
+ b'f' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"alse"));
+ visitor.visit_bool(false)
+ }
+ b'-' => {
+ self.eat_char();
+ tri!(self.parse_any_number(false)).visit(visitor)
+ }
+ b'0'..=b'9' => tri!(self.parse_any_number(true)).visit(visitor),
+ b'"' => {
+ self.eat_char();
+ self.scratch.clear();
+ match tri!(self.read.parse_str(&mut self.scratch)) {
+ Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
+ Reference::Copied(s) => visitor.visit_str(s),
+ }
+ }
+ b'[' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_seq(SeqAccess::new(self));
+ }
+
+ match (ret, self.end_seq()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ b'{' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_map(MapAccess::new(self));
+ }
+
+ match (ret, self.end_map()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ _ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ // The de::Error impl creates errors with unknown line and column.
+ // Fill in the position here by looking at the current index in the
+ // input. There is no way to tell whether this should call `error`
+ // or `peek_error` so pick the one that seems correct more often.
+ // Worst case, the position is off by one character.
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b't' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"rue"));
+ visitor.visit_bool(true)
+ }
+ b'f' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"alse"));
+ visitor.visit_bool(false)
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ deserialize_number!(deserialize_i8);
+ deserialize_number!(deserialize_i16);
+ deserialize_number!(deserialize_i32);
+ deserialize_number!(deserialize_i64);
+ deserialize_number!(deserialize_u8);
+ deserialize_number!(deserialize_u16);
+ deserialize_number!(deserialize_u32);
+ deserialize_number!(deserialize_u64);
+ #[cfg(not(feature = "float_roundtrip"))]
+ deserialize_number!(deserialize_f32);
+ deserialize_number!(deserialize_f64);
+
+ #[cfg(feature = "float_roundtrip")]
+ fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.single_precision = true;
+ let val = self.deserialize_number(visitor);
+ self.single_precision = false;
+ val
+ }
+
+ fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let mut buf = String::new();
+
+ match tri!(self.parse_whitespace()) {
+ Some(b'-') => {
+ self.eat_char();
+ buf.push('-');
+ }
+ Some(_) => {}
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ tri!(self.scan_integer128(&mut buf));
+
+ let value = match buf.parse() {
+ Ok(int) => visitor.visit_i128(int),
+ Err(_) => {
+ return Err(self.error(ErrorCode::NumberOutOfRange));
+ }
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ match tri!(self.parse_whitespace()) {
+ Some(b'-') => {
+ return Err(self.peek_error(ErrorCode::NumberOutOfRange));
+ }
+ Some(_) => {}
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ }
+
+ let mut buf = String::new();
+ tri!(self.scan_integer128(&mut buf));
+
+ let value = match buf.parse() {
+ Ok(int) => visitor.visit_u128(int),
+ Err(_) => {
+ return Err(self.error(ErrorCode::NumberOutOfRange));
+ }
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'"' => {
+ self.eat_char();
+ self.scratch.clear();
+ match tri!(self.read.parse_str(&mut self.scratch)) {
+ Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
+ Reference::Copied(s) => visitor.visit_str(s),
+ }
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ /// Parses a JSON string as bytes. Note that this function does not check
+ /// whether the bytes represent a valid UTF-8 string.
+ ///
+ /// The relevant part of the JSON specification is Section 8.2 of [RFC
+ /// 7159]:
+ ///
+ /// > When all the strings represented in a JSON text are composed entirely
+ /// > of Unicode characters (however escaped), then that JSON text is
+ /// > interoperable in the sense that all software implementations that
+ /// > parse it will agree on the contents of names and of string values in
+ /// > objects and arrays.
+ /// >
+ /// > However, the ABNF in this specification allows member names and string
+ /// > values to contain bit sequences that cannot encode Unicode characters;
+ /// > for example, "\uDEAD" (a single unpaired UTF-16 surrogate). Instances
+ /// > of this have been observed, for example, when a library truncates a
+ /// > UTF-16 string without checking whether the truncation split a
+ /// > surrogate pair. The behavior of software that receives JSON texts
+ /// > containing such values is unpredictable; for example, implementations
+ /// > might return different values for the length of a string value or even
+ /// > suffer fatal runtime exceptions.
+ ///
+ /// [RFC 7159]: https://tools.ietf.org/html/rfc7159
+ ///
+ /// The behavior of serde_json is specified to fail on non-UTF-8 strings
+ /// when deserializing into Rust UTF-8 string types such as String, and
+ /// succeed with non-UTF-8 bytes when deserializing using this method.
+ ///
+ /// Escape sequences are processed as usual, and for `\uXXXX` escapes it is
+ /// still checked if the hex number represents a valid Unicode code point.
+ ///
+ /// # Examples
+ ///
+ /// You can use this to parse JSON strings containing invalid UTF-8 bytes,
+ /// or unpaired surrogates.
+ ///
+ /// ```
+ /// use serde_bytes::ByteBuf;
+ ///
+ /// fn look_at_bytes() -> Result<(), serde_json::Error> {
+ /// let json_data = b"\"some bytes: \xe5\x00\xe5\"";
+ /// let bytes: ByteBuf = serde_json::from_slice(json_data)?;
+ ///
+ /// assert_eq!(b'\xe5', bytes[12]);
+ /// assert_eq!(b'\0', bytes[13]);
+ /// assert_eq!(b'\xe5', bytes[14]);
+ ///
+ /// Ok(())
+ /// }
+ /// #
+ /// # look_at_bytes().unwrap();
+ /// ```
+ ///
+ /// Backslash escape sequences like `\n` are still interpreted and required
+ /// 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() -> 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();
+ /// ```
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'"' => {
+ self.eat_char();
+ self.scratch.clear();
+ match tri!(self.read.parse_str_raw(&mut self.scratch)) {
+ Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
+ Reference::Copied(b) => visitor.visit_bytes(b),
+ }
+ }
+ b'[' => self.deserialize_seq(visitor),
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ #[inline]
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_bytes(visitor)
+ }
+
+ /// Parses a `null` as a None, and any other values as a `Some(...)`.
+ #[inline]
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ match tri!(self.parse_whitespace()) {
+ Some(b'n') => {
+ self.eat_char();
+ tri!(self.parse_ident(b"ull"));
+ visitor.visit_none()
+ }
+ _ => visitor.visit_some(self),
+ }
+ }
+
+ fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'n' => {
+ self.eat_char();
+ tri!(self.parse_ident(b"ull"));
+ visitor.visit_unit()
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_unit(visitor)
+ }
+
+ /// Parses a newtype struct as the underlying value.
+ #[inline]
+ fn deserialize_newtype_struct<V>(self, name: &str, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ #[cfg(feature = "raw_value")]
+ {
+ if name == crate::raw::TOKEN {
+ return self.deserialize_raw_value(visitor);
+ }
+ }
+
+ let _ = name;
+ visitor.visit_newtype_struct(self)
+ }
+
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'[' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_seq(SeqAccess::new(self));
+ }
+
+ match (ret, self.end_seq()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_tuple_struct<V>(
+ self,
+ _name: &'static str,
+ _len: usize,
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'{' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_map(MapAccess::new(self));
+ }
+
+ match (ret, self.end_map()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ fn deserialize_struct<V>(
+ self,
+ _name: &'static str,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ let peek = match tri!(self.parse_whitespace()) {
+ Some(b) => b,
+ None => {
+ return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
+ }
+ };
+
+ let value = match peek {
+ b'[' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_seq(SeqAccess::new(self));
+ }
+
+ match (ret, self.end_seq()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ b'{' => {
+ check_recursion! {
+ self.eat_char();
+ let ret = visitor.visit_map(MapAccess::new(self));
+ }
+
+ match (ret, self.end_map()) {
+ (Ok(ret), Ok(())) => Ok(ret),
+ (Err(err), _) | (_, Err(err)) => Err(err),
+ }
+ }
+ _ => Err(self.peek_invalid_type(&visitor)),
+ };
+
+ match value {
+ Ok(value) => Ok(value),
+ Err(err) => Err(self.fix_position(err)),
+ }
+ }
+
+ /// Parses an enum as an object like `{"$KEY":$VALUE}`, where $VALUE is either a straight
+ /// value, a `[..]`, or a `{..}`.
+ #[inline]
+ fn deserialize_enum<V>(
+ self,
+ _name: &str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ match tri!(self.parse_whitespace()) {
+ Some(b'{') => {
+ check_recursion! {
+ self.eat_char();
+ let value = tri!(visitor.visit_enum(VariantAccess::new(self)));
+ }
+
+ match tri!(self.parse_whitespace()) {
+ Some(b'}') => {
+ self.eat_char();
+ Ok(value)
+ }
+ Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
+ None => Err(self.error(ErrorCode::EofWhileParsingObject)),
+ }
+ }
+ Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
+ Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
+ None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ tri!(self.ignore_value());
+ visitor.visit_unit()
+ }
+}
+
+struct SeqAccess<'a, R: 'a> {
+ de: &'a mut Deserializer<R>,
+ first: bool,
+}
+
+impl<'a, R: 'a> SeqAccess<'a, R> {
+ fn new(de: &'a mut Deserializer<R>) -> Self {
+ SeqAccess { de, first: true }
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
+ type Error = Error;
+
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ let peek = match tri!(self.de.parse_whitespace()) {
+ Some(b']') => {
+ return Ok(None);
+ }
+ Some(b',') if !self.first => {
+ self.de.eat_char();
+ tri!(self.de.parse_whitespace())
+ }
+ Some(b) => {
+ if self.first {
+ self.first = false;
+ Some(b)
+ } else {
+ return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
+ }
+ }
+ None => {
+ return Err(self.de.peek_error(ErrorCode::EofWhileParsingList));
+ }
+ };
+
+ match peek {
+ Some(b']') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
+ Some(_) => Ok(Some(tri!(seed.deserialize(&mut *self.de)))),
+ None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+}
+
+struct MapAccess<'a, R: 'a> {
+ de: &'a mut Deserializer<R>,
+ first: bool,
+}
+
+impl<'a, R: 'a> MapAccess<'a, R> {
+ fn new(de: &'a mut Deserializer<R>) -> Self {
+ MapAccess { de, first: true }
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
+ type Error = Error;
+
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
+ where
+ K: de::DeserializeSeed<'de>,
+ {
+ let peek = match tri!(self.de.parse_whitespace()) {
+ Some(b'}') => {
+ return Ok(None);
+ }
+ Some(b',') if !self.first => {
+ self.de.eat_char();
+ tri!(self.de.parse_whitespace())
+ }
+ Some(b) => {
+ if self.first {
+ self.first = false;
+ Some(b)
+ } else {
+ return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
+ }
+ }
+ None => {
+ return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject));
+ }
+ };
+
+ match peek {
+ Some(b'"') => seed.deserialize(MapKey { de: &mut *self.de }).map(Some),
+ Some(b'}') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
+ Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)),
+ None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
+ }
+ }
+
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ tri!(self.de.parse_object_colon());
+
+ seed.deserialize(&mut *self.de)
+ }
+}
+
+struct VariantAccess<'a, R: 'a> {
+ de: &'a mut Deserializer<R>,
+}
+
+impl<'a, R: 'a> VariantAccess<'a, R> {
+ fn new(de: &'a mut Deserializer<R>) -> Self {
+ VariantAccess { de }
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for VariantAccess<'a, R> {
+ type Error = Error;
+ type Variant = Self;
+
+ fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ let val = tri!(seed.deserialize(&mut *self.de));
+ tri!(self.de.parse_object_colon());
+ Ok((val, self))
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for VariantAccess<'a, R> {
+ type Error = Error;
+
+ fn unit_variant(self) -> Result<()> {
+ de::Deserialize::deserialize(self.de)
+ }
+
+ fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self.de)
+ }
+
+ fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ de::Deserializer::deserialize_seq(self.de, visitor)
+ }
+
+ fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
+ }
+}
+
+struct UnitVariantAccess<'a, R: 'a> {
+ de: &'a mut Deserializer<R>,
+}
+
+impl<'a, R: 'a> UnitVariantAccess<'a, R> {
+ fn new(de: &'a mut Deserializer<R>) -> Self {
+ UnitVariantAccess { de }
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::EnumAccess<'de> for UnitVariantAccess<'a, R> {
+ type Error = Error;
+ type Variant = Self;
+
+ fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ let variant = tri!(seed.deserialize(&mut *self.de));
+ Ok((variant, self))
+ }
+}
+
+impl<'de, 'a, R: Read<'de> + 'a> de::VariantAccess<'de> for UnitVariantAccess<'a, R> {
+ type Error = Error;
+
+ fn unit_variant(self) -> Result<()> {
+ Ok(())
+ }
+
+ fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"newtype variant",
+ ))
+ }
+
+ fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"tuple variant",
+ ))
+ }
+
+ fn struct_variant<V>(self, _fields: &'static [&'static str], _visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"struct variant",
+ ))
+ }
+}
+
+/// Only deserialize from this after peeking a '"' byte! Otherwise it may
+/// deserialize invalid JSON successfully.
+struct MapKey<'a, R: 'a> {
+ de: &'a mut Deserializer<R>,
+}
+
+macro_rules! deserialize_integer_key {
+ ($method:ident => $visit:ident) => {
+ fn $method<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.de.eat_char();
+ self.de.scratch.clear();
+ let string = tri!(self.de.read.parse_str(&mut self.de.scratch));
+ match (string.parse(), string) {
+ (Ok(integer), _) => visitor.$visit(integer),
+ (Err(_), Reference::Borrowed(s)) => visitor.visit_borrowed_str(s),
+ (Err(_), Reference::Copied(s)) => visitor.visit_str(s),
+ }
+ }
+ };
+}
+
+impl<'de, 'a, R> de::Deserializer<'de> for MapKey<'a, R>
+where
+ R: Read<'de>,
+{
+ type Error = Error;
+
+ #[inline]
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.de.eat_char();
+ self.de.scratch.clear();
+ match tri!(self.de.read.parse_str(&mut self.de.scratch)) {
+ Reference::Borrowed(s) => visitor.visit_borrowed_str(s),
+ Reference::Copied(s) => visitor.visit_str(s),
+ }
+ }
+
+ deserialize_integer_key!(deserialize_i8 => visit_i8);
+ deserialize_integer_key!(deserialize_i16 => visit_i16);
+ deserialize_integer_key!(deserialize_i32 => visit_i32);
+ deserialize_integer_key!(deserialize_i64 => visit_i64);
+ deserialize_integer_key!(deserialize_i128 => visit_i128);
+ deserialize_integer_key!(deserialize_u8 => visit_u8);
+ deserialize_integer_key!(deserialize_u16 => visit_u16);
+ deserialize_integer_key!(deserialize_u32 => visit_u32);
+ deserialize_integer_key!(deserialize_u64 => visit_u64);
+ deserialize_integer_key!(deserialize_u128 => visit_u128);
+
+ #[inline]
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ // Map keys cannot be null.
+ visitor.visit_some(self)
+ }
+
+ #[inline]
+ 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)
+ }
+
+ #[inline]
+ fn deserialize_enum<V>(
+ self,
+ name: &'static str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.de.deserialize_enum(name, variants, visitor)
+ }
+
+ #[inline]
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.de.deserialize_bytes(visitor)
+ }
+
+ #[inline]
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: de::Visitor<'de>,
+ {
+ self.de.deserialize_bytes(visitor)
+ }
+
+ forward_to_deserialize_any! {
+ bool f32 f64 char str string unit unit_struct seq tuple tuple_struct map
+ struct identifier ignored_any
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// Iterator that deserializes a stream into multiple JSON values.
+///
+/// A stream deserializer can be created from any JSON deserializer using the
+/// `Deserializer::into_iter` method.
+///
+/// The data can consist of any JSON value. Values need to be a self-delineating value e.g.
+/// arrays, objects, or strings, or be followed by whitespace or a self-delineating value.
+///
+/// ```
+/// use serde_json::{Deserializer, Value};
+///
+/// fn main() {
+/// let data = "{\"k\": 3}1\"cool\"\"stuff\" 3{} [0, 1, 2]";
+///
+/// let stream = Deserializer::from_str(data).into_iter::<Value>();
+///
+/// for value in stream {
+/// println!("{}", value.unwrap());
+/// }
+/// }
+/// ```
+pub struct StreamDeserializer<'de, R, T> {
+ de: Deserializer<R>,
+ offset: usize,
+ failed: bool,
+ output: PhantomData<T>,
+ lifetime: PhantomData<&'de ()>,
+}
+
+impl<'de, R, T> StreamDeserializer<'de, R, T>
+where
+ R: read::Read<'de>,
+ T: de::Deserialize<'de>,
+{
+ /// Create a JSON stream deserializer from one of the possible serde_json
+ /// input sources.
+ ///
+ /// Typically it is more convenient to use one of these methods instead:
+ ///
+ /// - Deserializer::from_str(...).into_iter()
+ /// - Deserializer::from_slice(...).into_iter()
+ /// - Deserializer::from_reader(...).into_iter()
+ pub fn new(read: R) -> Self {
+ let offset = read.byte_offset();
+ StreamDeserializer {
+ de: Deserializer::new(read),
+ offset,
+ failed: false,
+ output: PhantomData,
+ lifetime: PhantomData,
+ }
+ }
+
+ /// Returns the number of bytes so far deserialized into a successful `T`.
+ ///
+ /// If a stream deserializer returns an EOF error, new data can be joined to
+ /// `old_data[stream.byte_offset()..]` to try again.
+ ///
+ /// ```
+ /// let data = b"[0] [1] [";
+ ///
+ /// let de = serde_json::Deserializer::from_slice(data);
+ /// let mut stream = de.into_iter::<Vec<i32>>();
+ /// assert_eq!(0, stream.byte_offset());
+ ///
+ /// println!("{:?}", stream.next()); // [0]
+ /// assert_eq!(3, stream.byte_offset());
+ ///
+ /// println!("{:?}", stream.next()); // [1]
+ /// assert_eq!(7, stream.byte_offset());
+ ///
+ /// println!("{:?}", stream.next()); // error
+ /// assert_eq!(8, stream.byte_offset());
+ ///
+ /// // If err.is_eof(), can join the remaining data to new data and continue.
+ /// let remaining = &data[stream.byte_offset()..];
+ /// ```
+ ///
+ /// *Note:* In the future this method may be changed to return the number of
+ /// bytes so far deserialized into a successful T *or* syntactically valid
+ /// JSON skipped over due to a type error. See [serde-rs/json#70] for an
+ /// example illustrating this.
+ ///
+ /// [serde-rs/json#70]: https://github.com/serde-rs/json/issues/70
+ pub fn byte_offset(&self) -> usize {
+ self.offset
+ }
+
+ fn peek_end_of_value(&mut self) -> Result<()> {
+ match tri!(self.de.peek()) {
+ Some(b' ') | Some(b'\n') | Some(b'\t') | Some(b'\r') | Some(b'"') | Some(b'[')
+ | Some(b']') | Some(b'{') | Some(b'}') | Some(b',') | Some(b':') | None => Ok(()),
+ Some(_) => {
+ let position = self.de.read.peek_position();
+ Err(Error::syntax(
+ ErrorCode::TrailingCharacters,
+ position.line,
+ position.column,
+ ))
+ }
+ }
+ }
+}
+
+impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T>
+where
+ R: Read<'de>,
+ T: de::Deserialize<'de>,
+{
+ type Item = Result<T>;
+
+ fn next(&mut self) -> Option<Result<T>> {
+ if R::should_early_return_if_failed && self.failed {
+ return None;
+ }
+
+ // skip whitespaces, if any
+ // this helps with trailing whitespaces, since whitespaces between
+ // values are handled for us.
+ match self.de.parse_whitespace() {
+ Ok(None) => {
+ self.offset = self.de.read.byte_offset();
+ None
+ }
+ Ok(Some(b)) => {
+ // If the value does not have a clear way to show the end of the value
+ // (like numbers, null, true etc.) we have to look for whitespace or
+ // the beginning of a self-delineated value.
+ let self_delineated_value = match b {
+ b'[' | b'"' | b'{' => true,
+ _ => false,
+ };
+ self.offset = self.de.read.byte_offset();
+ let result = de::Deserialize::deserialize(&mut self.de);
+
+ Some(match result {
+ Ok(value) => {
+ self.offset = self.de.read.byte_offset();
+ if self_delineated_value {
+ Ok(value)
+ } else {
+ self.peek_end_of_value().map(|_| value)
+ }
+ }
+ Err(e) => {
+ self.de.read.set_failed(&mut self.failed);
+ Err(e)
+ }
+ })
+ }
+ Err(e) => {
+ self.de.read.set_failed(&mut self.failed);
+ Some(Err(e))
+ }
+ }
+ }
+}
+
+impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T>
+where
+ R: Read<'de> + Fused,
+ T: de::Deserialize<'de>,
+{
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+fn from_trait<'de, R, T>(read: R) -> Result<T>
+where
+ R: Read<'de>,
+ T: de::Deserialize<'de>,
+{
+ let mut de = Deserializer::new(read);
+ let value = tri!(de::Deserialize::deserialize(&mut de));
+
+ // Make sure the whole stream has been consumed.
+ tri!(de.end());
+ Ok(value)
+}
+
+/// Deserialize an instance of type `T` from an IO stream of JSON.
+///
+/// The content of the IO stream is deserialized directly from the stream
+/// without being buffered in memory by serde_json.
+///
+/// When reading from a source against which short reads are not efficient, such
+/// as a [`File`], you will want to apply your own buffering because serde_json
+/// will not buffer the input. See [`std::io::BufReader`].
+///
+/// It is expected that the input stream ends after the deserialized object.
+/// If the stream does not end, such as in the case of a persistent socket connection,
+/// this function will not return. It is possible instead to deserialize from a prefix of an input
+/// stream without looking for EOF by managing your own [`Deserializer`].
+///
+/// Note that counter to intuition, this function is usually slower than
+/// reading a file completely into memory and then applying [`from_str`]
+/// or [`from_slice`] on it. See [issue #160].
+///
+/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
+/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
+/// [`from_str`]: ./fn.from_str.html
+/// [`from_slice`]: ./fn.from_slice.html
+/// [issue #160]: https://github.com/serde-rs/json/issues/160
+///
+/// # Example
+///
+/// Reading the contents of a file.
+///
+/// ```
+/// use serde::Deserialize;
+///
+/// use std::error::Error;
+/// use std::fs::File;
+/// use std::io::BufReader;
+/// use std::path::Path;
+///
+/// #[derive(Deserialize, Debug)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> {
+/// // Open the file in read-only mode with buffer.
+/// let file = File::open(path)?;
+/// let reader = BufReader::new(file);
+///
+/// // Read the JSON contents of the file as an instance of `User`.
+/// let u = serde_json::from_reader(reader)?;
+///
+/// // Return the `User`.
+/// Ok(u)
+/// }
+///
+/// fn main() {
+/// # }
+/// # fn fake_main() {
+/// let u = read_user_from_file("test.json").unwrap();
+/// println!("{:#?}", u);
+/// }
+/// ```
+///
+/// Reading from a persistent socket connection.
+///
+/// ```
+/// use serde::Deserialize;
+///
+/// use std::error::Error;
+/// use std::net::{TcpListener, TcpStream};
+///
+/// #[derive(Deserialize, Debug)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
+/// let mut de = serde_json::Deserializer::from_reader(tcp_stream);
+/// let u = User::deserialize(&mut de)?;
+///
+/// Ok(u)
+/// }
+///
+/// fn main() {
+/// # }
+/// # fn fake_main() {
+/// let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
+///
+/// for stream in listener.incoming() {
+/// println!("{:#?}", read_user_from_stream(stream.unwrap()));
+/// }
+/// }
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if the structure of the input does not match the
+/// structure expected by `T`, for example if `T` is a struct type but the input
+/// contains something other than a JSON map. It can also fail if the structure
+/// is correct but `T`'s implementation of `Deserialize` decides that something
+/// is wrong with the data, for example required struct fields are missing from
+/// the JSON map or some number is too big to fit in the expected primitive
+/// type.
+#[cfg(feature = "std")]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub fn from_reader<R, T>(rdr: R) -> Result<T>
+where
+ R: crate::io::Read,
+ T: de::DeserializeOwned,
+{
+ from_trait(read::IoRead::new(rdr))
+}
+
+/// Deserialize an instance of type `T` from bytes of JSON text.
+///
+/// # Example
+///
+/// ```
+/// use serde::Deserialize;
+///
+/// #[derive(Deserialize, Debug)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn main() {
+/// // The type of `j` is `&[u8]`
+/// let j = b"
+/// {
+/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
+/// \"location\": \"Menlo Park, CA\"
+/// }";
+///
+/// let u: User = serde_json::from_slice(j).unwrap();
+/// println!("{:#?}", u);
+/// }
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if the structure of the input does not match the
+/// structure expected by `T`, for example if `T` is a struct type but the input
+/// contains something other than a JSON map. It can also fail if the structure
+/// is correct but `T`'s implementation of `Deserialize` decides that something
+/// is wrong with the data, for example required struct fields are missing from
+/// the JSON map or some number is too big to fit in the expected primitive
+/// type.
+pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
+where
+ T: de::Deserialize<'a>,
+{
+ from_trait(read::SliceRead::new(v))
+}
+
+/// Deserialize an instance of type `T` from a string of JSON text.
+///
+/// # Example
+///
+/// ```
+/// use serde::Deserialize;
+///
+/// #[derive(Deserialize, Debug)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn main() {
+/// // The type of `j` is `&str`
+/// let j = "
+/// {
+/// \"fingerprint\": \"0xF9BA143B95FF6D82\",
+/// \"location\": \"Menlo Park, CA\"
+/// }";
+///
+/// let u: User = serde_json::from_str(j).unwrap();
+/// println!("{:#?}", u);
+/// }
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if the structure of the input does not match the
+/// structure expected by `T`, for example if `T` is a struct type but the input
+/// contains something other than a JSON map. It can also fail if the structure
+/// is correct but `T`'s implementation of `Deserialize` decides that something
+/// is wrong with the data, for example required struct fields are missing from
+/// the JSON map or some number is too big to fit in the expected primitive
+/// type.
+pub fn from_str<'a, T>(s: &'a str) -> Result<T>
+where
+ T: de::Deserialize<'a>,
+{
+ from_trait(read::StrRead::new(s))
+}
diff --git a/third_party/rust/serde_json/src/error.rs b/third_party/rust/serde_json/src/error.rs
new file mode 100644
index 0000000000..1875ef08b0
--- /dev/null
+++ b/third_party/rust/serde_json/src/error.rs
@@ -0,0 +1,445 @@
+//! When serializing or deserializing JSON goes wrong.
+
+use crate::io;
+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.
+pub struct Error {
+ /// This `Box` allows us to keep the size of `Error` as small as possible. A
+ /// larger `Error` type was substantially slower due to all the functions
+ /// that pass around `Result<T, Error>`.
+ err: Box<ErrorImpl>,
+}
+
+/// Alias for a `Result` with the error type `serde_json::Error`.
+pub type Result<T> = result::Result<T, Error>;
+
+impl Error {
+ /// One-based line number at which the error was detected.
+ ///
+ /// Characters in the first line of the input (before the first newline
+ /// character) are in line 1.
+ pub fn line(&self) -> usize {
+ self.err.line
+ }
+
+ /// One-based column number at which the error was detected.
+ ///
+ /// The first character in the input and any characters immediately
+ /// following a newline character are in column 1.
+ ///
+ /// Note that errors may occur in column 0, for example if a read from an IO
+ /// stream fails immediately following a previously read newline character.
+ pub fn column(&self) -> usize {
+ self.err.column
+ }
+
+ /// Categorizes the cause of this error.
+ ///
+ /// - `Category::Io` - failure to read or write bytes on an IO stream
+ /// - `Category::Syntax` - input that is not syntactically valid JSON
+ /// - `Category::Data` - input data that is semantically incorrect
+ /// - `Category::Eof` - unexpected end of the input data
+ pub fn classify(&self) -> Category {
+ match self.err.code {
+ ErrorCode::Message(_) => Category::Data,
+ ErrorCode::Io(_) => Category::Io,
+ ErrorCode::EofWhileParsingList
+ | ErrorCode::EofWhileParsingObject
+ | ErrorCode::EofWhileParsingString
+ | ErrorCode::EofWhileParsingValue => Category::Eof,
+ ErrorCode::ExpectedColon
+ | ErrorCode::ExpectedListCommaOrEnd
+ | ErrorCode::ExpectedObjectCommaOrEnd
+ | ErrorCode::ExpectedSomeIdent
+ | ErrorCode::ExpectedSomeValue
+ | ErrorCode::InvalidEscape
+ | ErrorCode::InvalidNumber
+ | ErrorCode::NumberOutOfRange
+ | ErrorCode::InvalidUnicodeCodePoint
+ | ErrorCode::ControlCharacterWhileParsingString
+ | ErrorCode::KeyMustBeAString
+ | ErrorCode::LoneLeadingSurrogateInHexEscape
+ | ErrorCode::TrailingComma
+ | ErrorCode::TrailingCharacters
+ | ErrorCode::UnexpectedEndOfHexEscape
+ | ErrorCode::RecursionLimitExceeded => Category::Syntax,
+ }
+ }
+
+ /// Returns true if this error was caused by a failure to read or write
+ /// bytes on an IO stream.
+ pub fn is_io(&self) -> bool {
+ self.classify() == Category::Io
+ }
+
+ /// Returns true if this error was caused by input that was not
+ /// syntactically valid JSON.
+ pub fn is_syntax(&self) -> bool {
+ self.classify() == Category::Syntax
+ }
+
+ /// Returns true if this error was caused by input data that was
+ /// semantically incorrect.
+ ///
+ /// For example, JSON containing a number is semantically incorrect when the
+ /// type being deserialized into holds a String.
+ pub fn is_data(&self) -> bool {
+ self.classify() == Category::Data
+ }
+
+ /// Returns true if this error was caused by prematurely reaching the end of
+ /// the input data.
+ ///
+ /// Callers that process streaming input may be interested in retrying the
+ /// deserialization once more data is available.
+ pub fn is_eof(&self) -> bool {
+ self.classify() == Category::Eof
+ }
+}
+
+/// Categorizes the cause of a `serde_json::Error`.
+#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+pub enum Category {
+ /// The error was caused by a failure to read or write bytes on an IO
+ /// stream.
+ Io,
+
+ /// The error was caused by input that was not syntactically valid JSON.
+ Syntax,
+
+ /// The error was caused by input data that was semantically incorrect.
+ ///
+ /// For example, JSON containing a number is semantically incorrect when the
+ /// type being deserialized into holds a String.
+ Data,
+
+ /// The error was caused by prematurely reaching the end of the input data.
+ ///
+ /// Callers that process streaming input may be interested in retrying the
+ /// deserialization once more data is available.
+ Eof,
+}
+
+#[cfg(feature = "std")]
+#[allow(clippy::fallible_impl_from)]
+impl From<Error> for io::Error {
+ /// Convert a `serde_json::Error` into an `io::Error`.
+ ///
+ /// JSON syntax and data errors are turned into `InvalidData` IO errors.
+ /// EOF errors are turned into `UnexpectedEof` IO errors.
+ ///
+ /// ```
+ /// use std::io;
+ ///
+ /// enum MyError {
+ /// Io(io::Error),
+ /// Json(serde_json::Error),
+ /// }
+ ///
+ /// impl From<serde_json::Error> for MyError {
+ /// fn from(err: serde_json::Error) -> MyError {
+ /// use serde_json::error::Category;
+ /// match err.classify() {
+ /// Category::Io => {
+ /// MyError::Io(err.into())
+ /// }
+ /// Category::Syntax | Category::Data | Category::Eof => {
+ /// MyError::Json(err)
+ /// }
+ /// }
+ /// }
+ /// }
+ /// ```
+ fn from(j: Error) -> Self {
+ if let ErrorCode::Io(err) = j.err.code {
+ err
+ } else {
+ match j.classify() {
+ Category::Io => unreachable!(),
+ Category::Syntax | Category::Data => io::Error::new(io::ErrorKind::InvalidData, j),
+ Category::Eof => io::Error::new(io::ErrorKind::UnexpectedEof, j),
+ }
+ }
+ }
+}
+
+struct ErrorImpl {
+ code: ErrorCode,
+ line: usize,
+ column: usize,
+}
+
+pub(crate) enum ErrorCode {
+ /// Catchall for syntax error messages
+ Message(Box<str>),
+
+ /// Some IO error occurred while serializing or deserializing.
+ Io(io::Error),
+
+ /// EOF while parsing a list.
+ EofWhileParsingList,
+
+ /// EOF while parsing an object.
+ EofWhileParsingObject,
+
+ /// EOF while parsing a string.
+ EofWhileParsingString,
+
+ /// EOF while parsing a JSON value.
+ EofWhileParsingValue,
+
+ /// Expected this character to be a `':'`.
+ ExpectedColon,
+
+ /// Expected this character to be either a `','` or a `']'`.
+ ExpectedListCommaOrEnd,
+
+ /// Expected this character to be either a `','` or a `'}'`.
+ ExpectedObjectCommaOrEnd,
+
+ /// Expected to parse either a `true`, `false`, or a `null`.
+ ExpectedSomeIdent,
+
+ /// Expected this character to start a JSON value.
+ ExpectedSomeValue,
+
+ /// Invalid hex escape code.
+ InvalidEscape,
+
+ /// Invalid number.
+ InvalidNumber,
+
+ /// Number is bigger than the maximum value of its type.
+ NumberOutOfRange,
+
+ /// Invalid unicode code point.
+ InvalidUnicodeCodePoint,
+
+ /// Control character found while parsing a string.
+ ControlCharacterWhileParsingString,
+
+ /// Object key is not a string.
+ KeyMustBeAString,
+
+ /// Lone leading surrogate in hex escape.
+ LoneLeadingSurrogateInHexEscape,
+
+ /// JSON has a comma after the last value in an array or map.
+ TrailingComma,
+
+ /// JSON has non-whitespace trailing characters after the value.
+ TrailingCharacters,
+
+ /// Unexpected end of hex escape.
+ UnexpectedEndOfHexEscape,
+
+ /// Encountered nesting of JSON maps and arrays more than 128 layers deep.
+ RecursionLimitExceeded,
+}
+
+impl Error {
+ #[cold]
+ pub(crate) fn syntax(code: ErrorCode, line: usize, column: usize) -> Self {
+ Error {
+ err: Box::new(ErrorImpl { code, line, column }),
+ }
+ }
+
+ // Not public API. Should be pub(crate).
+ //
+ // Update `eager_json` crate when this function changes.
+ #[doc(hidden)]
+ #[cold]
+ pub fn io(error: io::Error) -> Self {
+ Error {
+ err: Box::new(ErrorImpl {
+ code: ErrorCode::Io(error),
+ line: 0,
+ column: 0,
+ }),
+ }
+ }
+
+ #[cold]
+ pub(crate) fn fix_position<F>(self, f: F) -> Self
+ where
+ F: FnOnce(ErrorCode) -> Error,
+ {
+ if self.err.line == 0 {
+ f(self.err.code)
+ } else {
+ self
+ }
+ }
+}
+
+impl Display for ErrorCode {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ ErrorCode::Message(msg) => f.write_str(msg),
+ ErrorCode::Io(err) => Display::fmt(err, f),
+ ErrorCode::EofWhileParsingList => f.write_str("EOF while parsing a list"),
+ ErrorCode::EofWhileParsingObject => f.write_str("EOF while parsing an object"),
+ ErrorCode::EofWhileParsingString => f.write_str("EOF while parsing a string"),
+ ErrorCode::EofWhileParsingValue => f.write_str("EOF while parsing a value"),
+ ErrorCode::ExpectedColon => f.write_str("expected `:`"),
+ ErrorCode::ExpectedListCommaOrEnd => f.write_str("expected `,` or `]`"),
+ ErrorCode::ExpectedObjectCommaOrEnd => f.write_str("expected `,` or `}`"),
+ ErrorCode::ExpectedSomeIdent => f.write_str("expected ident"),
+ ErrorCode::ExpectedSomeValue => f.write_str("expected value"),
+ ErrorCode::InvalidEscape => f.write_str("invalid escape"),
+ ErrorCode::InvalidNumber => f.write_str("invalid number"),
+ ErrorCode::NumberOutOfRange => f.write_str("number out of range"),
+ ErrorCode::InvalidUnicodeCodePoint => f.write_str("invalid unicode code point"),
+ ErrorCode::ControlCharacterWhileParsingString => {
+ f.write_str("control character (\\u0000-\\u001F) found while parsing a string")
+ }
+ ErrorCode::KeyMustBeAString => f.write_str("key must be a string"),
+ ErrorCode::LoneLeadingSurrogateInHexEscape => {
+ f.write_str("lone leading surrogate in hex escape")
+ }
+ ErrorCode::TrailingComma => f.write_str("trailing comma"),
+ ErrorCode::TrailingCharacters => f.write_str("trailing characters"),
+ ErrorCode::UnexpectedEndOfHexEscape => f.write_str("unexpected end of hex escape"),
+ ErrorCode::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
+ }
+ }
+}
+
+impl serde::de::StdError for Error {
+ #[cfg(feature = "std")]
+ fn source(&self) -> Option<&(dyn error::Error + 'static)> {
+ match &self.err.code {
+ ErrorCode::Io(err) => Some(err),
+ _ => None,
+ }
+ }
+}
+
+impl Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ Display::fmt(&*self.err, f)
+ }
+}
+
+impl Display for ErrorImpl {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ if self.line == 0 {
+ Display::fmt(&self.code, f)
+ } else {
+ write!(
+ f,
+ "{} at line {} column {}",
+ self.code, self.line, self.column
+ )
+ }
+ }
+}
+
+// Remove two layers of verbosity from the debug representation. Humans often
+// end up seeing this representation because it is what unwrap() shows.
+impl Debug for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "Error({:?}, line: {}, column: {})",
+ self.err.code.to_string(),
+ self.err.line,
+ self.err.column
+ )
+ }
+}
+
+impl de::Error for Error {
+ #[cold]
+ fn custom<T: Display>(msg: T) -> Error {
+ make_error(msg.to_string())
+ }
+
+ #[cold]
+ fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
+ if let de::Unexpected::Unit = unexp {
+ Error::custom(format_args!("invalid type: null, expected {}", exp))
+ } else {
+ Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
+ }
+ }
+}
+
+impl ser::Error for Error {
+ #[cold]
+ fn custom<T: Display>(msg: T) -> Error {
+ make_error(msg.to_string())
+ }
+}
+
+// Parse our own error message that looks like "{} at line {} column {}" to work
+// around erased-serde round-tripping the error through de::Error::custom.
+fn make_error(mut msg: String) -> Error {
+ let (line, column) = parse_line_col(&mut msg).unwrap_or((0, 0));
+ Error {
+ err: Box::new(ErrorImpl {
+ code: ErrorCode::Message(msg.into_boxed_str()),
+ line,
+ column,
+ }),
+ }
+}
+
+fn parse_line_col(msg: &mut String) -> Option<(usize, usize)> {
+ let start_of_suffix = match msg.rfind(" at line ") {
+ Some(index) => index,
+ None => return None,
+ };
+
+ // Find start and end of line number.
+ let start_of_line = start_of_suffix + " at line ".len();
+ let mut end_of_line = start_of_line;
+ while starts_with_digit(&msg[end_of_line..]) {
+ end_of_line += 1;
+ }
+
+ if !msg[end_of_line..].starts_with(" column ") {
+ return None;
+ }
+
+ // Find start and end of column number.
+ let start_of_column = end_of_line + " column ".len();
+ let mut end_of_column = start_of_column;
+ while starts_with_digit(&msg[end_of_column..]) {
+ end_of_column += 1;
+ }
+
+ if end_of_column < msg.len() {
+ return None;
+ }
+
+ // Parse numbers.
+ let line = match usize::from_str(&msg[start_of_line..end_of_line]) {
+ Ok(line) => line,
+ Err(_) => return None,
+ };
+ let column = match usize::from_str(&msg[start_of_column..end_of_column]) {
+ Ok(column) => column,
+ Err(_) => return None,
+ };
+
+ msg.truncate(start_of_suffix);
+ Some((line, column))
+}
+
+fn starts_with_digit(slice: &str) -> bool {
+ match slice.as_bytes().first() {
+ None => false,
+ Some(&byte) => byte >= b'0' && byte <= b'9',
+ }
+}
diff --git a/third_party/rust/serde_json/src/features_check/error.rs b/third_party/rust/serde_json/src/features_check/error.rs
new file mode 100644
index 0000000000..22e58235c7
--- /dev/null
+++ b/third_party/rust/serde_json/src/features_check/error.rs
@@ -0,0 +1 @@
+"serde_json requires that either `std` (default) or `alloc` feature is enabled"
diff --git a/third_party/rust/serde_json/src/features_check/mod.rs b/third_party/rust/serde_json/src/features_check/mod.rs
new file mode 100644
index 0000000000..d12032cef8
--- /dev/null
+++ b/third_party/rust/serde_json/src/features_check/mod.rs
@@ -0,0 +1,13 @@
+//! Shows a user-friendly compiler error on incompatible selected features.
+
+#[allow(unused_macros)]
+macro_rules! hide_from_rustfmt {
+ ($mod:item) => {
+ $mod
+ };
+}
+
+#[cfg(not(any(feature = "std", feature = "alloc")))]
+hide_from_rustfmt! {
+ mod error;
+}
diff --git a/third_party/rust/serde_json/src/io/core.rs b/third_party/rust/serde_json/src/io/core.rs
new file mode 100644
index 0000000000..465ab8b249
--- /dev/null
+++ b/third_party/rust/serde_json/src/io/core.rs
@@ -0,0 +1,79 @@
+//! Reimplements core logic and types from `std::io` in an `alloc`-friendly
+//! fashion.
+
+use alloc::vec::Vec;
+use core::fmt::{self, Display};
+use core::result;
+
+pub enum ErrorKind {
+ Other,
+}
+
+// IO errors can never occur in no-std mode. All our no-std IO implementations
+// are infallible.
+pub struct Error;
+
+impl Display for Error {
+ fn fmt(&self, _formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ unreachable!()
+ }
+}
+
+impl Error {
+ pub(crate) fn new(_kind: ErrorKind, _error: &'static str) -> Error {
+ Error
+ }
+}
+
+pub type Result<T> = result::Result<T, Error>;
+
+pub trait Write {
+ fn write(&mut self, buf: &[u8]) -> Result<usize>;
+
+ fn write_all(&mut self, buf: &[u8]) -> Result<()> {
+ // All our Write impls in no_std mode always write the whole buffer in
+ // one call infallibly.
+ let result = self.write(buf);
+ debug_assert!(result.is_ok());
+ debug_assert_eq!(result.unwrap_or(0), buf.len());
+ Ok(())
+ }
+
+ fn flush(&mut self) -> Result<()>;
+}
+
+impl<W: Write> Write for &mut W {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> Result<usize> {
+ (*self).write(buf)
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> Result<()> {
+ (*self).write_all(buf)
+ }
+
+ #[inline]
+ fn flush(&mut self) -> Result<()> {
+ (*self).flush()
+ }
+}
+
+impl Write for Vec<u8> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> Result<usize> {
+ self.extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> Result<()> {
+ self.extend_from_slice(buf);
+ Ok(())
+ }
+
+ #[inline]
+ fn flush(&mut self) -> Result<()> {
+ Ok(())
+ }
+}
diff --git a/third_party/rust/serde_json/src/io/mod.rs b/third_party/rust/serde_json/src/io/mod.rs
new file mode 100644
index 0000000000..9dee4a0685
--- /dev/null
+++ b/third_party/rust/serde_json/src/io/mod.rs
@@ -0,0 +1,20 @@
+//! A tiny, `no_std`-friendly facade around `std::io`.
+//! Reexports types from `std` when available; otherwise reimplements and
+//! provides some of the core logic.
+//!
+//! The main reason that `std::io` hasn't found itself reexported as part of
+//! the `core` crate is the `std::io::{Read, Write}` traits' reliance on
+//! `std::io::Error`, which may contain internally a heap-allocated `Box<Error>`
+//! and/or now relying on OS-specific `std::backtrace::Backtrace`.
+
+pub use self::imp::{Error, ErrorKind, Result, Write};
+
+#[cfg(not(feature = "std"))]
+#[path = "core.rs"]
+mod imp;
+
+#[cfg(feature = "std")]
+use std::io as imp;
+
+#[cfg(feature = "std")]
+pub use std::io::{Bytes, Read};
diff --git a/third_party/rust/serde_json/src/iter.rs b/third_party/rust/serde_json/src/iter.rs
new file mode 100644
index 0000000000..9792916dcc
--- /dev/null
+++ b/third_party/rust/serde_json/src/iter.rs
@@ -0,0 +1,70 @@
+use crate::io;
+
+pub struct LineColIterator<I> {
+ iter: I,
+
+ /// Index of the current line. Characters in the first line of the input
+ /// (before the first newline character) are in line 1.
+ line: usize,
+
+ /// Index of the current column. The first character in the input and any
+ /// characters immediately following a newline character are in column 1.
+ /// The column is 0 immediately after a newline character has been read.
+ col: usize,
+
+ /// Byte offset of the start of the current line. This is the sum of lengths
+ /// of all previous lines. Keeping track of things this way allows efficient
+ /// computation of the current line, column, and byte offset while only
+ /// updating one of the counters in `next()` in the common case.
+ start_of_line: usize,
+}
+
+impl<I> LineColIterator<I>
+where
+ I: Iterator<Item = io::Result<u8>>,
+{
+ pub fn new(iter: I) -> LineColIterator<I> {
+ LineColIterator {
+ iter,
+ line: 1,
+ col: 0,
+ start_of_line: 0,
+ }
+ }
+
+ pub fn line(&self) -> usize {
+ self.line
+ }
+
+ pub fn col(&self) -> usize {
+ self.col
+ }
+
+ pub fn byte_offset(&self) -> usize {
+ self.start_of_line + self.col
+ }
+}
+
+impl<I> Iterator for LineColIterator<I>
+where
+ I: Iterator<Item = io::Result<u8>>,
+{
+ type Item = io::Result<u8>;
+
+ fn next(&mut self) -> Option<io::Result<u8>> {
+ match self.iter.next() {
+ None => None,
+ Some(Ok(b'\n')) => {
+ self.start_of_line += self.col + 1;
+ self.line += 1;
+ self.col = 0;
+ Some(Ok(b'\n'))
+ }
+ Some(Ok(c)) => {
+ self.col += 1;
+ Some(Ok(c))
+ }
+ Some(Err(e)) => Some(Err(e)),
+ }
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/algorithm.rs b/third_party/rust/serde_json/src/lexical/algorithm.rs
new file mode 100644
index 0000000000..a2cbf18aff
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/algorithm.rs
@@ -0,0 +1,193 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Algorithms to efficiently convert strings to floats.
+
+use super::bhcomp::*;
+use super::cached::*;
+use super::errors::*;
+use super::float::ExtendedFloat;
+use super::num::*;
+use super::small_powers::*;
+
+// FAST
+// ----
+
+/// Convert mantissa to exact value for a non-base2 power.
+///
+/// Returns the resulting float and if the value can be represented exactly.
+pub(crate) fn fast_path<F>(mantissa: u64, exponent: i32) -> Option<F>
+where
+ F: Float,
+{
+ // `mantissa >> (F::MANTISSA_SIZE+1) != 0` effectively checks if the
+ // value has a no bits above the hidden bit, which is what we want.
+ let (min_exp, max_exp) = F::exponent_limit();
+ let shift_exp = F::mantissa_limit();
+ let mantissa_size = F::MANTISSA_SIZE + 1;
+ if mantissa == 0 {
+ Some(F::ZERO)
+ } else if mantissa >> mantissa_size != 0 {
+ // Would require truncation of the mantissa.
+ None
+ } else if exponent == 0 {
+ // 0 exponent, same as value, exact representation.
+ let float = F::as_cast(mantissa);
+ Some(float)
+ } else if exponent >= min_exp && exponent <= max_exp {
+ // Value can be exactly represented, return the value.
+ // Do not use powi, since powi can incrementally introduce
+ // error.
+ let float = F::as_cast(mantissa);
+ Some(float.pow10(exponent))
+ } else if exponent >= 0 && exponent <= max_exp + shift_exp {
+ // Check to see if we have a disguised fast-path, where the
+ // number of digits in the mantissa is very small, but and
+ // so digits can be shifted from the exponent to the mantissa.
+ // https://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
+ let small_powers = POW10_64;
+ let shift = exponent - max_exp;
+ let power = small_powers[shift as usize];
+
+ // Compute the product of the power, if it overflows,
+ // prematurely return early, otherwise, if we didn't overshoot,
+ // we can get an exact value.
+ let value = mantissa.checked_mul(power)?;
+ if value >> mantissa_size != 0 {
+ None
+ } else {
+ // Use powi, since it's correct, and faster on
+ // the fast-path.
+ let float = F::as_cast(value);
+ Some(float.pow10(max_exp))
+ }
+ } else {
+ // Cannot be exactly represented, exponent too small or too big,
+ // would require truncation.
+ None
+ }
+}
+
+// MODERATE
+// --------
+
+/// Multiply the floating-point by the exponent.
+///
+/// Multiply by pre-calculated powers of the base, modify the extended-
+/// float, and return if new value and if the value can be represented
+/// accurately.
+fn multiply_exponent_extended<F>(fp: &mut ExtendedFloat, exponent: i32, truncated: bool) -> bool
+where
+ F: Float,
+{
+ let powers = ExtendedFloat::get_powers();
+ let exponent = exponent.saturating_add(powers.bias);
+ let small_index = exponent % powers.step;
+ let large_index = exponent / powers.step;
+ if exponent < 0 {
+ // Guaranteed underflow (assign 0).
+ fp.mant = 0;
+ true
+ } else if large_index as usize >= powers.large.len() {
+ // Overflow (assign infinity)
+ fp.mant = 1 << 63;
+ fp.exp = 0x7FF;
+ true
+ } else {
+ // Within the valid exponent range, multiply by the large and small
+ // exponents and return the resulting value.
+
+ // Track errors to as a factor of unit in last-precision.
+ let mut errors: u32 = 0;
+ if truncated {
+ errors += u64::error_halfscale();
+ }
+
+ // Multiply by the small power.
+ // Check if we can directly multiply by an integer, if not,
+ // use extended-precision multiplication.
+ match fp
+ .mant
+ .overflowing_mul(powers.get_small_int(small_index as usize))
+ {
+ // Overflow, multiplication unsuccessful, go slow path.
+ (_, true) => {
+ fp.normalize();
+ fp.imul(&powers.get_small(small_index as usize));
+ errors += u64::error_halfscale();
+ }
+ // No overflow, multiplication successful.
+ (mant, false) => {
+ fp.mant = mant;
+ fp.normalize();
+ }
+ }
+
+ // Multiply by the large power
+ fp.imul(&powers.get_large(large_index as usize));
+ if errors > 0 {
+ errors += 1;
+ }
+ errors += u64::error_halfscale();
+
+ // Normalize the floating point (and the errors).
+ let shift = fp.normalize();
+ errors <<= shift;
+
+ u64::error_is_accurate::<F>(errors, fp)
+ }
+}
+
+/// Create a precise native float using an intermediate extended-precision float.
+///
+/// Return the float approximation and if the value can be accurately
+/// represented with mantissa bits of precision.
+#[inline]
+pub(crate) fn moderate_path<F>(
+ mantissa: u64,
+ exponent: i32,
+ truncated: bool,
+) -> (ExtendedFloat, bool)
+where
+ F: Float,
+{
+ let mut fp = ExtendedFloat {
+ mant: mantissa,
+ exp: 0,
+ };
+ let valid = multiply_exponent_extended::<F>(&mut fp, exponent, truncated);
+ (fp, valid)
+}
+
+// FALLBACK
+// --------
+
+/// Fallback path when the fast path does not work.
+///
+/// Uses the moderate path, if applicable, otherwise, uses the slow path
+/// as required.
+pub(crate) fn fallback_path<F>(
+ integer: &[u8],
+ fraction: &[u8],
+ mantissa: u64,
+ exponent: i32,
+ mantissa_exponent: i32,
+ truncated: bool,
+) -> F
+where
+ F: Float,
+{
+ // Moderate path (use an extended 80-bit representation).
+ let (fp, valid) = moderate_path::<F>(mantissa, mantissa_exponent, truncated);
+ if valid {
+ return fp.into_float::<F>();
+ }
+
+ // Slow path, fast path didn't work.
+ let b = fp.into_downward_float::<F>();
+ if b.is_special() {
+ // We have a non-finite number, we get to leave early.
+ b
+ } else {
+ bhcomp(b, integer, fraction, exponent)
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/bhcomp.rs b/third_party/rust/serde_json/src/lexical/bhcomp.rs
new file mode 100644
index 0000000000..1f2a7bbdee
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/bhcomp.rs
@@ -0,0 +1,218 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Compare the mantissa to the halfway representation of the float.
+//!
+//! Compares the actual significant digits of the mantissa to the
+//! theoretical digits from `b+h`, scaled into the proper range.
+
+use super::bignum::*;
+use super::digit::*;
+use super::exponent::*;
+use super::float::*;
+use super::math::*;
+use super::num::*;
+use super::rounding::*;
+use core::{cmp, mem};
+
+// MANTISSA
+
+/// Parse the full mantissa into a big integer.
+///
+/// Max digits is the maximum number of digits plus one.
+fn parse_mantissa<F>(integer: &[u8], fraction: &[u8]) -> Bigint
+where
+ F: Float,
+{
+ // Main loop
+ let small_powers = POW10_LIMB;
+ let step = small_powers.len() - 2;
+ let max_digits = F::MAX_DIGITS - 1;
+ let mut counter = 0;
+ let mut value: Limb = 0;
+ let mut i: usize = 0;
+ let mut result = Bigint::default();
+
+ // Iteratively process all the data in the mantissa.
+ for &digit in integer.iter().chain(fraction) {
+ // We've parsed the max digits using small values, add to bignum
+ if counter == step {
+ result.imul_small(small_powers[counter]);
+ result.iadd_small(value);
+ counter = 0;
+ value = 0;
+ }
+
+ value *= 10;
+ value += as_limb(to_digit(digit).unwrap());
+
+ i += 1;
+ counter += 1;
+ if i == max_digits {
+ break;
+ }
+ }
+
+ // We will always have a remainder, as long as we entered the loop
+ // once, or counter % step is 0.
+ if counter != 0 {
+ result.imul_small(small_powers[counter]);
+ result.iadd_small(value);
+ }
+
+ // If we have any remaining digits after the last value, we need
+ // to add a 1 after the rest of the array, it doesn't matter where,
+ // just move it up. This is good for the worst-possible float
+ // representation. We also need to return an index.
+ // Since we already trimmed trailing zeros, we know there has
+ // to be a non-zero digit if there are any left.
+ if i < integer.len() + fraction.len() {
+ result.imul_small(10);
+ result.iadd_small(1);
+ }
+
+ result
+}
+
+// FLOAT OPS
+
+/// Calculate `b` from a a representation of `b` as a float.
+#[inline]
+pub(super) fn b_extended<F: Float>(f: F) -> ExtendedFloat {
+ ExtendedFloat::from_float(f)
+}
+
+/// Calculate `b+h` from a a representation of `b` as a float.
+#[inline]
+pub(super) fn bh_extended<F: Float>(f: F) -> ExtendedFloat {
+ // None of these can overflow.
+ let b = b_extended(f);
+ ExtendedFloat {
+ mant: (b.mant << 1) + 1,
+ exp: b.exp - 1,
+ }
+}
+
+// ROUNDING
+
+/// Custom round-nearest, tie-event algorithm for bhcomp.
+#[inline]
+fn round_nearest_tie_even(fp: &mut ExtendedFloat, shift: i32, is_truncated: bool) {
+ let (mut is_above, mut is_halfway) = round_nearest(fp, shift);
+ if is_halfway && is_truncated {
+ is_above = true;
+ is_halfway = false;
+ }
+ tie_even(fp, is_above, is_halfway);
+}
+
+// BHCOMP
+
+/// Calculate the mantissa for a big integer with a positive exponent.
+fn large_atof<F>(mantissa: Bigint, exponent: i32) -> F
+where
+ F: Float,
+{
+ let bits = mem::size_of::<u64>() * 8;
+
+ // Simple, we just need to multiply by the power of the radix.
+ // Now, we can calculate the mantissa and the exponent from this.
+ // The binary exponent is the binary exponent for the mantissa
+ // shifted to the hidden bit.
+ let mut bigmant = mantissa;
+ bigmant.imul_pow10(exponent as u32);
+
+ // Get the exact representation of the float from the big integer.
+ let (mant, is_truncated) = bigmant.hi64();
+ let exp = bigmant.bit_length() as i32 - bits as i32;
+ let mut fp = ExtendedFloat { mant, exp };
+ fp.round_to_native::<F, _>(|fp, shift| round_nearest_tie_even(fp, shift, is_truncated));
+ into_float(fp)
+}
+
+/// Calculate the mantissa for a big integer with a negative exponent.
+///
+/// This invokes the comparison with `b+h`.
+fn small_atof<F>(mantissa: Bigint, exponent: i32, f: F) -> F
+where
+ F: Float,
+{
+ // Get the significant digits and radix exponent for the real digits.
+ let mut real_digits = mantissa;
+ let real_exp = exponent;
+ debug_assert!(real_exp < 0);
+
+ // Get the significant digits and the binary exponent for `b+h`.
+ let theor = bh_extended(f);
+ let mut theor_digits = Bigint::from_u64(theor.mant);
+ let theor_exp = theor.exp;
+
+ // We need to scale the real digits and `b+h` digits to be the same
+ // order. We currently have `real_exp`, in `radix`, that needs to be
+ // shifted to `theor_digits` (since it is negative), and `theor_exp`
+ // to either `theor_digits` or `real_digits` as a power of 2 (since it
+ // may be positive or negative). Try to remove as many powers of 2
+ // as possible. All values are relative to `theor_digits`, that is,
+ // reflect the power you need to multiply `theor_digits` by.
+
+ // Can remove a power-of-two, since the radix is 10.
+ // Both are on opposite-sides of equation, can factor out a
+ // power of two.
+ //
+ // Example: 10^-10, 2^-10 -> ( 0, 10, 0)
+ // Example: 10^-10, 2^-15 -> (-5, 10, 0)
+ // Example: 10^-10, 2^-5 -> ( 5, 10, 0)
+ // Example: 10^-10, 2^5 -> (15, 10, 0)
+ let binary_exp = theor_exp - real_exp;
+ let halfradix_exp = -real_exp;
+ let radix_exp = 0;
+
+ // Carry out our multiplication.
+ if halfradix_exp != 0 {
+ theor_digits.imul_pow5(halfradix_exp as u32);
+ }
+ if radix_exp != 0 {
+ theor_digits.imul_pow10(radix_exp as u32);
+ }
+ if binary_exp > 0 {
+ theor_digits.imul_pow2(binary_exp as u32);
+ } else if binary_exp < 0 {
+ real_digits.imul_pow2(-binary_exp as u32);
+ }
+
+ // Compare real digits to theoretical digits and round the float.
+ match real_digits.compare(&theor_digits) {
+ cmp::Ordering::Greater => f.next_positive(),
+ cmp::Ordering::Less => f,
+ cmp::Ordering::Equal => f.round_positive_even(),
+ }
+}
+
+/// Calculate the exact value of the float.
+///
+/// Note: fraction must not have trailing zeros.
+pub(crate) fn bhcomp<F>(b: F, integer: &[u8], mut fraction: &[u8], exponent: i32) -> F
+where
+ F: Float,
+{
+ // Calculate the number of integer digits and use that to determine
+ // where the significant digits start in the fraction.
+ let integer_digits = integer.len();
+ let fraction_digits = fraction.len();
+ let digits_start = if integer_digits == 0 {
+ let start = fraction.iter().take_while(|&x| *x == b'0').count();
+ fraction = &fraction[start..];
+ start
+ } else {
+ 0
+ };
+ let sci_exp = scientific_exponent(exponent, integer_digits, digits_start);
+ let count = F::MAX_DIGITS.min(integer_digits + fraction_digits - digits_start);
+ let scaled_exponent = sci_exp + 1 - count as i32;
+
+ let mantissa = parse_mantissa::<F>(integer, fraction);
+ if scaled_exponent >= 0 {
+ large_atof(mantissa, scaled_exponent)
+ } else {
+ small_atof(mantissa, scaled_exponent, b)
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/bignum.rs b/third_party/rust/serde_json/src/lexical/bignum.rs
new file mode 100644
index 0000000000..f9551f534f
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/bignum.rs
@@ -0,0 +1,33 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Big integer type definition.
+
+use super::math::*;
+use alloc::vec::Vec;
+
+/// Storage for a big integer type.
+#[derive(Clone, PartialEq, Eq)]
+pub(crate) struct Bigint {
+ /// Internal storage for the Bigint, in little-endian order.
+ pub(crate) data: Vec<Limb>,
+}
+
+impl Default for Bigint {
+ fn default() -> Self {
+ Bigint {
+ data: Vec::with_capacity(20),
+ }
+ }
+}
+
+impl Math for Bigint {
+ #[inline]
+ fn data(&self) -> &Vec<Limb> {
+ &self.data
+ }
+
+ #[inline]
+ fn data_mut(&mut self) -> &mut Vec<Limb> {
+ &mut self.data
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/cached.rs b/third_party/rust/serde_json/src/lexical/cached.rs
new file mode 100644
index 0000000000..ef5a9fe54d
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/cached.rs
@@ -0,0 +1,82 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Cached powers trait for extended-precision floats.
+
+use super::cached_float80;
+use super::float::ExtendedFloat;
+
+// POWERS
+
+/// Precalculated powers that uses two-separate arrays for memory-efficiency.
+#[doc(hidden)]
+pub(crate) struct ExtendedFloatArray {
+ // Pre-calculated mantissa for the powers.
+ pub mant: &'static [u64],
+ // Pre-calculated binary exponents for the powers.
+ pub exp: &'static [i32],
+}
+
+/// Allow indexing of values without bounds checking
+impl ExtendedFloatArray {
+ #[inline]
+ pub fn get_extended_float(&self, index: usize) -> ExtendedFloat {
+ let mant = self.mant[index];
+ let exp = self.exp[index];
+ ExtendedFloat { mant, exp }
+ }
+
+ #[inline]
+ pub fn len(&self) -> usize {
+ self.mant.len()
+ }
+}
+
+// MODERATE PATH POWERS
+
+/// Precalculated powers of base N for the moderate path.
+#[doc(hidden)]
+pub(crate) struct ModeratePathPowers {
+ // Pre-calculated small powers.
+ pub small: ExtendedFloatArray,
+ // Pre-calculated large powers.
+ pub large: ExtendedFloatArray,
+ /// Pre-calculated small powers as 64-bit integers
+ pub small_int: &'static [u64],
+ // Step between large powers and number of small powers.
+ pub step: i32,
+ // Exponent bias for the large powers.
+ pub bias: i32,
+}
+
+/// Allow indexing of values without bounds checking
+impl ModeratePathPowers {
+ #[inline]
+ pub fn get_small(&self, index: usize) -> ExtendedFloat {
+ self.small.get_extended_float(index)
+ }
+
+ #[inline]
+ pub fn get_large(&self, index: usize) -> ExtendedFloat {
+ self.large.get_extended_float(index)
+ }
+
+ #[inline]
+ pub fn get_small_int(&self, index: usize) -> u64 {
+ self.small_int[index]
+ }
+}
+
+// CACHED EXTENDED POWERS
+
+/// Cached powers as a trait for a floating-point type.
+pub(crate) trait ModeratePathCache {
+ /// Get cached powers.
+ fn get_powers() -> &'static ModeratePathPowers;
+}
+
+impl ModeratePathCache for ExtendedFloat {
+ #[inline]
+ fn get_powers() -> &'static ModeratePathPowers {
+ cached_float80::get_powers()
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/cached_float80.rs b/third_party/rust/serde_json/src/lexical/cached_float80.rs
new file mode 100644
index 0000000000..9beda3ddb1
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/cached_float80.rs
@@ -0,0 +1,206 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Cached exponents for basen values with 80-bit extended floats.
+//!
+//! Exact versions of base**n as an extended-precision float, with both
+//! large and small powers. Use the large powers to minimize the amount
+//! of compounded error.
+//!
+//! These values were calculated using Python, using the arbitrary-precision
+//! integer to calculate exact extended-representation of each value.
+//! These values are all normalized.
+
+use super::cached::{ExtendedFloatArray, ModeratePathPowers};
+
+// LOW-LEVEL
+// ---------
+
+// BASE10
+
+const BASE10_SMALL_MANTISSA: [u64; 10] = [
+ 9223372036854775808, // 10^0
+ 11529215046068469760, // 10^1
+ 14411518807585587200, // 10^2
+ 18014398509481984000, // 10^3
+ 11258999068426240000, // 10^4
+ 14073748835532800000, // 10^5
+ 17592186044416000000, // 10^6
+ 10995116277760000000, // 10^7
+ 13743895347200000000, // 10^8
+ 17179869184000000000, // 10^9
+];
+const BASE10_SMALL_EXPONENT: [i32; 10] = [
+ -63, // 10^0
+ -60, // 10^1
+ -57, // 10^2
+ -54, // 10^3
+ -50, // 10^4
+ -47, // 10^5
+ -44, // 10^6
+ -40, // 10^7
+ -37, // 10^8
+ -34, // 10^9
+];
+const BASE10_LARGE_MANTISSA: [u64; 66] = [
+ 11555125961253852697, // 10^-350
+ 13451937075301367670, // 10^-340
+ 15660115838168849784, // 10^-330
+ 18230774251475056848, // 10^-320
+ 10611707258198326947, // 10^-310
+ 12353653155963782858, // 10^-300
+ 14381545078898527261, // 10^-290
+ 16742321987285426889, // 10^-280
+ 9745314011399999080, // 10^-270
+ 11345038669416679861, // 10^-260
+ 13207363278391631158, // 10^-250
+ 15375394465392026070, // 10^-240
+ 17899314949046850752, // 10^-230
+ 10418772551374772303, // 10^-220
+ 12129047596099288555, // 10^-210
+ 14120069793541087484, // 10^-200
+ 16437924692338667210, // 10^-190
+ 9568131466127621947, // 10^-180
+ 11138771039116687545, // 10^-170
+ 12967236152753102995, // 10^-160
+ 15095849699286165408, // 10^-150
+ 17573882009934360870, // 10^-140
+ 10229345649675443343, // 10^-130
+ 11908525658859223294, // 10^-120
+ 13863348470604074297, // 10^-110
+ 16139061738043178685, // 10^-100
+ 9394170331095332911, // 10^-90
+ 10936253623915059621, // 10^-80
+ 12731474852090538039, // 10^-70
+ 14821387422376473014, // 10^-60
+ 17254365866976409468, // 10^-50
+ 10043362776618689222, // 10^-40
+ 11692013098647223345, // 10^-30
+ 13611294676837538538, // 10^-20
+ 15845632502852867518, // 10^-10
+ 9223372036854775808, // 10^0
+ 10737418240000000000, // 10^10
+ 12500000000000000000, // 10^20
+ 14551915228366851806, // 10^30
+ 16940658945086006781, // 10^40
+ 9860761315262647567, // 10^50
+ 11479437019748901445, // 10^60
+ 13363823550460978230, // 10^70
+ 15557538194652854267, // 10^80
+ 18111358157653424735, // 10^90
+ 10542197943230523224, // 10^100
+ 12272733663244316382, // 10^110
+ 14287342391028437277, // 10^120
+ 16632655625031838749, // 10^130
+ 9681479787123295682, // 10^140
+ 11270725851789228247, // 10^150
+ 13120851772591970218, // 10^160
+ 15274681817498023410, // 10^170
+ 17782069995880619867, // 10^180
+ 10350527006597618960, // 10^190
+ 12049599325514420588, // 10^200
+ 14027579833653779454, // 10^210
+ 16330252207878254650, // 10^220
+ 9505457831475799117, // 10^230
+ 11065809325636130661, // 10^240
+ 12882297539194266616, // 10^250
+ 14996968138956309548, // 10^260
+ 17458768723248864463, // 10^270
+ 10162340898095201970, // 10^280
+ 11830521861667747109, // 10^290
+ 13772540099066387756, // 10^300
+];
+const BASE10_LARGE_EXPONENT: [i32; 66] = [
+ -1226, // 10^-350
+ -1193, // 10^-340
+ -1160, // 10^-330
+ -1127, // 10^-320
+ -1093, // 10^-310
+ -1060, // 10^-300
+ -1027, // 10^-290
+ -994, // 10^-280
+ -960, // 10^-270
+ -927, // 10^-260
+ -894, // 10^-250
+ -861, // 10^-240
+ -828, // 10^-230
+ -794, // 10^-220
+ -761, // 10^-210
+ -728, // 10^-200
+ -695, // 10^-190
+ -661, // 10^-180
+ -628, // 10^-170
+ -595, // 10^-160
+ -562, // 10^-150
+ -529, // 10^-140
+ -495, // 10^-130
+ -462, // 10^-120
+ -429, // 10^-110
+ -396, // 10^-100
+ -362, // 10^-90
+ -329, // 10^-80
+ -296, // 10^-70
+ -263, // 10^-60
+ -230, // 10^-50
+ -196, // 10^-40
+ -163, // 10^-30
+ -130, // 10^-20
+ -97, // 10^-10
+ -63, // 10^0
+ -30, // 10^10
+ 3, // 10^20
+ 36, // 10^30
+ 69, // 10^40
+ 103, // 10^50
+ 136, // 10^60
+ 169, // 10^70
+ 202, // 10^80
+ 235, // 10^90
+ 269, // 10^100
+ 302, // 10^110
+ 335, // 10^120
+ 368, // 10^130
+ 402, // 10^140
+ 435, // 10^150
+ 468, // 10^160
+ 501, // 10^170
+ 534, // 10^180
+ 568, // 10^190
+ 601, // 10^200
+ 634, // 10^210
+ 667, // 10^220
+ 701, // 10^230
+ 734, // 10^240
+ 767, // 10^250
+ 800, // 10^260
+ 833, // 10^270
+ 867, // 10^280
+ 900, // 10^290
+ 933, // 10^300
+];
+const BASE10_SMALL_INT_POWERS: [u64; 10] = [
+ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
+];
+const BASE10_STEP: i32 = 10;
+const BASE10_BIAS: i32 = 350;
+
+// HIGH LEVEL
+// ----------
+
+const BASE10_POWERS: ModeratePathPowers = ModeratePathPowers {
+ small: ExtendedFloatArray {
+ mant: &BASE10_SMALL_MANTISSA,
+ exp: &BASE10_SMALL_EXPONENT,
+ },
+ large: ExtendedFloatArray {
+ mant: &BASE10_LARGE_MANTISSA,
+ exp: &BASE10_LARGE_EXPONENT,
+ },
+ small_int: &BASE10_SMALL_INT_POWERS,
+ step: BASE10_STEP,
+ bias: BASE10_BIAS,
+};
+
+/// Get powers from base.
+pub(crate) fn get_powers() -> &'static ModeratePathPowers {
+ &BASE10_POWERS
+}
diff --git a/third_party/rust/serde_json/src/lexical/digit.rs b/third_party/rust/serde_json/src/lexical/digit.rs
new file mode 100644
index 0000000000..882aa9eef2
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/digit.rs
@@ -0,0 +1,15 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Helpers to convert and add digits from characters.
+
+// Convert u8 to digit.
+#[inline]
+pub(crate) fn to_digit(c: u8) -> Option<u32> {
+ (c as char).to_digit(10)
+}
+
+// Add digit to mantissa.
+#[inline]
+pub(crate) fn add_digit(value: u64, digit: u32) -> Option<u64> {
+ value.checked_mul(10)?.checked_add(digit as u64)
+}
diff --git a/third_party/rust/serde_json/src/lexical/errors.rs b/third_party/rust/serde_json/src/lexical/errors.rs
new file mode 100644
index 0000000000..cad4bd3d58
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/errors.rs
@@ -0,0 +1,133 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Estimate the error in an 80-bit approximation of a float.
+//!
+//! This estimates the error in a floating-point representation.
+//!
+//! This implementation is loosely based off the Golang implementation,
+//! found here:
+//! https://golang.org/src/strconv/atof.go
+
+use super::float::*;
+use super::num::*;
+use super::rounding::*;
+
+pub(crate) trait FloatErrors {
+ /// Get the full error scale.
+ fn error_scale() -> u32;
+ /// Get the half error scale.
+ fn error_halfscale() -> u32;
+ /// Determine if the number of errors is tolerable for float precision.
+ fn error_is_accurate<F: Float>(count: u32, fp: &ExtendedFloat) -> bool;
+}
+
+/// Check if the error is accurate with a round-nearest rounding scheme.
+#[inline]
+fn nearest_error_is_accurate(errors: u64, fp: &ExtendedFloat, extrabits: u64) -> bool {
+ // Round-to-nearest, need to use the halfway point.
+ if extrabits == 65 {
+ // Underflow, we have a shift larger than the mantissa.
+ // Representation is valid **only** if the value is close enough
+ // overflow to the next bit within errors. If it overflows,
+ // the representation is **not** valid.
+ !fp.mant.overflowing_add(errors).1
+ } else {
+ let mask: u64 = lower_n_mask(extrabits);
+ let extra: u64 = fp.mant & mask;
+
+ // Round-to-nearest, need to check if we're close to halfway.
+ // IE, b10100 | 100000, where `|` signifies the truncation point.
+ let halfway: u64 = lower_n_halfway(extrabits);
+ let cmp1 = halfway.wrapping_sub(errors) < extra;
+ let cmp2 = extra < halfway.wrapping_add(errors);
+
+ // If both comparisons are true, we have significant rounding error,
+ // and the value cannot be exactly represented. Otherwise, the
+ // representation is valid.
+ !(cmp1 && cmp2)
+ }
+}
+
+impl FloatErrors for u64 {
+ #[inline]
+ fn error_scale() -> u32 {
+ 8
+ }
+
+ #[inline]
+ fn error_halfscale() -> u32 {
+ u64::error_scale() / 2
+ }
+
+ #[inline]
+ fn error_is_accurate<F: Float>(count: u32, fp: &ExtendedFloat) -> bool {
+ // Determine if extended-precision float is a good approximation.
+ // If the error has affected too many units, the float will be
+ // inaccurate, or if the representation is too close to halfway
+ // that any operations could affect this halfway representation.
+ // See the documentation for dtoa for more information.
+ let bias = -(F::EXPONENT_BIAS - F::MANTISSA_SIZE);
+ let denormal_exp = bias - 63;
+ // This is always a valid u32, since (denormal_exp - fp.exp)
+ // will always be positive and the significand size is {23, 52}.
+ let extrabits = if fp.exp <= denormal_exp {
+ 64 - F::MANTISSA_SIZE + denormal_exp - fp.exp
+ } else {
+ 63 - F::MANTISSA_SIZE
+ };
+
+ // Our logic is as follows: we want to determine if the actual
+ // mantissa and the errors during calculation differ significantly
+ // from the rounding point. The rounding point for round-nearest
+ // is the halfway point, IE, this when the truncated bits start
+ // with b1000..., while the rounding point for the round-toward
+ // is when the truncated bits are equal to 0.
+ // To do so, we can check whether the rounding point +/- the error
+ // are >/< the actual lower n bits.
+ //
+ // For whether we need to use signed or unsigned types for this
+ // analysis, see this example, using u8 rather than u64 to simplify
+ // things.
+ //
+ // # Comparisons
+ // cmp1 = (halfway - errors) < extra
+ // cmp1 = extra < (halfway + errors)
+ //
+ // # Large Extrabits, Low Errors
+ //
+ // extrabits = 8
+ // halfway = 0b10000000
+ // extra = 0b10000010
+ // errors = 0b00000100
+ // halfway - errors = 0b01111100
+ // halfway + errors = 0b10000100
+ //
+ // Unsigned:
+ // halfway - errors = 124
+ // halfway + errors = 132
+ // extra = 130
+ // cmp1 = true
+ // cmp2 = true
+ // Signed:
+ // halfway - errors = 124
+ // halfway + errors = -124
+ // extra = -126
+ // cmp1 = false
+ // cmp2 = true
+ //
+ // # Conclusion
+ //
+ // Since errors will always be small, and since we want to detect
+ // if the representation is accurate, we need to use an **unsigned**
+ // type for comparisons.
+
+ let extrabits = extrabits as u64;
+ let errors = count as u64;
+ if extrabits > 65 {
+ // Underflow, we have a literal 0.
+ return true;
+ }
+
+ nearest_error_is_accurate(errors, fp, extrabits)
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/exponent.rs b/third_party/rust/serde_json/src/lexical/exponent.rs
new file mode 100644
index 0000000000..6fc51977ed
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/exponent.rs
@@ -0,0 +1,50 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Utilities to calculate exponents.
+
+/// Convert usize into i32 without overflow.
+///
+/// This is needed to ensure when adjusting the exponent relative to
+/// the mantissa we do not overflow for comically-long exponents.
+#[inline]
+fn into_i32(value: usize) -> i32 {
+ if value > i32::max_value() as usize {
+ i32::max_value()
+ } else {
+ value as i32
+ }
+}
+
+// EXPONENT CALCULATION
+
+// Calculate the scientific notation exponent without overflow.
+//
+// For example, 0.1 would be -1, and 10 would be 1 in base 10.
+#[inline]
+pub(crate) fn scientific_exponent(
+ exponent: i32,
+ integer_digits: usize,
+ fraction_start: usize,
+) -> i32 {
+ if integer_digits == 0 {
+ let fraction_start = into_i32(fraction_start);
+ exponent.saturating_sub(fraction_start).saturating_sub(1)
+ } else {
+ let integer_shift = into_i32(integer_digits - 1);
+ exponent.saturating_add(integer_shift)
+ }
+}
+
+// Calculate the mantissa exponent without overflow.
+//
+// Remove the number of digits that contributed to the mantissa past
+// the dot, and add the number of truncated digits from the mantissa,
+// to calculate the scaling factor for the mantissa from a raw exponent.
+#[inline]
+pub(crate) fn mantissa_exponent(exponent: i32, fraction_digits: usize, truncated: usize) -> i32 {
+ if fraction_digits > truncated {
+ exponent.saturating_sub(into_i32(fraction_digits - truncated))
+ } else {
+ exponent.saturating_add(into_i32(truncated - fraction_digits))
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/float.rs b/third_party/rust/serde_json/src/lexical/float.rs
new file mode 100644
index 0000000000..2d434a29cb
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/float.rs
@@ -0,0 +1,183 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+// FLOAT TYPE
+
+use super::num::*;
+use super::rounding::*;
+use super::shift::*;
+
+/// Extended precision floating-point type.
+///
+/// Private implementation, exposed only for testing purposes.
+#[doc(hidden)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) struct ExtendedFloat {
+ /// Mantissa for the extended-precision float.
+ pub mant: u64,
+ /// Binary exponent for the extended-precision float.
+ pub exp: i32,
+}
+
+impl ExtendedFloat {
+ // PROPERTIES
+
+ // OPERATIONS
+
+ /// Multiply two normalized extended-precision floats, as if by `a*b`.
+ ///
+ /// The precision is maximal when the numbers are normalized, however,
+ /// decent precision will occur as long as both values have high bits
+ /// set. The result is not normalized.
+ ///
+ /// Algorithm:
+ /// 1. Non-signed multiplication of mantissas (requires 2x as many bits as input).
+ /// 2. Normalization of the result (not done here).
+ /// 3. Addition of exponents.
+ pub(crate) fn mul(&self, b: &ExtendedFloat) -> ExtendedFloat {
+ // Logic check, values must be decently normalized prior to multiplication.
+ debug_assert!((self.mant & u64::HIMASK != 0) && (b.mant & u64::HIMASK != 0));
+
+ // Extract high-and-low masks.
+ let ah = self.mant >> u64::HALF;
+ let al = self.mant & u64::LOMASK;
+ let bh = b.mant >> u64::HALF;
+ let bl = b.mant & u64::LOMASK;
+
+ // Get our products
+ let ah_bl = ah * bl;
+ let al_bh = al * bh;
+ let al_bl = al * bl;
+ let ah_bh = ah * bh;
+
+ let mut tmp = (ah_bl & u64::LOMASK) + (al_bh & u64::LOMASK) + (al_bl >> u64::HALF);
+ // round up
+ tmp += 1 << (u64::HALF - 1);
+
+ ExtendedFloat {
+ mant: ah_bh + (ah_bl >> u64::HALF) + (al_bh >> u64::HALF) + (tmp >> u64::HALF),
+ exp: self.exp + b.exp + u64::FULL,
+ }
+ }
+
+ /// Multiply in-place, as if by `a*b`.
+ ///
+ /// The result is not normalized.
+ #[inline]
+ pub(crate) fn imul(&mut self, b: &ExtendedFloat) {
+ *self = self.mul(b);
+ }
+
+ // NORMALIZE
+
+ /// Normalize float-point number.
+ ///
+ /// Shift the mantissa so the number of leading zeros is 0, or the value
+ /// itself is 0.
+ ///
+ /// Get the number of bytes shifted.
+ #[inline]
+ pub(crate) fn normalize(&mut self) -> u32 {
+ // Note:
+ // Using the cltz intrinsic via leading_zeros is way faster (~10x)
+ // than shifting 1-bit at a time, via while loop, and also way
+ // faster (~2x) than an unrolled loop that checks at 32, 16, 4,
+ // 2, and 1 bit.
+ //
+ // Using a modulus of pow2 (which will get optimized to a bitwise
+ // and with 0x3F or faster) is slightly slower than an if/then,
+ // however, removing the if/then will likely optimize more branched
+ // code as it removes conditional logic.
+
+ // Calculate the number of leading zeros, and then zero-out
+ // any overflowing bits, to avoid shl overflow when self.mant == 0.
+ let shift = if self.mant == 0 {
+ 0
+ } else {
+ self.mant.leading_zeros()
+ };
+ shl(self, shift as i32);
+ shift
+ }
+
+ // ROUND
+
+ /// Lossy round float-point number to native mantissa boundaries.
+ #[inline]
+ pub(crate) fn round_to_native<F, Algorithm>(&mut self, algorithm: Algorithm)
+ where
+ F: Float,
+ Algorithm: FnOnce(&mut ExtendedFloat, i32),
+ {
+ round_to_native::<F, _>(self, algorithm);
+ }
+
+ // FROM
+
+ /// Create extended float from native float.
+ #[inline]
+ pub fn from_float<F: Float>(f: F) -> ExtendedFloat {
+ from_float(f)
+ }
+
+ // INTO
+
+ /// Convert into default-rounded, lower-precision native float.
+ #[inline]
+ pub(crate) fn into_float<F: Float>(mut self) -> F {
+ self.round_to_native::<F, _>(round_nearest_tie_even);
+ into_float(self)
+ }
+
+ /// Convert into downward-rounded, lower-precision native float.
+ #[inline]
+ pub(crate) fn into_downward_float<F: Float>(mut self) -> F {
+ self.round_to_native::<F, _>(round_downward);
+ into_float(self)
+ }
+}
+
+// FROM FLOAT
+
+// Import ExtendedFloat from native float.
+#[inline]
+pub(crate) fn from_float<F>(f: F) -> ExtendedFloat
+where
+ F: Float,
+{
+ ExtendedFloat {
+ mant: u64::as_cast(f.mantissa()),
+ exp: f.exponent(),
+ }
+}
+
+// INTO FLOAT
+
+// Export extended-precision float to native float.
+//
+// The extended-precision float must be in native float representation,
+// with overflow/underflow appropriately handled.
+#[inline]
+pub(crate) fn into_float<F>(fp: ExtendedFloat) -> F
+where
+ F: Float,
+{
+ // Export floating-point number.
+ if fp.mant == 0 || fp.exp < F::DENORMAL_EXPONENT {
+ // sub-denormal, underflow
+ F::ZERO
+ } else if fp.exp >= F::MAX_EXPONENT {
+ // overflow
+ F::from_bits(F::INFINITY_BITS)
+ } else {
+ // calculate the exp and fraction bits, and return a float from bits.
+ let exp: u64;
+ if (fp.exp == F::DENORMAL_EXPONENT) && (fp.mant & F::HIDDEN_BIT_MASK.as_u64()) == 0 {
+ exp = 0;
+ } else {
+ exp = (fp.exp + F::EXPONENT_BIAS) as u64;
+ }
+ let exp = exp << F::MANTISSA_SIZE;
+ let mant = fp.mant & F::MANTISSA_MASK.as_u64();
+ F::from_bits(F::Unsigned::as_cast(mant | exp))
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/large_powers.rs b/third_party/rust/serde_json/src/lexical/large_powers.rs
new file mode 100644
index 0000000000..c63ce1cf21
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/large_powers.rs
@@ -0,0 +1,9 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Precalculated large powers for limbs.
+
+#[cfg(limb_width_32)]
+pub(crate) use super::large_powers32::*;
+
+#[cfg(limb_width_64)]
+pub(crate) use super::large_powers64::*;
diff --git a/third_party/rust/serde_json/src/lexical/large_powers32.rs b/third_party/rust/serde_json/src/lexical/large_powers32.rs
new file mode 100644
index 0000000000..7991197262
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/large_powers32.rs
@@ -0,0 +1,183 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Precalculated large powers for 32-bit limbs.
+
+/// Large powers (&[u32]) for base5 operations.
+const POW5_1: [u32; 1] = [5];
+const POW5_2: [u32; 1] = [25];
+const POW5_3: [u32; 1] = [625];
+const POW5_4: [u32; 1] = [390625];
+const POW5_5: [u32; 2] = [2264035265, 35];
+const POW5_6: [u32; 3] = [2242703233, 762134875, 1262];
+const POW5_7: [u32; 5] = [3211403009, 1849224548, 3668416493, 3913284084, 1593091];
+const POW5_8: [u32; 10] = [
+ 781532673, 64985353, 253049085, 594863151, 3553621484, 3288652808, 3167596762, 2788392729,
+ 3911132675, 590,
+];
+const POW5_9: [u32; 19] = [
+ 2553183233, 3201533787, 3638140786, 303378311, 1809731782, 3477761648, 3583367183, 649228654,
+ 2915460784, 487929380, 1011012442, 1677677582, 3428152256, 1710878487, 1438394610, 2161952759,
+ 4100910556, 1608314830, 349175,
+];
+const POW5_10: [u32; 38] = [
+ 4234999809, 2012377703, 2408924892, 1570150255, 3090844311, 3273530073, 1187251475, 2498123591,
+ 3364452033, 1148564857, 687371067, 2854068671, 1883165473, 505794538, 2988060450, 3159489326,
+ 2531348317, 3215191468, 849106862, 3892080979, 3288073877, 2242451748, 4183778142, 2995818208,
+ 2477501924, 325481258, 2487842652, 1774082830, 1933815724, 2962865281, 1168579910, 2724829000,
+ 2360374019, 2315984659, 2360052375, 3251779801, 1664357844, 28,
+];
+const POW5_11: [u32; 75] = [
+ 689565697, 4116392818, 1853628763, 516071302, 2568769159, 365238920, 336250165, 1283268122,
+ 3425490969, 248595470, 2305176814, 2111925499, 507770399, 2681111421, 589114268, 591287751,
+ 1708941527, 4098957707, 475844916, 3378731398, 2452339615, 2817037361, 2678008327, 1656645978,
+ 2383430340, 73103988, 448667107, 2329420453, 3124020241, 3625235717, 3208634035, 2412059158,
+ 2981664444, 4117622508, 838560765, 3069470027, 270153238, 1802868219, 3692709886, 2161737865,
+ 2159912357, 2585798786, 837488486, 4237238160, 2540319504, 3798629246, 3748148874, 1021550776,
+ 2386715342, 1973637538, 1823520457, 1146713475, 833971519, 3277251466, 905620390, 26278816,
+ 2680483154, 2294040859, 373297482, 5996609, 4109575006, 512575049, 917036550, 1942311753,
+ 2816916778, 3248920332, 1192784020, 3537586671, 2456567643, 2925660628, 759380297, 888447942,
+ 3559939476, 3654687237, 805,
+];
+const POW5_12: [u32; 149] = [
+ 322166785, 3809044581, 2994556223, 1239584207, 3962455841, 4001882964, 3053876612, 915114683,
+ 2783289745, 785739093, 4253185907, 3931164994, 1370983858, 2553556126, 3360742076, 2255410929,
+ 422849554, 2457422215, 3539495362, 1720790602, 1908931983, 1470596141, 592794347, 4219465164,
+ 4085652704, 941661409, 2534650953, 885063988, 2355909854, 2812815516, 767256131, 3821757683,
+ 2155151105, 3817418473, 281116564, 2834395026, 2821201622, 2524625843, 1511330880, 2572352493,
+ 330571332, 2951088579, 2730271766, 4044456479, 4212286644, 2444937588, 3603420843, 2387148597,
+ 1142537539, 3299235429, 1751012624, 861228086, 2873722519, 230498814, 1023297821, 2553128038,
+ 3421129895, 2651917435, 2042981258, 1606787143, 2228751918, 447345732, 1930371132, 1784132011,
+ 3612538790, 2275925090, 2487567871, 1080427616, 2009179183, 3383506781, 3899054063, 1950782960,
+ 2168622213, 2717674390, 3616636027, 2079341593, 1530129217, 1461057425, 2406264415, 3674671357,
+ 2972036238, 2019354295, 1455849819, 1866918619, 1324269294, 424891864, 2722422332, 2641594816,
+ 1400249021, 3482963993, 3734946379, 225889849, 1891545473, 777383150, 3589824633, 4117601611,
+ 4220028667, 334453379, 1083130821, 1060342180, 4208163139, 1489826908, 4163762246, 1096580926,
+ 689301528, 2336054516, 1782865703, 4175148410, 3398369392, 2329412588, 3001580596, 59740741,
+ 3202189932, 3351895776, 246185302, 718535188, 3772647488, 4151666556, 4055698133, 2461934110,
+ 2281316281, 3466396836, 3536023465, 1064267812, 2955456354, 2423805422, 3627960790, 1325057500,
+ 3876919979, 2009959531, 175455101, 184092852, 2358785571, 3842977831, 2485266289, 487121622,
+ 4159252710, 4075707558, 459389244, 300652075, 2521346588, 3458976673, 888631636, 2076098096,
+ 3844514585, 2363697580, 3729421522, 3051115477, 649395,
+];
+const POW5_13: [u32; 298] = [
+ 711442433, 3564261005, 2399042279, 4170849936, 4010295575, 1423987028, 330414929, 1349249065,
+ 4213813618, 3852031822, 4040843590, 2154565331, 3094013374, 1159028371, 3227065538, 2115927092,
+ 2085102554, 488590542, 2609619432, 3602898805, 3812736528, 3269439096, 23816114, 253984538,
+ 1035905997, 2942969204, 3400787671, 338562688, 1637191975, 740509713, 2264962817, 3410753922,
+ 4162231428, 2282041228, 1759373012, 3155367777, 4278913285, 1420532801, 1981002276, 438054990,
+ 1006507643, 1142697287, 1332538012, 2029019521, 3949305784, 818392641, 2491288846, 2716584663,
+ 3648886102, 556814413, 444795339, 4071412999, 1066321706, 4253169466, 2510832316, 672091442,
+ 4083256000, 2165985028, 1841538484, 3549854235, 364431512, 3707648143, 1162785440, 2268641545,
+ 281340310, 735693841, 848809228, 1700785200, 2919703985, 4094234344, 58530286, 965505005,
+ 1000010347, 3381961808, 3040089923, 1973852082, 2890971585, 1019960210, 4292895237, 2821887841,
+ 3756675650, 3951282907, 3885870583, 1008791145, 503998487, 1881258362, 1949332730, 392996726,
+ 2012973814, 3970014187, 2461725150, 2942547730, 3728066699, 2766901132, 3778532841, 1085564064,
+ 2278673896, 1116879805, 3448726271, 774279411, 157211670, 1506320155, 531168605, 1362654525,
+ 956967721, 2148871960, 769186085, 4186232894, 2055679604, 3248365487, 3981268013, 3975787984,
+ 2489510517, 3309046495, 212771124, 933418041, 3371839114, 562115198, 1853601831, 757336096,
+ 1354633440, 1486083256, 2872126393, 522920738, 1141587749, 3210903262, 1926940553, 3054024853,
+ 2021162538, 2262742000, 1877899947, 3147002868, 669840763, 4158174590, 4238502559, 1023731922,
+ 3386840011, 829588074, 3449720188, 2835142880, 2999162007, 813056473, 482949569, 638108879,
+ 3067201471, 1026714238, 4004452838, 2383667807, 3999477803, 771648919, 630660440, 3827121348,
+ 176185980, 2878191002, 2666149832, 3909811063, 2429163983, 2665690412, 907266128, 4269332098,
+ 2022665808, 1527122180, 3072053668, 1072477492, 3006022924, 549664855, 2800340954, 37352654,
+ 1212772743, 2711280533, 3029527946, 2511120040, 1305308377, 3474662224, 4226330922, 442988428,
+ 954940108, 3274548099, 4212288177, 2688499880, 3982226758, 3922609956, 1279948029, 1939943640,
+ 3650489901, 2733364929, 2494263275, 1864579964, 1225941120, 2390465139, 1267503249, 3533240729,
+ 904410805, 2842550015, 2517736241, 1796069820, 3335274381, 673539835, 1924694759, 3598098235,
+ 2792633405, 16535707, 3703535497, 3592841791, 2929082877, 1317622811, 294990855, 1396706563,
+ 2383271770, 3853857605, 277813677, 277580220, 1101318484, 3761974115, 1132150143, 2544692622,
+ 3419825776, 743770306, 1695464553, 1548693232, 2421159615, 2575672031, 2678971806, 1591267897,
+ 626546738, 3823443129, 267710932, 1455435162, 2353985540, 3248523795, 335348168, 3872552561,
+ 2814522612, 2634118860, 3503767026, 1301019273, 1414467789, 722985138, 3070909565, 4253482569,
+ 3744939841, 558142907, 2229819389, 13833173, 77003966, 2763671364, 3905603970, 2931990126,
+ 2280419384, 1879090457, 2934846267, 4284933164, 2331863845, 62191163, 3178861020, 1522063815,
+ 785672270, 1215568492, 2936443917, 802972489, 2956820173, 3916732783, 2893572089, 1391232801,
+ 3168640330, 2396859648, 894950918, 1103583736, 961991865, 2807302642, 305977505, 3054505899,
+ 1048256994, 781017659, 2459278754, 3164823415, 537658277, 905753687, 464963300, 4149131560,
+ 1029507924, 2278300961, 1231291503, 414073408, 3630740085, 2345841814, 475358196, 3258243317,
+ 4167625072, 4178911231, 2927355042, 655438830, 3138378018, 623200562, 2785714112, 273403236,
+ 807993669, 98,
+];
+const POW5_14: [u32; 595] = [
+ 1691320321, 2671006246, 1682531301, 2072858707, 1240508969, 3108358191, 1125119096, 2470144952,
+ 1610099978, 1690632660, 1941696884, 2663506355, 1006364675, 3909158537, 4147711374, 1072663936,
+ 4078768933, 745751659, 4123687570, 471458681, 655028926, 4113407388, 3945524552, 985625313,
+ 1254424514, 2127508744, 570530434, 945388122, 3194649404, 2589065070, 2731705399, 202030749,
+ 2090780394, 3348662271, 1481754777, 1130635472, 4025144705, 1924486271, 2578567861, 125491448,
+ 1558036315, 994248173, 3817216711, 763950077, 1030439870, 959586474, 3845661701, 483795093,
+ 1637944470, 2275463649, 3398804829, 1758016486, 2665513698, 2004912571, 1094885097, 4223064276,
+ 3307819021, 651121777, 1757003305, 3603542336, 129917786, 2215974994, 3042386306, 2205352757,
+ 3944939700, 3710987569, 97967515, 1217242524, 930630949, 3660328512, 1787663098, 1784141600,
+ 2500542892, 4034561586, 3444961378, 785043562, 3869499367, 885623728, 2625011087, 3053789617,
+ 1965731793, 3900511934, 2648823592, 3851062028, 3321968688, 799195417, 1011847510, 1369129160,
+ 1348009103, 2876796955, 2915408967, 3305284948, 263399535, 1715990604, 2645821294, 1587844552,
+ 2624912049, 3035631499, 2306636348, 3499275462, 675152704, 854794152, 4004972748, 1739996642,
+ 1333476491, 4012621867, 3658792931, 3297985728, 2864481726, 3066357406, 785287846, 1671499798,
+ 433044045, 1919608025, 264833858, 3999983367, 1116778570, 1301982149, 4213901070, 4081649357,
+ 536169226, 1389008649, 188923873, 373495152, 2551132278, 1800758715, 3951840330, 2632334454,
+ 3118778225, 1034046547, 1862428410, 3037609062, 1994608505, 29051798, 2571685694, 264151332,
+ 2260643090, 2717535964, 3508441116, 3283713017, 1903365635, 923575694, 1219598101, 2288281570,
+ 3676533911, 1014136356, 555142354, 2389170030, 4185108175, 884862419, 836141292, 2957159173,
+ 1997444768, 4233903127, 2876184692, 3089125070, 1480848293, 1097600237, 299700527, 2507669891,
+ 2982628312, 2114881043, 2529576251, 2812279824, 2987750993, 4241938954, 2204775591, 1037094060,
+ 829315638, 1231047149, 52608178, 3735136637, 3455232602, 962039123, 488286513, 50685385,
+ 3516451821, 843975207, 1572355722, 675489076, 2428445672, 1555117248, 3708476086, 10375249,
+ 4172112346, 2117510871, 2227658327, 3187664554, 3050656558, 328034318, 3179601324, 1247769761,
+ 3439263953, 1431538938, 2962525068, 1213366289, 3813013550, 2651093719, 1860661503, 3933716208,
+ 264320617, 789980519, 2257856172, 102000748, 977269860, 1113845122, 3008928583, 1461738106,
+ 557786285, 2926560363, 1038106190, 3643478847, 828004507, 457818698, 1933056971, 373408056,
+ 2076808229, 3160935130, 2781854874, 2519636100, 177606000, 4237103862, 3977834316, 1621936232,
+ 2599050516, 319893558, 3343370366, 765044144, 976657331, 7026264, 294277429, 3829376742,
+ 3029627280, 2705178718, 3614653880, 230519152, 3288033233, 293525479, 3805751881, 3227511198,
+ 2520308544, 3648103003, 1111086184, 437622105, 2232033852, 3239146386, 584244184, 1450926016,
+ 2462430443, 3226534010, 298582169, 4214576928, 1762099469, 964985185, 1585788148, 1641127666,
+ 787006566, 2315956284, 3258232694, 2275058964, 2541003317, 1508235863, 2613339827, 4080647514,
+ 1152057965, 3149266279, 731345410, 914737650, 65395712, 1884566942, 1379520432, 2611027720,
+ 4163073378, 2619704967, 2746552541, 1388822415, 3005141199, 843440249, 4288674003, 3136174279,
+ 4051522914, 4144149433, 3427566947, 3419023197, 3758479825, 3893877676, 96899594, 1657725776,
+ 253618880, 434129337, 1499045748, 2996992534, 4036042074, 2110713869, 906222950, 928326225,
+ 2541827893, 1604330202, 226792470, 4022228930, 815850898, 1466012310, 3377712199, 292769859,
+ 2822055597, 3225701344, 3052947004, 385831222, 705324593, 4030158636, 3540280538, 2982120874,
+ 2136414455, 255762046, 3852783591, 3262064164, 2358991588, 3756586117, 4143612643, 3326743817,
+ 2897365738, 807711264, 3719310016, 3721264861, 3627337076, 944539331, 3640975513, 3712525681,
+ 1162911839, 2008243316, 2179489649, 2867584109, 261861553, 3570253908, 2062868357, 2220328623,
+ 3857004679, 3744109002, 4138041873, 1451860932, 2364975637, 2802161722, 2680106834, 753401584,
+ 1223182946, 1245401957, 4163377735, 3565815922, 2216942838, 4036140094, 71979081, 3924559643,
+ 400477238, 551750683, 1174153235, 859969898, 1185921017, 1711399735, 812991545, 4051735761,
+ 3549118738, 1631653329, 3631835958, 3648867800, 1206500363, 2155893137, 361030362, 3454286017,
+ 2505909489, 1083595169, 453595313, 1510564703, 1706163902, 1632924345, 1381875722, 1661526119,
+ 1082778324, 3571910052, 1140625929, 851544870, 1145546234, 2938573139, 907528924, 1304752338,
+ 1764668294, 1788942063, 1700368828, 104979467, 1413911959, 3327497828, 1956384744, 1272712474,
+ 2815637534, 3307809377, 1320574940, 1111968962, 4073107827, 434096622, 169451929, 3201183459,
+ 3331028877, 2852366972, 3369830128, 2924794558, 3106537952, 3739481231, 1612955817, 4138608722,
+ 2721281595, 2755775390, 843505117, 982234295, 1157276611, 814674632, 4246504726, 3532006708,
+ 992340967, 1647538031, 204696133, 193866982, 3899126129, 300851698, 1379496684, 1759463683,
+ 1354782756, 1374637239, 3410883240, 1073406229, 3038431791, 1053909855, 3607043270, 173719711,
+ 3733903830, 171820911, 1573050589, 932781534, 4183534770, 2158849555, 372245998, 3573073830,
+ 841339264, 2759200520, 1610547277, 2603293319, 3890906486, 1557138278, 3964109906, 677238797,
+ 537994297, 1124184993, 4287078344, 4207654540, 2943022776, 2977947524, 3255359985, 4098397558,
+ 2274666217, 2915862060, 243524940, 2467726756, 2869020032, 507521339, 3403121914, 522051455,
+ 1803903108, 3471254194, 473535371, 1948602036, 3352095732, 3116527002, 1795743673, 775867940,
+ 2551469548, 3757442064, 3162525227, 3765412747, 3040105484, 1927625810, 48214767, 2997207130,
+ 1342349989, 2536583992, 1501320191, 3592287317, 887432730, 967585477, 3334212779, 948663609,
+ 1064513472, 15386372, 2465931737, 3230242590, 3036652803, 2063155087, 1927500726, 2821790499,
+ 2187774383, 501520074, 3688568496, 3606711121, 2576459247, 3176542345, 378322447, 156541411,
+ 1400607301, 1406179107, 677848877, 2253753529, 193196070, 4207435024, 4166396241, 509467541,
+ 2906024136, 1221753746, 3375413222, 431327897, 2749265123, 2848827671, 3412997614, 2051920238,
+ 1283516885, 1300498239, 1957256104, 2634010560, 3531900395, 360276850, 1461184973, 2012063967,
+ 2873572430, 2914608609, 4289554777, 1539331673, 1859532928, 4213441063, 538215691, 3512720863,
+ 4258743698, 3040408445, 982396546, 343095663, 4138069496, 1021581857, 214185242, 1968079460,
+ 2864275059, 3347192726, 4096783459, 3259169450, 3707808869, 142485006, 399610869, 230556456,
+ 2219467721, 4191227798, 2242548189, 3136366572, 179755707, 3464881829, 452317775, 3887426070,
+ 3446430233, 1473370015, 1576807208, 3964523248, 419325089, 2373067114, 1596072055, 1928415752,
+ 3635452689, 1005598891, 3335462724, 3290848636, 3669078247, 1178176812, 2110774376, 3068593619,
+ 1253036518, 908857731, 3631223047, 4138506423, 2903592318, 3596915748, 3289036113, 3721512676,
+ 2704409359, 3386016968, 3676268074, 2185259502, 1096257611, 3360076717, 3548676554, 170167319,
+ 3360064287, 3899940843, 9640,
+];
+
+pub(crate) const POW5: [&'static [u32]; 14] = [
+ &POW5_1, &POW5_2, &POW5_3, &POW5_4, &POW5_5, &POW5_6, &POW5_7, &POW5_8, &POW5_9, &POW5_10,
+ &POW5_11, &POW5_12, &POW5_13, &POW5_14,
+];
diff --git a/third_party/rust/serde_json/src/lexical/large_powers64.rs b/third_party/rust/serde_json/src/lexical/large_powers64.rs
new file mode 100644
index 0000000000..ee36561088
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/large_powers64.rs
@@ -0,0 +1,625 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Precalculated large powers for 64-bit limbs.
+
+/// Large powers (&[u64]) for base5 operations.
+const POW5_1: [u64; 1] = [5];
+const POW5_2: [u64; 1] = [25];
+const POW5_3: [u64; 1] = [625];
+const POW5_4: [u64; 1] = [390625];
+const POW5_5: [u64; 1] = [152587890625];
+const POW5_6: [u64; 2] = [3273344365508751233, 1262];
+const POW5_7: [u64; 3] = [7942358959831785217, 16807427164405733357, 1593091];
+const POW5_8: [u64; 5] = [
+ 279109966635548161,
+ 2554917779393558781,
+ 14124656261812188652,
+ 11976055582626787546,
+ 2537941837315,
+];
+const POW5_9: [u64; 10] = [
+ 13750482914757213185,
+ 1302999927698857842,
+ 14936872543252795590,
+ 2788415840139466767,
+ 2095640732773017264,
+ 7205570348933370714,
+ 7348167152523113408,
+ 9285516396840364274,
+ 6907659600622710236,
+ 349175,
+];
+const POW5_10: [u64; 19] = [
+ 8643096425819600897,
+ 6743743997439985372,
+ 14059704609098336919,
+ 10729359125898331411,
+ 4933048501514368705,
+ 12258131603170554683,
+ 2172371001088594721,
+ 13569903330219142946,
+ 13809142207969578845,
+ 16716360519037769646,
+ 9631256923806107285,
+ 12866941232305103710,
+ 1397931361048440292,
+ 7619627737732970332,
+ 12725409486282665900,
+ 11703051443360963910,
+ 9947078370803086083,
+ 13966287901448440471,
+ 121923442132,
+];
+const POW5_11: [u64; 38] = [
+ 17679772531488845825,
+ 2216509366347768155,
+ 1568689219195129479,
+ 5511594616325588277,
+ 1067709417009240089,
+ 9070650952098657518,
+ 11515285870634858015,
+ 2539561553659505564,
+ 17604889300961091799,
+ 14511540856854204724,
+ 12099083339557485471,
+ 7115240299237943815,
+ 313979240050606788,
+ 10004784664717172195,
+ 15570268847930131473,
+ 10359715202835930803,
+ 17685054012115162812,
+ 13183273382855797757,
+ 7743260039872919062,
+ 9284593436392572926,
+ 11105921222066415013,
+ 18198799323400703846,
+ 16314988383739458320,
+ 4387527177871570570,
+ 8476708682254672590,
+ 4925096874831034057,
+ 14075687868072027455,
+ 112866656203221926,
+ 9852830467773230418,
+ 25755239915196746,
+ 2201493076310172510,
+ 8342165458688466438,
+ 13954006576066379050,
+ 15193819059903295636,
+ 12565616718911389531,
+ 3815854855847885129,
+ 15696762163583540628,
+ 805,
+];
+const POW5_12: [u64; 75] = [
+ 16359721904723189761,
+ 5323973632697650495,
+ 17187956456762001185,
+ 3930387638628283780,
+ 3374723710406992273,
+ 16884225088663222131,
+ 10967440051041439154,
+ 9686916182456720060,
+ 10554548046311730194,
+ 7390739362393647554,
+ 6316162333127736719,
+ 18122464886584070891,
+ 4044404959645932768,
+ 3801320885861987401,
+ 12080950653257274590,
+ 16414324262488991299,
+ 16395687498836410113,
+ 12173633940896186260,
+ 10843185433142632150,
+ 11048169832730399808,
+ 12674828934734683716,
+ 17370808310130582550,
+ 10500926985433408692,
+ 10252725158410704555,
+ 14170108270502067523,
+ 3698946465517688080,
+ 989984870770509463,
+ 10965601426733943069,
+ 11389898658438335655,
+ 6901098232861256586,
+ 1921335291173932590,
+ 7662788640922083388,
+ 9775023833308395430,
+ 4640401278902814207,
+ 14532050972198413359,
+ 8378549018693130223,
+ 11672322628395371653,
+ 8930704142764178555,
+ 6275193859483102017,
+ 15782593304269205087,
+ 8673060659034172558,
+ 8018354414354334043,
+ 1824896661540749038,
+ 11345563346725559868,
+ 14959216444480821949,
+ 970189517688324683,
+ 3338835207603007873,
+ 17684964260791738489,
+ 1436466329061721851,
+ 4554134986752476101,
+ 6398757850768963907,
+ 4709779218751158342,
+ 10033277748582410264,
+ 17932125878679265063,
+ 10004750887749091440,
+ 256584531835386932,
+ 14396282740722731628,
+ 3086085133731396950,
+ 17831272085689600064,
+ 10573926491412564693,
+ 14888061047859191737,
+ 4570995450261499817,
+ 10410165022312935266,
+ 5691078631447480790,
+ 8632710455805418155,
+ 790672778942823293,
+ 16505464105756800547,
+ 2092171438149740401,
+ 17505030673829275878,
+ 1291290830058928444,
+ 14856191690683232796,
+ 8916773426496500052,
+ 10152003807578858265,
+ 13104441193763861714,
+ 649395,
+];
+const POW5_13: [u64; 149] = [
+ 15308384451594534913,
+ 17913664074042735335,
+ 6115977719198531863,
+ 5794980608663993169,
+ 16544350702855106930,
+ 9253787637781258566,
+ 4977988951675168190,
+ 9087837664087448770,
+ 2098480401110016986,
+ 15474332540882100712,
+ 14042133997396540944,
+ 1090855284423485362,
+ 12639956485351058381,
+ 1454115676006639319,
+ 3180465001342538023,
+ 14649076551958697729,
+ 9801292446545910916,
+ 13552201410826594004,
+ 6101141927469189381,
+ 1881431857880609316,
+ 4907847477899433595,
+ 8714572486973123228,
+ 3514969632331374520,
+ 11667642286891470094,
+ 2391499697425323350,
+ 17486585679659076043,
+ 18267223761882105642,
+ 2886610765822313148,
+ 9302834862968900288,
+ 15246507846733637044,
+ 15924227519624562840,
+ 9743741243284697760,
+ 3159780987244964246,
+ 7304816812369628428,
+ 17584602612559717809,
+ 4146812420657846766,
+ 14525415362681041515,
+ 8477630142371600195,
+ 4380695748062263745,
+ 12119915994367943173,
+ 16970630866565485122,
+ 4332724980155264503,
+ 8079943140620527639,
+ 1687908087554405626,
+ 17051081099834002166,
+ 12638146269730763230,
+ 11883749876933445771,
+ 4662462156371383785,
+ 4796962238316531176,
+ 3325504751659868927,
+ 6469595803187862550,
+ 5852556621152583005,
+ 9229334792448387881,
+ 17979733373938620709,
+ 13951623534175792756,
+ 17075879371091039277,
+ 14212246479457938037,
+ 4008999959804158260,
+ 2414266395366403722,
+ 3252733766253918247,
+ 6382678985007829216,
+ 2245927470982310841,
+ 13790724502051307301,
+ 13116936866733148041,
+ 9718402891306794538,
+ 13516274400356104875,
+ 17859223875778049403,
+ 4396895129099725471,
+ 3563053650368467915,
+ 12176845952536972668,
+ 3492050964335269015,
+ 2740656767075170753,
+ 4409704077614761919,
+ 10237775279597492710,
+ 3314206875098230827,
+ 16437361028114095448,
+ 12361736225407656572,
+ 16792510651790145480,
+ 11449053143229929935,
+ 18336641737580333136,
+ 6558939822118891088,
+ 4606255756908155300,
+ 2360792578991605004,
+ 160428430149144538,
+ 11644861220729221511,
+ 10785178451159739786,
+ 14923560618031934681,
+ 1902620814992781610,
+ 14064076995338910412,
+ 11547019064112212657,
+ 16847481479966225734,
+ 8331994491163145469,
+ 11739712981738851885,
+ 8008309968651120619,
+ 10266969595459035264,
+ 15175153381217702033,
+ 12208659352573720245,
+ 7714061140750342961,
+ 2892831567213510541,
+ 15453714249045017319,
+ 71020323573871677,
+ 15431137995750602633,
+ 5659146884637671933,
+ 5998809010488554503,
+ 16552192379299157850,
+ 1192197967194298797,
+ 16157555793424861524,
+ 10929371590994640255,
+ 3194469143425738352,
+ 6651586784672005225,
+ 11062427140788057791,
+ 6834443579468668318,
+ 16421563197797455922,
+ 6251046422506172884,
+ 13952303462156793860,
+ 16632486601871393224,
+ 11313454360291325172,
+ 5587835232504462834,
+ 3105197524618514637,
+ 18268568531031972989,
+ 2397205535804309313,
+ 59413027864729597,
+ 11869878125348715710,
+ 12592801707270523266,
+ 8070632061321113656,
+ 18403647807860650811,
+ 267109013517069093,
+ 6537214311028855260,
+ 5220826919973709902,
+ 3448740582779163661,
+ 16822239213112884941,
+ 5975299384311048185,
+ 10294433804430712138,
+ 4739856055412448774,
+ 12057273038326387897,
+ 13119002941950056609,
+ 3354445304051737058,
+ 13592813067499314594,
+ 3890182464434078629,
+ 17820384357466425060,
+ 9785228118969879380,
+ 1778431746734556271,
+ 10075313876350055029,
+ 13994048489400919028,
+ 17948287074199726448,
+ 2815088342305858722,
+ 2676626035777198370,
+ 1174257960026283968,
+ 421714788677,
+];
+const POW5_14: [u64; 298] = [
+ 11471884475673051137,
+ 8902860357476377573,
+ 13350296775839230505,
+ 10609191786344608888,
+ 7261211985859587338,
+ 11439672689354862964,
+ 16789708072300570627,
+ 4607056528866348430,
+ 3202978990421512997,
+ 2024899620433984146,
+ 17666950207239811774,
+ 4233228489390288200,
+ 9137580478688460738,
+ 4060411066587388546,
+ 11119949806060600124,
+ 867715462473090103,
+ 14382394941384869610,
+ 4856042377419278489,
+ 8265605599571137921,
+ 538981667666252469,
+ 4270263388700786523,
+ 3281140600308898503,
+ 4121392524544394174,
+ 2077884106245940229,
+ 9773041957329767574,
+ 7550623316597646685,
+ 8611033926449791714,
+ 18137922955420802793,
+ 2796546741236224013,
+ 15477096484628446761,
+ 9517540128113714010,
+ 9471917970500821378,
+ 15938570248662483124,
+ 5228016831978462619,
+ 15720991252586974501,
+ 7662829825220776698,
+ 17328310068068434348,
+ 3371736428170309730,
+ 3803724952191098855,
+ 13115926536504376719,
+ 16752571196153442257,
+ 16540185467776259880,
+ 3432518182450051120,
+ 5880364967211798870,
+ 12355748840305392783,
+ 14196090758536469575,
+ 7370123524686686319,
+ 6819740424617592686,
+ 13037938013537368753,
+ 15029273671291927100,
+ 3671312928327205696,
+ 7473228676544792780,
+ 17234079691312938123,
+ 14164740848093544419,
+ 13169904779481875902,
+ 7179036968465894054,
+ 8244653688947194445,
+ 17179797746073799490,
+ 5591970751047577674,
+ 17530550506268329742,
+ 5965746721852312330,
+ 1604149463243472865,
+ 7734199791463116918,
+ 11305790396015856714,
+ 4441196105025505137,
+ 13046431581185664762,
+ 124776524294606713,
+ 1134521334706523966,
+ 11671728093344476434,
+ 14103440020972933148,
+ 3966727403013869059,
+ 9828094508409132821,
+ 4355682486381147287,
+ 10261407143988481234,
+ 3800455155249557199,
+ 12700901937937547500,
+ 18184475466894579360,
+ 13267691151779895412,
+ 4714157123477697445,
+ 10770360171308585263,
+ 9083344917597998040,
+ 12078649873810212155,
+ 18218989082046199377,
+ 4454285072780637351,
+ 5287307245618354742,
+ 16042289702059031730,
+ 4131926574212754010,
+ 217692071448455473,
+ 3624845916216282093,
+ 2901203491797614218,
+ 6679177724033967080,
+ 44561358851332790,
+ 9094639944041587162,
+ 13690915012276084311,
+ 1408896670826320686,
+ 5359130319612337580,
+ 6148412925099835601,
+ 5211368532286409612,
+ 11386360825549027374,
+ 16895182466965795071,
+ 3392940493846427241,
+ 438089879085393580,
+ 4783928372776399972,
+ 6278117363595909959,
+ 12569481049412674733,
+ 15648622492570893902,
+ 1966316336235305115,
+ 1603775390515993547,
+ 13576113010204316709,
+ 10821754650102840474,
+ 18198222517222903152,
+ 6966163076615302988,
+ 1373932372410129684,
+ 3285839581819684990,
+ 30177575069719475,
+ 16447047871247307061,
+ 11618654126674833808,
+ 990072222556306872,
+ 1260682336135768017,
+ 13862055046689532489,
+ 15668483092844698432,
+ 1879572630092764264,
+ 13912027797058626108,
+ 6231679788219816920,
+ 13857858054844167403,
+ 18101470072534728857,
+ 4144579812461609229,
+ 7048589655616599284,
+ 9946956499532694630,
+ 9771303850109874038,
+ 6477823708780339765,
+ 17526247621747041971,
+ 13525995675852669549,
+ 3928768291901239810,
+ 8094153383078124544,
+ 11214278667728965552,
+ 11251547162596832610,
+ 5964946855123292381,
+ 3622548288590237903,
+ 13469765967150053587,
+ 17798986288523466082,
+ 14684592818807932259,
+ 16724077276802963921,
+ 7119877993753121290,
+ 1864571304902781632,
+ 12871984921385213812,
+ 9065447042604670298,
+ 3987130777300360550,
+ 6890545752116901685,
+ 17275341711601865750,
+ 6296474927799264658,
+ 1257436973037243463,
+ 13854281781965301421,
+ 1657132483318662716,
+ 17309399540017292849,
+ 12808111630089217242,
+ 1098489625264462071,
+ 14010458905686364135,
+ 16134414519481621220,
+ 14288255900328821475,
+ 3469093466388187882,
+ 15982710881468295872,
+ 4056765540058056052,
+ 15945176389096104089,
+ 8625339365793505375,
+ 12316179968863788913,
+ 15334123773538054321,
+ 9536238824220581765,
+ 16080825720106203271,
+ 6235695225418121745,
+ 12035192956458019349,
+ 3235835166714703698,
+ 5348960676912581218,
+ 15315062772709464647,
+ 17335089708021308662,
+ 16855855317958414409,
+ 2369751139431140406,
+ 3693542588628609043,
+ 7350405893393987577,
+ 17402072586341663801,
+ 7007897690013647122,
+ 15671767872059304758,
+ 9259490518292347915,
+ 14836045474406130394,
+ 4654005815464502513,
+ 6487825998330548401,
+ 7013356660323385022,
+ 7136200343936679946,
+ 15341236858676437716,
+ 3657357368867197449,
+ 12621075530054608378,
+ 5603868621997066972,
+ 7683447656788439942,
+ 450883379216880060,
+ 14291494350184945047,
+ 5466258454997635048,
+ 14206933098432772126,
+ 4775870327277641692,
+ 1864430798867181939,
+ 13748978265070608793,
+ 12250822864261576589,
+ 12561896977498605296,
+ 16060949594257359328,
+ 17775189113543311529,
+ 11835965177892927035,
+ 4218664174878121437,
+ 3499000902478111683,
+ 15169853304359126294,
+ 7076121963053575143,
+ 832652347668916805,
+ 1292148207755194737,
+ 7556838978364207852,
+ 5904021986723518500,
+ 4610244652288570024,
+ 4526508363195533871,
+ 746120481022614726,
+ 737965197247830486,
+ 4006266184415762653,
+ 9272188239892688050,
+ 15346235246415709678,
+ 11850675997347533184,
+ 11181059668610842701,
+ 6687857983250662774,
+ 2908718488661492818,
+ 4828337780126983225,
+ 18071738646453002184,
+ 12790187227727197880,
+ 17602483480871623153,
+ 12523532189621855977,
+ 10598805712727696716,
+ 2179787555896149376,
+ 2242193929457337594,
+ 14908923241136742532,
+ 8369182018012550027,
+ 13385381554043022324,
+ 3332327430110633913,
+ 16138090784046208492,
+ 16172324607469047339,
+ 8279089815915615244,
+ 12872906602736235247,
+ 10894545290539475621,
+ 15428756545851905023,
+ 4155747980686992922,
+ 4074479178894544043,
+ 66083965608603584,
+ 13873786284662268377,
+ 8861183628277687555,
+ 12119497911296021430,
+ 2154012318305274287,
+ 15490706314503067312,
+ 13643145488710608367,
+ 672340241093017103,
+ 6039493278284091973,
+ 9679797700977436461,
+ 18070795828318171174,
+ 2188146431134935377,
+ 5247392385741514952,
+ 1852539214842869734,
+ 12235621681634112739,
+ 8812930319623534062,
+ 5585597406294108629,
+ 11312989214475901864,
+ 1547377291787797995,
+ 8641748937186208205,
+ 12518148659168623694,
+ 6611379197521520985,
+ 18096591571068008576,
+ 15087021227100112139,
+ 13058454842015958418,
+ 1473584652966833794,
+ 4387660670140018168,
+ 8452836916843525402,
+ 14376083294443363955,
+ 13998026203969090659,
+ 611968444648172645,
+ 990232438801273845,
+ 18001186324715561929,
+ 13470591857250177501,
+ 14881554140239420091,
+ 16696367836720124495,
+ 6328076032778459673,
+ 17027497695968504616,
+ 10192245646262428833,
+ 8282482589527318647,
+ 4319014353374321425,
+ 14134087271041670980,
+ 5060230880114618599,
+ 13179509240430058600,
+ 3903514232614801894,
+ 17774749744702165255,
+ 15448635507030969726,
+ 15983775238358480209,
+ 14542832143965487887,
+ 9385618098039514666,
+ 14431419612662304843,
+ 730863073501675978,
+ 16750118380379734815,
+ 9640,
+];
+
+pub(crate) const POW5: [&[u64]; 14] = [
+ &POW5_1, &POW5_2, &POW5_3, &POW5_4, &POW5_5, &POW5_6, &POW5_7, &POW5_8, &POW5_9, &POW5_10,
+ &POW5_11, &POW5_12, &POW5_13, &POW5_14,
+];
diff --git a/third_party/rust/serde_json/src/lexical/math.rs b/third_party/rust/serde_json/src/lexical/math.rs
new file mode 100644
index 0000000000..37cc1d24ad
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/math.rs
@@ -0,0 +1,886 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Building-blocks for arbitrary-precision math.
+//!
+//! These algorithms assume little-endian order for the large integer
+//! buffers, so for a `vec![0, 1, 2, 3]`, `3` is the most significant limb,
+//! and `0` is the least significant limb.
+
+use super::large_powers;
+use super::num::*;
+use super::small_powers::*;
+use alloc::vec::Vec;
+use core::{cmp, iter, mem};
+
+// ALIASES
+// -------
+
+// Type for a single limb of the big integer.
+//
+// A limb is analogous to a digit in base10, except, it stores 32-bit
+// or 64-bit numbers instead.
+//
+// This should be all-known 64-bit platforms supported by Rust.
+// https://forge.rust-lang.org/platform-support.html
+//
+// Platforms where native 128-bit multiplication is explicitly supported:
+// - x86_64 (Supported via `MUL`).
+// - mips64 (Supported via `DMULTU`, which `HI` and `LO` can be read-from).
+//
+// Platforms where native 64-bit multiplication is supported and
+// you can extract hi-lo for 64-bit multiplications.
+// aarch64 (Requires `UMULH` and `MUL` to capture high and low bits).
+// powerpc64 (Requires `MULHDU` and `MULLD` to capture high and low bits).
+//
+// Platforms where native 128-bit multiplication is not supported,
+// requiring software emulation.
+// sparc64 (`UMUL` only supported double-word arguments).
+
+// 32-BIT LIMB
+#[cfg(limb_width_32)]
+pub type Limb = u32;
+
+#[cfg(limb_width_32)]
+pub const POW5_LIMB: &[Limb] = &POW5_32;
+
+#[cfg(limb_width_32)]
+pub const POW10_LIMB: &[Limb] = &POW10_32;
+
+#[cfg(limb_width_32)]
+type Wide = u64;
+
+// 64-BIT LIMB
+#[cfg(limb_width_64)]
+pub type Limb = u64;
+
+#[cfg(limb_width_64)]
+pub const POW5_LIMB: &[Limb] = &POW5_64;
+
+#[cfg(limb_width_64)]
+pub const POW10_LIMB: &[Limb] = &POW10_64;
+
+#[cfg(limb_width_64)]
+type Wide = u128;
+
+/// Cast to limb type.
+#[inline]
+pub(crate) fn as_limb<T: Integer>(t: T) -> Limb {
+ Limb::as_cast(t)
+}
+
+/// Cast to wide type.
+#[inline]
+fn as_wide<T: Integer>(t: T) -> Wide {
+ Wide::as_cast(t)
+}
+
+// SPLIT
+// -----
+
+/// Split u64 into limbs, in little-endian order.
+#[inline]
+#[cfg(limb_width_32)]
+fn split_u64(x: u64) -> [Limb; 2] {
+ [as_limb(x), as_limb(x >> 32)]
+}
+
+/// Split u64 into limbs, in little-endian order.
+#[inline]
+#[cfg(limb_width_64)]
+fn split_u64(x: u64) -> [Limb; 1] {
+ [as_limb(x)]
+}
+
+// HI64
+// ----
+
+// NONZERO
+
+/// Check if any of the remaining bits are non-zero.
+#[inline]
+pub fn nonzero<T: Integer>(x: &[T], rindex: usize) -> bool {
+ let len = x.len();
+ let slc = &x[..len - rindex];
+ slc.iter().rev().any(|&x| x != T::ZERO)
+}
+
+/// Shift 64-bit integer to high 64-bits.
+#[inline]
+fn u64_to_hi64_1(r0: u64) -> (u64, bool) {
+ debug_assert!(r0 != 0);
+ let ls = r0.leading_zeros();
+ (r0 << ls, false)
+}
+
+/// Shift 2 64-bit integers to high 64-bits.
+#[inline]
+fn u64_to_hi64_2(r0: u64, r1: u64) -> (u64, bool) {
+ debug_assert!(r0 != 0);
+ let ls = r0.leading_zeros();
+ let rs = 64 - ls;
+ let v = match ls {
+ 0 => r0,
+ _ => (r0 << ls) | (r1 >> rs),
+ };
+ let n = r1 << ls != 0;
+ (v, n)
+}
+
+/// Trait to export the high 64-bits from a little-endian slice.
+trait Hi64<T>: AsRef<[T]> {
+ /// Get the hi64 bits from a 1-limb slice.
+ fn hi64_1(&self) -> (u64, bool);
+
+ /// Get the hi64 bits from a 2-limb slice.
+ fn hi64_2(&self) -> (u64, bool);
+
+ /// Get the hi64 bits from a 3-limb slice.
+ fn hi64_3(&self) -> (u64, bool);
+
+ /// High-level exporter to extract the high 64 bits from a little-endian slice.
+ #[inline]
+ fn hi64(&self) -> (u64, bool) {
+ match self.as_ref().len() {
+ 0 => (0, false),
+ 1 => self.hi64_1(),
+ 2 => self.hi64_2(),
+ _ => self.hi64_3(),
+ }
+ }
+}
+
+impl Hi64<u32> for [u32] {
+ #[inline]
+ fn hi64_1(&self) -> (u64, bool) {
+ debug_assert!(self.len() == 1);
+ let r0 = self[0] as u64;
+ u64_to_hi64_1(r0)
+ }
+
+ #[inline]
+ fn hi64_2(&self) -> (u64, bool) {
+ debug_assert!(self.len() == 2);
+ let r0 = (self[1] as u64) << 32;
+ let r1 = self[0] as u64;
+ u64_to_hi64_1(r0 | r1)
+ }
+
+ #[inline]
+ fn hi64_3(&self) -> (u64, bool) {
+ debug_assert!(self.len() >= 3);
+ let r0 = self[self.len() - 1] as u64;
+ let r1 = (self[self.len() - 2] as u64) << 32;
+ let r2 = self[self.len() - 3] as u64;
+ let (v, n) = u64_to_hi64_2(r0, r1 | r2);
+ (v, n || nonzero(self, 3))
+ }
+}
+
+impl Hi64<u64> for [u64] {
+ #[inline]
+ fn hi64_1(&self) -> (u64, bool) {
+ debug_assert!(self.len() == 1);
+ let r0 = self[0];
+ u64_to_hi64_1(r0)
+ }
+
+ #[inline]
+ fn hi64_2(&self) -> (u64, bool) {
+ debug_assert!(self.len() >= 2);
+ let r0 = self[self.len() - 1];
+ let r1 = self[self.len() - 2];
+ let (v, n) = u64_to_hi64_2(r0, r1);
+ (v, n || nonzero(self, 2))
+ }
+
+ #[inline]
+ fn hi64_3(&self) -> (u64, bool) {
+ self.hi64_2()
+ }
+}
+
+// SCALAR
+// ------
+
+// Scalar-to-scalar operations, for building-blocks for arbitrary-precision
+// operations.
+
+mod scalar {
+ use super::*;
+
+ // ADDITION
+
+ /// Add two small integers and return the resulting value and if overflow happens.
+ #[inline]
+ pub fn add(x: Limb, y: Limb) -> (Limb, bool) {
+ x.overflowing_add(y)
+ }
+
+ /// AddAssign two small integers and return if overflow happens.
+ #[inline]
+ pub fn iadd(x: &mut Limb, y: Limb) -> bool {
+ let t = add(*x, y);
+ *x = t.0;
+ t.1
+ }
+
+ // SUBTRACTION
+
+ /// Subtract two small integers and return the resulting value and if overflow happens.
+ #[inline]
+ pub fn sub(x: Limb, y: Limb) -> (Limb, bool) {
+ x.overflowing_sub(y)
+ }
+
+ /// SubAssign two small integers and return if overflow happens.
+ #[inline]
+ pub fn isub(x: &mut Limb, y: Limb) -> bool {
+ let t = sub(*x, y);
+ *x = t.0;
+ t.1
+ }
+
+ // MULTIPLICATION
+
+ /// Multiply two small integers (with carry) (and return the overflow contribution).
+ ///
+ /// Returns the (low, high) components.
+ #[inline]
+ pub fn mul(x: Limb, y: Limb, carry: Limb) -> (Limb, Limb) {
+ // Cannot overflow, as long as wide is 2x as wide. This is because
+ // the following is always true:
+ // `Wide::max_value() - (Narrow::max_value() * Narrow::max_value()) >= Narrow::max_value()`
+ let z: Wide = as_wide(x) * as_wide(y) + as_wide(carry);
+ let bits = mem::size_of::<Limb>() * 8;
+ (as_limb(z), as_limb(z >> bits))
+ }
+
+ /// Multiply two small integers (with carry) (and return if overflow happens).
+ #[inline]
+ pub fn imul(x: &mut Limb, y: Limb, carry: Limb) -> Limb {
+ let t = mul(*x, y, carry);
+ *x = t.0;
+ t.1
+ }
+} // scalar
+
+// SMALL
+// -----
+
+// Large-to-small operations, to modify a big integer from a native scalar.
+
+mod small {
+ use super::*;
+
+ // MULTIPLICATIION
+
+ /// ADDITION
+
+ /// Implied AddAssign implementation for adding a small integer to bigint.
+ ///
+ /// Allows us to choose a start-index in x to store, to allow incrementing
+ /// from a non-zero start.
+ #[inline]
+ pub fn iadd_impl(x: &mut Vec<Limb>, y: Limb, xstart: usize) {
+ if x.len() <= xstart {
+ x.push(y);
+ } else {
+ // Initial add
+ let mut carry = scalar::iadd(&mut x[xstart], y);
+
+ // Increment until overflow stops occurring.
+ let mut size = xstart + 1;
+ while carry && size < x.len() {
+ carry = scalar::iadd(&mut x[size], 1);
+ size += 1;
+ }
+
+ // If we overflowed the buffer entirely, need to add 1 to the end
+ // of the buffer.
+ if carry {
+ x.push(1);
+ }
+ }
+ }
+
+ /// AddAssign small integer to bigint.
+ #[inline]
+ pub fn iadd(x: &mut Vec<Limb>, y: Limb) {
+ iadd_impl(x, y, 0);
+ }
+
+ // SUBTRACTION
+
+ /// SubAssign small integer to bigint.
+ /// Does not do overflowing subtraction.
+ #[inline]
+ pub fn isub_impl(x: &mut Vec<Limb>, y: Limb, xstart: usize) {
+ debug_assert!(x.len() > xstart && (x[xstart] >= y || x.len() > xstart + 1));
+
+ // Initial subtraction
+ let mut carry = scalar::isub(&mut x[xstart], y);
+
+ // Increment until overflow stops occurring.
+ let mut size = xstart + 1;
+ while carry && size < x.len() {
+ carry = scalar::isub(&mut x[size], 1);
+ size += 1;
+ }
+ normalize(x);
+ }
+
+ // MULTIPLICATION
+
+ /// MulAssign small integer to bigint.
+ #[inline]
+ pub fn imul(x: &mut Vec<Limb>, y: Limb) {
+ // Multiply iteratively over all elements, adding the carry each time.
+ let mut carry: Limb = 0;
+ for xi in x.iter_mut() {
+ carry = scalar::imul(xi, y, carry);
+ }
+
+ // Overflow of value, add to end.
+ if carry != 0 {
+ x.push(carry);
+ }
+ }
+
+ /// Mul small integer to bigint.
+ #[inline]
+ pub fn mul(x: &[Limb], y: Limb) -> Vec<Limb> {
+ let mut z = Vec::<Limb>::default();
+ z.extend_from_slice(x);
+ imul(&mut z, y);
+ z
+ }
+
+ /// MulAssign by a power.
+ ///
+ /// Theoretically...
+ ///
+ /// Use an exponentiation by squaring method, since it reduces the time
+ /// complexity of the multiplication to ~`O(log(n))` for the squaring,
+ /// and `O(n*m)` for the result. Since `m` is typically a lower-order
+ /// factor, this significantly reduces the number of multiplications
+ /// we need to do. Iteratively multiplying by small powers follows
+ /// the nth triangular number series, which scales as `O(p^2)`, but
+ /// where `p` is `n+m`. In short, it scales very poorly.
+ ///
+ /// Practically....
+ ///
+ /// Exponentiation by Squaring:
+ /// running 2 tests
+ /// test bigcomp_f32_lexical ... bench: 1,018 ns/iter (+/- 78)
+ /// test bigcomp_f64_lexical ... bench: 3,639 ns/iter (+/- 1,007)
+ ///
+ /// Exponentiation by Iterative Small Powers:
+ /// running 2 tests
+ /// test bigcomp_f32_lexical ... bench: 518 ns/iter (+/- 31)
+ /// test bigcomp_f64_lexical ... bench: 583 ns/iter (+/- 47)
+ ///
+ /// Exponentiation by Iterative Large Powers (of 2):
+ /// running 2 tests
+ /// test bigcomp_f32_lexical ... bench: 671 ns/iter (+/- 31)
+ /// test bigcomp_f64_lexical ... bench: 1,394 ns/iter (+/- 47)
+ ///
+ /// Even using worst-case scenarios, exponentiation by squaring is
+ /// significantly slower for our workloads. Just multiply by small powers,
+ /// in simple cases, and use precalculated large powers in other cases.
+ pub fn imul_pow5(x: &mut Vec<Limb>, n: u32) {
+ use super::large::KARATSUBA_CUTOFF;
+
+ let small_powers = POW5_LIMB;
+ let large_powers = large_powers::POW5;
+
+ if n == 0 {
+ // No exponent, just return.
+ // The 0-index of the large powers is `2^0`, which is 1, so we want
+ // to make sure we don't take that path with a literal 0.
+ return;
+ }
+
+ // We want to use the asymptotically faster algorithm if we're going
+ // to be using Karabatsu multiplication sometime during the result,
+ // otherwise, just use exponentiation by squaring.
+ let bit_length = 32 - n.leading_zeros() as usize;
+ debug_assert!(bit_length != 0 && bit_length <= large_powers.len());
+ if x.len() + large_powers[bit_length - 1].len() < 2 * KARATSUBA_CUTOFF {
+ // We can use iterative small powers to make this faster for the
+ // easy cases.
+
+ // Multiply by the largest small power until n < step.
+ let step = small_powers.len() - 1;
+ let power = small_powers[step];
+ let mut n = n as usize;
+ while n >= step {
+ imul(x, power);
+ n -= step;
+ }
+
+ // Multiply by the remainder.
+ imul(x, small_powers[n]);
+ } else {
+ // In theory, this code should be asymptotically a lot faster,
+ // in practice, our small::imul seems to be the limiting step,
+ // and large imul is slow as well.
+
+ // Multiply by higher order powers.
+ let mut idx: usize = 0;
+ let mut bit: usize = 1;
+ let mut n = n as usize;
+ while n != 0 {
+ if n & bit != 0 {
+ debug_assert!(idx < large_powers.len());
+ large::imul(x, large_powers[idx]);
+ n ^= bit;
+ }
+ idx += 1;
+ bit <<= 1;
+ }
+ }
+ }
+
+ // BIT LENGTH
+
+ /// Get number of leading zero bits in the storage.
+ #[inline]
+ pub fn leading_zeros(x: &[Limb]) -> usize {
+ x.last().map_or(0, |x| x.leading_zeros() as usize)
+ }
+
+ /// Calculate the bit-length of the big-integer.
+ #[inline]
+ pub fn bit_length(x: &[Limb]) -> usize {
+ let bits = mem::size_of::<Limb>() * 8;
+ // Avoid overflowing, calculate via total number of bits
+ // minus leading zero bits.
+ let nlz = leading_zeros(x);
+ bits.checked_mul(x.len())
+ .map_or_else(usize::max_value, |v| v - nlz)
+ }
+
+ // SHL
+
+ /// Shift-left bits inside a buffer.
+ ///
+ /// Assumes `n < Limb::BITS`, IE, internally shifting bits.
+ #[inline]
+ pub fn ishl_bits(x: &mut Vec<Limb>, n: usize) {
+ // Need to shift by the number of `bits % Limb::BITS)`.
+ let bits = mem::size_of::<Limb>() * 8;
+ debug_assert!(n < bits);
+ if n == 0 {
+ return;
+ }
+
+ // Internally, for each item, we shift left by n, and add the previous
+ // right shifted limb-bits.
+ // For example, we transform (for u8) shifted left 2, to:
+ // b10100100 b01000010
+ // b10 b10010001 b00001000
+ let rshift = bits - n;
+ let lshift = n;
+ let mut prev: Limb = 0;
+ for xi in x.iter_mut() {
+ let tmp = *xi;
+ *xi <<= lshift;
+ *xi |= prev >> rshift;
+ prev = tmp;
+ }
+
+ // Always push the carry, even if it creates a non-normal result.
+ let carry = prev >> rshift;
+ if carry != 0 {
+ x.push(carry);
+ }
+ }
+
+ /// Shift-left `n` digits inside a buffer.
+ ///
+ /// Assumes `n` is not 0.
+ #[inline]
+ pub fn ishl_limbs(x: &mut Vec<Limb>, n: usize) {
+ debug_assert!(n != 0);
+ if !x.is_empty() {
+ x.reserve(n);
+ x.splice(..0, iter::repeat(0).take(n));
+ }
+ }
+
+ /// Shift-left buffer by n bits.
+ #[inline]
+ pub fn ishl(x: &mut Vec<Limb>, n: usize) {
+ let bits = mem::size_of::<Limb>() * 8;
+ // Need to pad with zeros for the number of `bits / Limb::BITS`,
+ // and shift-left with carry for `bits % Limb::BITS`.
+ let rem = n % bits;
+ let div = n / bits;
+ ishl_bits(x, rem);
+ if div != 0 {
+ ishl_limbs(x, div);
+ }
+ }
+
+ // NORMALIZE
+
+ /// Normalize the container by popping any leading zeros.
+ #[inline]
+ pub fn normalize(x: &mut Vec<Limb>) {
+ // Remove leading zero if we cause underflow. Since we're dividing
+ // by a small power, we have at max 1 int removed.
+ while x.last() == Some(&0) {
+ x.pop();
+ }
+ }
+} // small
+
+// LARGE
+// -----
+
+// Large-to-large operations, to modify a big integer from a native scalar.
+
+mod large {
+ use super::*;
+
+ // RELATIVE OPERATORS
+
+ /// Compare `x` to `y`, in little-endian order.
+ #[inline]
+ pub fn compare(x: &[Limb], y: &[Limb]) -> cmp::Ordering {
+ if x.len() > y.len() {
+ cmp::Ordering::Greater
+ } else if x.len() < y.len() {
+ cmp::Ordering::Less
+ } else {
+ let iter = x.iter().rev().zip(y.iter().rev());
+ for (&xi, &yi) in iter {
+ if xi > yi {
+ return cmp::Ordering::Greater;
+ } else if xi < yi {
+ return cmp::Ordering::Less;
+ }
+ }
+ // Equal case.
+ cmp::Ordering::Equal
+ }
+ }
+
+ /// Check if x is less than y.
+ #[inline]
+ pub fn less(x: &[Limb], y: &[Limb]) -> bool {
+ compare(x, y) == cmp::Ordering::Less
+ }
+
+ /// Check if x is greater than or equal to y.
+ #[inline]
+ pub fn greater_equal(x: &[Limb], y: &[Limb]) -> bool {
+ !less(x, y)
+ }
+
+ // ADDITION
+
+ /// Implied AddAssign implementation for bigints.
+ ///
+ /// Allows us to choose a start-index in x to store, so we can avoid
+ /// padding the buffer with zeros when not needed, optimized for vectors.
+ pub fn iadd_impl(x: &mut Vec<Limb>, y: &[Limb], xstart: usize) {
+ // The effective x buffer is from `xstart..x.len()`, so we need to treat
+ // that as the current range. If the effective y buffer is longer, need
+ // to resize to that, + the start index.
+ if y.len() > x.len() - xstart {
+ x.resize(y.len() + xstart, 0);
+ }
+
+ // Iteratively add elements from y to x.
+ let mut carry = false;
+ 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.
+ let mut tmp = scalar::iadd(xi, *yi);
+ if carry {
+ tmp |= scalar::iadd(xi, 1);
+ }
+ carry = tmp;
+ }
+
+ // Overflow from the previous bit.
+ if carry {
+ small::iadd_impl(x, 1, y.len() + xstart);
+ }
+ }
+
+ /// AddAssign bigint to bigint.
+ #[inline]
+ pub fn iadd(x: &mut Vec<Limb>, y: &[Limb]) {
+ iadd_impl(x, y, 0);
+ }
+
+ /// Add bigint to bigint.
+ #[inline]
+ pub fn add(x: &[Limb], y: &[Limb]) -> Vec<Limb> {
+ let mut z = Vec::<Limb>::default();
+ z.extend_from_slice(x);
+ iadd(&mut z, y);
+ z
+ }
+
+ // SUBTRACTION
+
+ /// SubAssign bigint to bigint.
+ pub fn isub(x: &mut Vec<Limb>, y: &[Limb]) {
+ // Basic underflow checks.
+ debug_assert!(greater_equal(x, y));
+
+ // Iteratively add elements from y to x.
+ let mut carry = false;
+ for (xi, yi) in x.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.
+ let mut tmp = scalar::isub(xi, *yi);
+ if carry {
+ tmp |= scalar::isub(xi, 1);
+ }
+ carry = tmp;
+ }
+
+ if carry {
+ small::isub_impl(x, 1, y.len());
+ } else {
+ small::normalize(x);
+ }
+ }
+
+ // MULTIPLICATION
+
+ /// Number of digits to bottom-out to asymptotically slow algorithms.
+ ///
+ /// Karatsuba tends to out-perform long-multiplication at ~320-640 bits,
+ /// so we go halfway, while Newton division tends to out-perform
+ /// Algorithm D at ~1024 bits. We can toggle this for optimal performance.
+ pub const KARATSUBA_CUTOFF: usize = 32;
+
+ /// Grade-school multiplication algorithm.
+ ///
+ /// Slow, naive algorithm, using limb-bit bases and just shifting left for
+ /// each iteration. This could be optimized with numerous other algorithms,
+ /// but it's extremely simple, and works in O(n*m) time, which is fine
+ /// by me. Each iteration, of which there are `m` iterations, requires
+ /// `n` multiplications, and `n` additions, or grade-school multiplication.
+ fn long_mul(x: &[Limb], y: &[Limb]) -> Vec<Limb> {
+ // Using the immutable value, multiply by all the scalars in y, using
+ // the algorithm defined above. Use a single buffer to avoid
+ // frequent reallocations. Handle the first case to avoid a redundant
+ // addition, since we know y.len() >= 1.
+ let mut z: Vec<Limb> = small::mul(x, y[0]);
+ z.resize(x.len() + y.len(), 0);
+
+ // Handle the iterative cases.
+ for (i, &yi) in y[1..].iter().enumerate() {
+ let zi: Vec<Limb> = small::mul(x, yi);
+ iadd_impl(&mut z, &zi, i + 1);
+ }
+
+ small::normalize(&mut z);
+
+ z
+ }
+
+ /// Split two buffers into halfway, into (lo, hi).
+ #[inline]
+ pub fn karatsuba_split(z: &[Limb], m: usize) -> (&[Limb], &[Limb]) {
+ (&z[..m], &z[m..])
+ }
+
+ /// Karatsuba multiplication algorithm with roughly equal input sizes.
+ ///
+ /// Assumes `y.len() >= x.len()`.
+ fn karatsuba_mul(x: &[Limb], y: &[Limb]) -> Vec<Limb> {
+ if y.len() <= KARATSUBA_CUTOFF {
+ // Bottom-out to long division for small cases.
+ long_mul(x, y)
+ } else if x.len() < y.len() / 2 {
+ karatsuba_uneven_mul(x, y)
+ } else {
+ // Do our 3 multiplications.
+ let m = y.len() / 2;
+ let (xl, xh) = karatsuba_split(x, m);
+ let (yl, yh) = karatsuba_split(y, m);
+ let sumx = add(xl, xh);
+ let sumy = add(yl, yh);
+ let z0 = karatsuba_mul(xl, yl);
+ let mut z1 = karatsuba_mul(&sumx, &sumy);
+ let z2 = karatsuba_mul(xh, yh);
+ // Properly scale z1, which is `z1 - z2 - zo`.
+ isub(&mut z1, &z2);
+ isub(&mut z1, &z0);
+
+ // Create our result, which is equal to, in little-endian order:
+ // [z0, z1 - z2 - z0, z2]
+ // z1 must be shifted m digits (2^(32m)) over.
+ // z2 must be shifted 2*m digits (2^(64m)) over.
+ let len = z0.len().max(m + z1.len()).max(2 * m + z2.len());
+ let mut result = z0;
+ result.reserve_exact(len - result.len());
+ iadd_impl(&mut result, &z1, m);
+ iadd_impl(&mut result, &z2, 2 * m);
+
+ result
+ }
+ }
+
+ /// Karatsuba multiplication algorithm where y is substantially larger than x.
+ ///
+ /// Assumes `y.len() >= x.len()`.
+ fn karatsuba_uneven_mul(x: &[Limb], mut y: &[Limb]) -> Vec<Limb> {
+ let mut result = Vec::<Limb>::default();
+ result.resize(x.len() + y.len(), 0);
+
+ // This effectively is like grade-school multiplication between
+ // two numbers, except we're using splits on `y`, and the intermediate
+ // step is a Karatsuba multiplication.
+ let mut start = 0;
+ while !y.is_empty() {
+ let m = x.len().min(y.len());
+ let (yl, yh) = karatsuba_split(y, m);
+ let prod = karatsuba_mul(x, yl);
+ iadd_impl(&mut result, &prod, start);
+ y = yh;
+ start += m;
+ }
+ small::normalize(&mut result);
+
+ result
+ }
+
+ /// Forwarder to the proper Karatsuba algorithm.
+ #[inline]
+ fn karatsuba_mul_fwd(x: &[Limb], y: &[Limb]) -> Vec<Limb> {
+ if x.len() < y.len() {
+ karatsuba_mul(x, y)
+ } else {
+ karatsuba_mul(y, x)
+ }
+ }
+
+ /// MulAssign bigint to bigint.
+ #[inline]
+ pub fn imul(x: &mut Vec<Limb>, y: &[Limb]) {
+ if y.len() == 1 {
+ small::imul(x, y[0]);
+ } else {
+ // We're not really in a condition where using Karatsuba
+ // multiplication makes sense, so we're just going to use long
+ // division. ~20% speedup compared to:
+ // *x = karatsuba_mul_fwd(x, y);
+ *x = karatsuba_mul_fwd(x, y);
+ }
+ }
+} // large
+
+// TRAITS
+// ------
+
+/// Traits for shared operations for big integers.
+///
+/// None of these are implemented using normal traits, since these
+/// are very expensive operations, and we want to deliberately
+/// and explicitly use these functions.
+pub(crate) trait Math: Clone + Sized + Default {
+ // DATA
+
+ /// Get access to the underlying data
+ fn data(&self) -> &Vec<Limb>;
+
+ /// Get access to the underlying data
+ fn data_mut(&mut self) -> &mut Vec<Limb>;
+
+ // RELATIVE OPERATIONS
+
+ /// Compare self to y.
+ #[inline]
+ fn compare(&self, y: &Self) -> cmp::Ordering {
+ large::compare(self.data(), y.data())
+ }
+
+ // PROPERTIES
+
+ /// Get the high 64-bits from the bigint and if there are remaining bits.
+ #[inline]
+ fn hi64(&self) -> (u64, bool) {
+ self.data().as_slice().hi64()
+ }
+
+ /// Calculate the bit-length of the big-integer.
+ /// Returns usize::max_value() if the value overflows,
+ /// IE, if `self.data().len() > usize::max_value() / 8`.
+ #[inline]
+ fn bit_length(&self) -> usize {
+ small::bit_length(self.data())
+ }
+
+ // INTEGER CONVERSIONS
+
+ /// Create new big integer from u64.
+ #[inline]
+ fn from_u64(x: u64) -> Self {
+ let mut v = Self::default();
+ let slc = split_u64(x);
+ v.data_mut().extend_from_slice(&slc);
+ v.normalize();
+ v
+ }
+
+ // NORMALIZE
+
+ /// Normalize the integer, so any leading zero values are removed.
+ #[inline]
+ fn normalize(&mut self) {
+ small::normalize(self.data_mut());
+ }
+
+ // ADDITION
+
+ /// AddAssign small integer.
+ #[inline]
+ fn iadd_small(&mut self, y: Limb) {
+ small::iadd(self.data_mut(), y);
+ }
+
+ // MULTIPLICATION
+
+ /// MulAssign small integer.
+ #[inline]
+ fn imul_small(&mut self, y: Limb) {
+ small::imul(self.data_mut(), y);
+ }
+
+ /// Multiply by a power of 2.
+ #[inline]
+ fn imul_pow2(&mut self, n: u32) {
+ 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);
+ }
+
+ /// MulAssign by a power of 10.
+ #[inline]
+ fn imul_pow10(&mut self, n: u32) {
+ self.imul_pow5(n);
+ self.imul_pow2(n);
+ }
+
+ // SHIFTS
+
+ /// Shift-left the entire buffer n bits.
+ #[inline]
+ fn ishl(&mut self, n: usize) {
+ small::ishl(self.data_mut(), n);
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/mod.rs b/third_party/rust/serde_json/src/lexical/mod.rs
new file mode 100644
index 0000000000..b1a45e218d
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/mod.rs
@@ -0,0 +1,38 @@
+// The code in this module is derived from the `lexical` crate by @Alexhuszagh
+// which the author condensed into this minimal subset for use in serde_json.
+// For the serde_json use case we care more about reliably round tripping all
+// possible floating point values than about parsing any arbitrarily long string
+// of digits with perfect accuracy, as the latter would take a high cost in
+// compile time and performance.
+//
+// Dual licensed as MIT and Apache 2.0 just like the rest of serde_json, but
+// copyright Alexander Huszagh.
+
+//! Fast, minimal float-parsing algorithm.
+
+// MODULES
+pub(crate) mod algorithm;
+mod bhcomp;
+mod bignum;
+mod cached;
+mod cached_float80;
+mod digit;
+mod errors;
+pub(crate) mod exponent;
+pub(crate) mod float;
+mod large_powers;
+pub(crate) mod math;
+pub(crate) mod num;
+pub(crate) mod parse;
+pub(crate) mod rounding;
+mod shift;
+mod small_powers;
+
+#[cfg(limb_width_32)]
+mod large_powers32;
+
+#[cfg(limb_width_64)]
+mod large_powers64;
+
+// API
+pub use self::parse::{parse_concise_float, parse_truncated_float};
diff --git a/third_party/rust/serde_json/src/lexical/num.rs b/third_party/rust/serde_json/src/lexical/num.rs
new file mode 100644
index 0000000000..e47e003419
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/num.rs
@@ -0,0 +1,440 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Utilities for Rust numbers.
+
+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.
+const F32_POW10: [f32; 11] = [
+ 1.0,
+ 10.0,
+ 100.0,
+ 1000.0,
+ 10000.0,
+ 100000.0,
+ 1000000.0,
+ 10000000.0,
+ 100000000.0,
+ 1000000000.0,
+ 10000000000.0,
+];
+
+/// Precalculated values of radix**i for i in range [0, arr.len()-1].
+/// Each value can be **exactly** represented as that type.
+const F64_POW10: [f64; 23] = [
+ 1.0,
+ 10.0,
+ 100.0,
+ 1000.0,
+ 10000.0,
+ 100000.0,
+ 1000000.0,
+ 10000000.0,
+ 100000000.0,
+ 1000000000.0,
+ 10000000000.0,
+ 100000000000.0,
+ 1000000000000.0,
+ 10000000000000.0,
+ 100000000000000.0,
+ 1000000000000000.0,
+ 10000000000000000.0,
+ 100000000000000000.0,
+ 1000000000000000000.0,
+ 10000000000000000000.0,
+ 100000000000000000000.0,
+ 1000000000000000000000.0,
+ 10000000000000000000000.0,
+];
+
+/// Type that can be converted to primitive with `as`.
+pub trait AsPrimitive: Sized + Copy + PartialOrd {
+ fn as_u32(self) -> u32;
+ fn as_u64(self) -> u64;
+ fn as_u128(self) -> u128;
+ fn as_usize(self) -> usize;
+ fn as_f32(self) -> f32;
+ fn as_f64(self) -> f64;
+}
+
+macro_rules! as_primitive_impl {
+ ($($ty:ident)*) => {
+ $(
+ impl AsPrimitive for $ty {
+ #[inline]
+ fn as_u32(self) -> u32 {
+ self as u32
+ }
+
+ #[inline]
+ fn as_u64(self) -> u64 {
+ self as u64
+ }
+
+ #[inline]
+ fn as_u128(self) -> u128 {
+ self as u128
+ }
+
+ #[inline]
+ fn as_usize(self) -> usize {
+ self as usize
+ }
+
+ #[inline]
+ fn as_f32(self) -> f32 {
+ self as f32
+ }
+
+ #[inline]
+ fn as_f64(self) -> f64 {
+ self as f64
+ }
+ }
+ )*
+ };
+}
+
+as_primitive_impl! { u32 u64 u128 usize f32 f64 }
+
+/// An interface for casting between machine scalars.
+pub trait AsCast: AsPrimitive {
+ /// Creates a number from another value that can be converted into
+ /// a primitive via the `AsPrimitive` trait.
+ fn as_cast<N: AsPrimitive>(n: N) -> Self;
+}
+
+macro_rules! as_cast_impl {
+ ($ty:ident, $method:ident) => {
+ impl AsCast for $ty {
+ #[inline]
+ fn as_cast<N: AsPrimitive>(n: N) -> Self {
+ n.$method()
+ }
+ }
+ };
+}
+
+as_cast_impl!(u32, as_u32);
+as_cast_impl!(u64, as_u64);
+as_cast_impl!(u128, as_u128);
+as_cast_impl!(usize, as_usize);
+as_cast_impl!(f32, as_f32);
+as_cast_impl!(f64, as_f64);
+
+/// Numerical type trait.
+pub trait Number: AsCast + ops::Add<Output = Self> {}
+
+macro_rules! number_impl {
+ ($($ty:ident)*) => {
+ $(
+ impl Number for $ty {}
+ )*
+ };
+}
+
+number_impl! { u32 u64 u128 usize f32 f64 }
+
+/// Defines a trait that supports integral operations.
+pub trait Integer: Number + ops::BitAnd<Output = Self> + ops::Shr<i32, Output = Self> {
+ const ZERO: Self;
+}
+
+macro_rules! integer_impl {
+ ($($ty:tt)*) => {
+ $(
+ impl Integer for $ty {
+ const ZERO: Self = 0;
+ }
+ )*
+ };
+}
+
+integer_impl! { u32 u64 u128 usize }
+
+/// Type trait for the mantissa type.
+pub trait Mantissa: Integer {
+ /// Mask to extract the high bits from the integer.
+ const HIMASK: Self;
+ /// Mask to extract the low bits from the integer.
+ const LOMASK: Self;
+ /// Full size of the integer, in bits.
+ const FULL: i32;
+ /// Half size of the integer, in bits.
+ const HALF: i32 = Self::FULL / 2;
+}
+
+impl Mantissa for u64 {
+ const HIMASK: u64 = 0xFFFFFFFF00000000;
+ const LOMASK: u64 = 0x00000000FFFFFFFF;
+ const FULL: i32 = 64;
+}
+
+/// Get exact exponent limit for radix.
+pub trait Float: Number {
+ /// Unsigned type of the same size.
+ type Unsigned: Integer;
+
+ /// Literal zero.
+ const ZERO: Self;
+ /// Maximum number of digits that can contribute in the mantissa.
+ ///
+ /// We can exactly represent a float in radix `b` from radix 2 if
+ /// `b` is divisible by 2. This function calculates the exact number of
+ /// digits required to exactly represent that float.
+ ///
+ /// According to the "Handbook of Floating Point Arithmetic",
+ /// for IEEE754, with emin being the min exponent, p2 being the
+ /// precision, and b being the radix, the number of digits follows as:
+ ///
+ /// `−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋`
+ ///
+ /// For f32, this follows as:
+ /// emin = -126
+ /// p2 = 24
+ ///
+ /// For f64, this follows as:
+ /// emin = -1022
+ /// p2 = 53
+ ///
+ /// In Python:
+ /// `-emin + p2 + math.floor((emin+1)*math.log(2, b) - math.log(1-2**(-p2), b))`
+ ///
+ /// This was used to calculate the maximum number of digits for [2, 36].
+ const MAX_DIGITS: usize;
+
+ // MASKS
+
+ /// Bitmask for the sign bit.
+ const SIGN_MASK: Self::Unsigned;
+ /// Bitmask for the exponent, including the hidden bit.
+ const EXPONENT_MASK: Self::Unsigned;
+ /// Bitmask for the hidden bit in exponent, which is an implicit 1 in the fraction.
+ const HIDDEN_BIT_MASK: Self::Unsigned;
+ /// Bitmask for the mantissa (fraction), excluding the hidden bit.
+ const MANTISSA_MASK: Self::Unsigned;
+
+ // PROPERTIES
+
+ /// Positive infinity as bits.
+ const INFINITY_BITS: Self::Unsigned;
+ /// Positive infinity as bits.
+ const NEGATIVE_INFINITY_BITS: Self::Unsigned;
+ /// Size of the significand (mantissa) without hidden bit.
+ const MANTISSA_SIZE: i32;
+ /// Bias of the exponet
+ const EXPONENT_BIAS: i32;
+ /// Exponent portion of a denormal float.
+ const DENORMAL_EXPONENT: i32;
+ /// Maximum exponent value in float.
+ const MAX_EXPONENT: i32;
+
+ // ROUNDING
+
+ /// Default number of bits to shift (or 64 - mantissa size - 1).
+ const DEFAULT_SHIFT: i32;
+ /// Mask to determine if a full-carry occurred (1 in bit above hidden bit).
+ const CARRY_MASK: u64;
+
+ /// Get min and max exponent limits (exact) from radix.
+ fn exponent_limit() -> (i32, i32);
+
+ /// Get the number of digits that can be shifted from exponent to mantissa.
+ fn mantissa_limit() -> i32;
+
+ // Re-exported methods from std.
+ fn pow10(self, n: i32) -> Self;
+ fn from_bits(u: Self::Unsigned) -> Self;
+ fn to_bits(self) -> Self::Unsigned;
+ fn is_sign_positive(self) -> bool;
+ fn is_sign_negative(self) -> bool;
+
+ /// Returns true if the float is a denormal.
+ #[inline]
+ fn is_denormal(self) -> bool {
+ self.to_bits() & Self::EXPONENT_MASK == Self::Unsigned::ZERO
+ }
+
+ /// Returns true if the float is a NaN or Infinite.
+ #[inline]
+ fn is_special(self) -> bool {
+ self.to_bits() & Self::EXPONENT_MASK == Self::EXPONENT_MASK
+ }
+
+ /// Returns true if the float is infinite.
+ #[inline]
+ fn is_inf(self) -> bool {
+ self.is_special() && (self.to_bits() & Self::MANTISSA_MASK) == Self::Unsigned::ZERO
+ }
+
+ /// Get exponent component from the float.
+ #[inline]
+ fn exponent(self) -> i32 {
+ if self.is_denormal() {
+ return Self::DENORMAL_EXPONENT;
+ }
+
+ let bits = self.to_bits();
+ let biased_e = ((bits & Self::EXPONENT_MASK) >> Self::MANTISSA_SIZE).as_u32();
+ biased_e as i32 - Self::EXPONENT_BIAS
+ }
+
+ /// Get mantissa (significand) component from float.
+ #[inline]
+ fn mantissa(self) -> Self::Unsigned {
+ let bits = self.to_bits();
+ let s = bits & Self::MANTISSA_MASK;
+ if !self.is_denormal() {
+ s + Self::HIDDEN_BIT_MASK
+ } else {
+ s
+ }
+ }
+
+ /// Get next greater float for a positive float.
+ /// Value must be >= 0.0 and < INFINITY.
+ #[inline]
+ fn next_positive(self) -> Self {
+ debug_assert!(self.is_sign_positive() && !self.is_inf());
+ Self::from_bits(self.to_bits() + Self::Unsigned::as_cast(1u32))
+ }
+
+ /// Round a positive number to even.
+ #[inline]
+ fn round_positive_even(self) -> Self {
+ if self.mantissa() & Self::Unsigned::as_cast(1u32) == Self::Unsigned::as_cast(1u32) {
+ self.next_positive()
+ } else {
+ self
+ }
+ }
+}
+
+impl Float for f32 {
+ type Unsigned = u32;
+
+ const ZERO: f32 = 0.0;
+ const MAX_DIGITS: usize = 114;
+ const SIGN_MASK: u32 = 0x80000000;
+ const EXPONENT_MASK: u32 = 0x7F800000;
+ const HIDDEN_BIT_MASK: u32 = 0x00800000;
+ const MANTISSA_MASK: u32 = 0x007FFFFF;
+ const INFINITY_BITS: u32 = 0x7F800000;
+ const NEGATIVE_INFINITY_BITS: u32 = Self::INFINITY_BITS | Self::SIGN_MASK;
+ const MANTISSA_SIZE: i32 = 23;
+ const EXPONENT_BIAS: i32 = 127 + Self::MANTISSA_SIZE;
+ const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
+ const MAX_EXPONENT: i32 = 0xFF - Self::EXPONENT_BIAS;
+ const DEFAULT_SHIFT: i32 = u64::FULL - f32::MANTISSA_SIZE - 1;
+ const CARRY_MASK: u64 = 0x1000000;
+
+ #[inline]
+ fn exponent_limit() -> (i32, i32) {
+ (-10, 10)
+ }
+
+ #[inline]
+ fn mantissa_limit() -> i32 {
+ 7
+ }
+
+ #[inline]
+ fn pow10(self, n: i32) -> f32 {
+ // Check the exponent is within bounds in debug builds.
+ debug_assert!({
+ let (min, max) = Self::exponent_limit();
+ n >= min && n <= max
+ });
+
+ if n > 0 {
+ self * F32_POW10[n as usize]
+ } else {
+ self / F32_POW10[-n as usize]
+ }
+ }
+
+ #[inline]
+ fn from_bits(u: u32) -> f32 {
+ f32::from_bits(u)
+ }
+
+ #[inline]
+ fn to_bits(self) -> u32 {
+ f32::to_bits(self)
+ }
+
+ #[inline]
+ fn is_sign_positive(self) -> bool {
+ f32::is_sign_positive(self)
+ }
+
+ #[inline]
+ fn is_sign_negative(self) -> bool {
+ f32::is_sign_negative(self)
+ }
+}
+
+impl Float for f64 {
+ type Unsigned = u64;
+
+ const ZERO: f64 = 0.0;
+ const MAX_DIGITS: usize = 769;
+ const SIGN_MASK: u64 = 0x8000000000000000;
+ const EXPONENT_MASK: u64 = 0x7FF0000000000000;
+ const HIDDEN_BIT_MASK: u64 = 0x0010000000000000;
+ const MANTISSA_MASK: u64 = 0x000FFFFFFFFFFFFF;
+ const INFINITY_BITS: u64 = 0x7FF0000000000000;
+ const NEGATIVE_INFINITY_BITS: u64 = Self::INFINITY_BITS | Self::SIGN_MASK;
+ const MANTISSA_SIZE: i32 = 52;
+ const EXPONENT_BIAS: i32 = 1023 + Self::MANTISSA_SIZE;
+ const DENORMAL_EXPONENT: i32 = 1 - Self::EXPONENT_BIAS;
+ const MAX_EXPONENT: i32 = 0x7FF - Self::EXPONENT_BIAS;
+ const DEFAULT_SHIFT: i32 = u64::FULL - f64::MANTISSA_SIZE - 1;
+ const CARRY_MASK: u64 = 0x20000000000000;
+
+ #[inline]
+ fn exponent_limit() -> (i32, i32) {
+ (-22, 22)
+ }
+
+ #[inline]
+ fn mantissa_limit() -> i32 {
+ 15
+ }
+
+ #[inline]
+ fn pow10(self, n: i32) -> f64 {
+ // Check the exponent is within bounds in debug builds.
+ debug_assert!({
+ let (min, max) = Self::exponent_limit();
+ n >= min && n <= max
+ });
+
+ if n > 0 {
+ self * F64_POW10[n as usize]
+ } else {
+ self / F64_POW10[-n as usize]
+ }
+ }
+
+ #[inline]
+ fn from_bits(u: u64) -> f64 {
+ f64::from_bits(u)
+ }
+
+ #[inline]
+ fn to_bits(self) -> u64 {
+ f64::to_bits(self)
+ }
+
+ #[inline]
+ fn is_sign_positive(self) -> bool {
+ f64::is_sign_positive(self)
+ }
+
+ #[inline]
+ fn is_sign_negative(self) -> bool {
+ f64::is_sign_negative(self)
+ }
+}
diff --git a/third_party/rust/serde_json/src/lexical/parse.rs b/third_party/rust/serde_json/src/lexical/parse.rs
new file mode 100644
index 0000000000..e3d7f1e871
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/parse.rs
@@ -0,0 +1,83 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use super::algorithm::*;
+use super::bhcomp::*;
+use super::digit::*;
+use super::exponent::*;
+use super::num::*;
+
+// PARSERS
+// -------
+
+/// Parse float for which the entire integer and fraction parts fit into a 64
+/// bit mantissa.
+pub fn parse_concise_float<F>(mantissa: u64, mant_exp: i32) -> F
+where
+ F: Float,
+{
+ if let Some(float) = fast_path(mantissa, mant_exp) {
+ return float;
+ }
+
+ // Moderate path (use an extended 80-bit representation).
+ let truncated = false;
+ let (fp, valid) = moderate_path::<F>(mantissa, mant_exp, truncated);
+ if valid {
+ return fp.into_float::<F>();
+ }
+
+ let b = fp.into_downward_float::<F>();
+ if b.is_special() {
+ // We have a non-finite number, we get to leave early.
+ return b;
+ }
+
+ // Slow path, fast path didn't work.
+ let mut buffer = itoa::Buffer::new();
+ let integer = buffer.format(mantissa).as_bytes();
+ let fraction = &[];
+ bhcomp(b, integer, fraction, mant_exp)
+}
+
+/// Parse float from extracted float components.
+///
+/// * `integer` - Slice containing the integer digits.
+/// * `fraction` - Slice containing the fraction digits.
+/// * `exponent` - Parsed, 32-bit exponent.
+///
+/// Precondition: The integer must not have leading zeros.
+pub fn parse_truncated_float<F>(integer: &[u8], mut fraction: &[u8], exponent: i32) -> F
+where
+ F: Float,
+{
+ // Trim trailing zeroes from the fraction part.
+ while fraction.last() == Some(&b'0') {
+ fraction = &fraction[..fraction.len() - 1];
+ }
+
+ // Calculate the number of truncated digits.
+ let mut truncated = 0;
+ let mut mantissa: u64 = 0;
+ let mut iter = integer.iter().chain(fraction);
+ for &c in &mut iter {
+ mantissa = match add_digit(mantissa, to_digit(c).unwrap()) {
+ Some(v) => v,
+ None => {
+ truncated = 1 + iter.count();
+ break;
+ }
+ };
+ }
+
+ let mant_exp = mantissa_exponent(exponent, fraction.len(), truncated);
+ let is_truncated = true;
+
+ fallback_path(
+ integer,
+ fraction,
+ mantissa,
+ exponent,
+ mant_exp,
+ is_truncated,
+ )
+}
diff --git a/third_party/rust/serde_json/src/lexical/rounding.rs b/third_party/rust/serde_json/src/lexical/rounding.rs
new file mode 100644
index 0000000000..6ec1292aa5
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/rounding.rs
@@ -0,0 +1,231 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Defines rounding schemes for floating-point numbers.
+
+use super::float::ExtendedFloat;
+use super::num::*;
+use super::shift::*;
+use core::mem;
+
+// MASKS
+
+/// Calculate a scalar factor of 2 above the halfway point.
+#[inline]
+pub(crate) fn nth_bit(n: u64) -> u64 {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!(n < bits, "nth_bit() overflow in shl.");
+
+ 1 << n
+}
+
+/// Generate a bitwise mask for the lower `n` bits.
+#[inline]
+pub(crate) fn lower_n_mask(n: u64) -> u64 {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!(n <= bits, "lower_n_mask() overflow in shl.");
+
+ if n == bits {
+ u64::max_value()
+ } else {
+ (1 << n) - 1
+ }
+}
+
+/// Calculate the halfway point for the lower `n` bits.
+#[inline]
+pub(crate) fn lower_n_halfway(n: u64) -> u64 {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!(n <= bits, "lower_n_halfway() overflow in shl.");
+
+ if n == 0 {
+ 0
+ } else {
+ nth_bit(n - 1)
+ }
+}
+
+/// Calculate a bitwise mask with `n` 1 bits starting at the `bit` position.
+#[inline]
+pub(crate) fn internal_n_mask(bit: u64, n: u64) -> u64 {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!(bit <= bits, "internal_n_halfway() overflow in shl.");
+ debug_assert!(n <= bits, "internal_n_halfway() overflow in shl.");
+ debug_assert!(bit >= n, "internal_n_halfway() overflow in sub.");
+
+ lower_n_mask(bit) ^ lower_n_mask(bit - n)
+}
+
+// NEAREST ROUNDING
+
+// Shift right N-bytes and round to the nearest.
+//
+// Return if we are above halfway and if we are halfway.
+#[inline]
+pub(crate) fn round_nearest(fp: &mut ExtendedFloat, shift: i32) -> (bool, bool) {
+ // Extract the truncated bits using mask.
+ // Calculate if the value of the truncated bits are either above
+ // the mid-way point, or equal to it.
+ //
+ // For example, for 4 truncated bytes, the mask would be b1111
+ // and the midway point would be b1000.
+ let mask: u64 = lower_n_mask(shift as u64);
+ let halfway: u64 = lower_n_halfway(shift as u64);
+
+ let truncated_bits = fp.mant & mask;
+ let is_above = truncated_bits > halfway;
+ let is_halfway = truncated_bits == halfway;
+
+ // Bit shift so the leading bit is in the hidden bit.
+ overflowing_shr(fp, shift);
+
+ (is_above, is_halfway)
+}
+
+// Tie rounded floating point to event.
+#[inline]
+pub(crate) fn tie_even(fp: &mut ExtendedFloat, is_above: bool, is_halfway: bool) {
+ // Extract the last bit after shifting (and determine if it is odd).
+ let is_odd = fp.mant & 1 == 1;
+
+ // Calculate if we need to roundup.
+ // We need to roundup if we are above halfway, or if we are odd
+ // and at half-way (need to tie-to-even).
+ if is_above || (is_odd && is_halfway) {
+ fp.mant += 1;
+ }
+}
+
+// Shift right N-bytes and round nearest, tie-to-even.
+//
+// Floating-point arithmetic uses round to nearest, ties to even,
+// which rounds to the nearest value, if the value is halfway in between,
+// round to an even value.
+#[inline]
+pub(crate) fn round_nearest_tie_even(fp: &mut ExtendedFloat, shift: i32) {
+ let (is_above, is_halfway) = round_nearest(fp, shift);
+ tie_even(fp, is_above, is_halfway);
+}
+
+// DIRECTED ROUNDING
+
+// Shift right N-bytes and round towards a direction.
+//
+// Return if we have any truncated bytes.
+#[inline]
+fn round_toward(fp: &mut ExtendedFloat, shift: i32) -> bool {
+ let mask: u64 = lower_n_mask(shift as u64);
+ let truncated_bits = fp.mant & mask;
+
+ // Bit shift so the leading bit is in the hidden bit.
+ overflowing_shr(fp, shift);
+
+ truncated_bits != 0
+}
+
+// Round down.
+#[inline]
+fn downard(_: &mut ExtendedFloat, _: bool) {}
+
+// Shift right N-bytes and round toward zero.
+//
+// Floating-point arithmetic defines round toward zero, which rounds
+// towards positive zero.
+#[inline]
+pub(crate) fn round_downward(fp: &mut ExtendedFloat, shift: i32) {
+ // Bit shift so the leading bit is in the hidden bit.
+ // No rounding schemes, so we just ignore everything else.
+ let is_truncated = round_toward(fp, shift);
+ downard(fp, is_truncated);
+}
+
+// ROUND TO FLOAT
+
+// Shift the ExtendedFloat fraction to the fraction bits in a native float.
+//
+// Floating-point arithmetic uses round to nearest, ties to even,
+// which rounds to the nearest value, if the value is halfway in between,
+// round to an even value.
+#[inline]
+pub(crate) fn round_to_float<F, Algorithm>(fp: &mut ExtendedFloat, algorithm: Algorithm)
+where
+ F: Float,
+ Algorithm: FnOnce(&mut ExtendedFloat, i32),
+{
+ // Calculate the difference to allow a single calculation
+ // rather than a loop, to minimize the number of ops required.
+ // This does underflow detection.
+ let final_exp = fp.exp + F::DEFAULT_SHIFT;
+ if final_exp < F::DENORMAL_EXPONENT {
+ // We would end up with a denormal exponent, try to round to more
+ // digits. Only shift right if we can avoid zeroing out the value,
+ // which requires the exponent diff to be < M::BITS. The value
+ // is already normalized, so we shouldn't have any issue zeroing
+ // out the value.
+ let diff = F::DENORMAL_EXPONENT - fp.exp;
+ if diff <= u64::FULL {
+ // We can avoid underflow, can get a valid representation.
+ algorithm(fp, diff);
+ } else {
+ // Certain underflow, assign literal 0s.
+ fp.mant = 0;
+ fp.exp = 0;
+ }
+ } else {
+ algorithm(fp, F::DEFAULT_SHIFT);
+ }
+
+ if fp.mant & F::CARRY_MASK == F::CARRY_MASK {
+ // Roundup carried over to 1 past the hidden bit.
+ shr(fp, 1);
+ }
+}
+
+// AVOID OVERFLOW/UNDERFLOW
+
+// Avoid overflow for large values, shift left as needed.
+//
+// Shift until a 1-bit is in the hidden bit, if the mantissa is not 0.
+#[inline]
+pub(crate) fn avoid_overflow<F>(fp: &mut ExtendedFloat)
+where
+ F: Float,
+{
+ // Calculate the difference to allow a single calculation
+ // rather than a loop, minimizing the number of ops required.
+ if fp.exp >= F::MAX_EXPONENT {
+ let diff = fp.exp - F::MAX_EXPONENT;
+ if diff <= F::MANTISSA_SIZE {
+ // Our overflow mask needs to start at the hidden bit, or at
+ // `F::MANTISSA_SIZE+1`, and needs to have `diff+1` bits set,
+ // to see if our value overflows.
+ let bit = (F::MANTISSA_SIZE + 1) as u64;
+ let n = (diff + 1) as u64;
+ let mask = internal_n_mask(bit, n);
+ if (fp.mant & mask) == 0 {
+ // If we have no 1-bit in the hidden-bit position,
+ // which is index 0, we need to shift 1.
+ let shift = diff + 1;
+ shl(fp, shift);
+ }
+ }
+ }
+}
+
+// ROUND TO NATIVE
+
+// Round an extended-precision float to a native float representation.
+#[inline]
+pub(crate) fn round_to_native<F, Algorithm>(fp: &mut ExtendedFloat, algorithm: Algorithm)
+where
+ F: Float,
+ Algorithm: FnOnce(&mut ExtendedFloat, i32),
+{
+ // Shift all the way left, to ensure a consistent representation.
+ // The following right-shifts do not work for a non-normalized number.
+ fp.normalize();
+
+ // Round so the fraction is in a native mantissa representation,
+ // and avoid overflow/underflow.
+ round_to_float::<F, _>(fp, algorithm);
+ avoid_overflow::<F>(fp);
+}
diff --git a/third_party/rust/serde_json/src/lexical/shift.rs b/third_party/rust/serde_json/src/lexical/shift.rs
new file mode 100644
index 0000000000..a0bae01e0f
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/shift.rs
@@ -0,0 +1,46 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Bit-shift helpers.
+
+use super::float::ExtendedFloat;
+use core::mem;
+
+// Shift extended-precision float right `shift` bytes.
+#[inline]
+pub(crate) fn shr(fp: &mut ExtendedFloat, shift: i32) {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!((shift as u64) < bits, "shr() overflow in shift right.");
+
+ fp.mant >>= shift;
+ fp.exp += shift;
+}
+
+// Shift extended-precision float right `shift` bytes.
+//
+// Accepts when the shift is the same as the type size, and
+// sets the value to 0.
+#[inline]
+pub(crate) fn overflowing_shr(fp: &mut ExtendedFloat, shift: i32) {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!(
+ (shift as u64) <= bits,
+ "overflowing_shr() overflow in shift right."
+ );
+
+ fp.mant = if shift as u64 == bits {
+ 0
+ } else {
+ fp.mant >> shift
+ };
+ fp.exp += shift;
+}
+
+// Shift extended-precision float left `shift` bytes.
+#[inline]
+pub(crate) fn shl(fp: &mut ExtendedFloat, shift: i32) {
+ let bits: u64 = mem::size_of::<u64>() as u64 * 8;
+ debug_assert!((shift as u64) < bits, "shl() overflow in shift left.");
+
+ fp.mant <<= shift;
+ fp.exp -= shift;
+}
diff --git a/third_party/rust/serde_json/src/lexical/small_powers.rs b/third_party/rust/serde_json/src/lexical/small_powers.rs
new file mode 100644
index 0000000000..219d826116
--- /dev/null
+++ b/third_party/rust/serde_json/src/lexical/small_powers.rs
@@ -0,0 +1,70 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+//! Pre-computed small powers.
+
+// 32 BIT
+#[cfg(limb_width_32)]
+pub(crate) const POW5_32: [u32; 14] = [
+ 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625,
+ 1220703125,
+];
+
+#[cfg(limb_width_32)]
+pub(crate) const POW10_32: [u32; 10] = [
+ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
+];
+
+// 64 BIT
+#[cfg(limb_width_64)]
+pub(crate) const POW5_64: [u64; 28] = [
+ 1,
+ 5,
+ 25,
+ 125,
+ 625,
+ 3125,
+ 15625,
+ 78125,
+ 390625,
+ 1953125,
+ 9765625,
+ 48828125,
+ 244140625,
+ 1220703125,
+ 6103515625,
+ 30517578125,
+ 152587890625,
+ 762939453125,
+ 3814697265625,
+ 19073486328125,
+ 95367431640625,
+ 476837158203125,
+ 2384185791015625,
+ 11920928955078125,
+ 59604644775390625,
+ 298023223876953125,
+ 1490116119384765625,
+ 7450580596923828125,
+];
+pub(crate) const POW10_64: [u64; 20] = [
+ 1,
+ 10,
+ 100,
+ 1000,
+ 10000,
+ 100000,
+ 1000000,
+ 10000000,
+ 100000000,
+ 1000000000,
+ 10000000000,
+ 100000000000,
+ 1000000000000,
+ 10000000000000,
+ 100000000000000,
+ 1000000000000000,
+ 10000000000000000,
+ 100000000000000000,
+ 1000000000000000000,
+ 10000000000000000000,
+];
diff --git a/third_party/rust/serde_json/src/lib.rs b/third_party/rust/serde_json/src/lib.rs
new file mode 100644
index 0000000000..fdd95a1218
--- /dev/null
+++ b/third_party/rust/serde_json/src/lib.rs
@@ -0,0 +1,421 @@
+//! # Serde JSON
+//!
+//! JSON is a ubiquitous open-standard format that uses human-readable text to
+//! transmit data objects consisting of key-value pairs.
+//!
+//! ```json
+//! {
+//! "name": "John Doe",
+//! "age": 43,
+//! "address": {
+//! "street": "10 Downing Street",
+//! "city": "London"
+//! },
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! }
+//! ```
+//!
+//! There are three common ways that you might find yourself needing to work
+//! with JSON data in Rust.
+//!
+//! - **As text data.** An unprocessed string of JSON data that you receive on
+//! an HTTP endpoint, read from a file, or prepare to send to a remote
+//! server.
+//! - **As an untyped or loosely typed representation.** Maybe you want to
+//! check that some JSON data is valid before passing it on, but without
+//! knowing the structure of what it contains. Or you want to do very basic
+//! manipulations like insert a key in a particular spot.
+//! - **As a strongly typed Rust data structure.** When you expect all or most
+//! of your data to conform to a particular structure and want to get real
+//! work done without JSON's loosey-goosey nature tripping you up.
+//!
+//! Serde JSON provides efficient, flexible, safe ways of converting data
+//! between each of these representations.
+//!
+//! # Operating on untyped JSON values
+//!
+//! Any valid JSON data can be manipulated in the following recursive enum
+//! representation. This data structure is [`serde_json::Value`][value].
+//!
+//! ```
+//! # use serde_json::{Number, Map};
+//! #
+//! # #[allow(dead_code)]
+//! enum Value {
+//! Null,
+//! Bool(bool),
+//! Number(Number),
+//! String(String),
+//! Array(Vec<Value>),
+//! Object(Map<String, Value>),
+//! }
+//! ```
+//!
+//! A string of JSON data can be parsed into a `serde_json::Value` by the
+//! [`serde_json::from_str`][from_str] function. There is also
+//! [`from_slice`][from_slice] for parsing from a byte slice &[u8] and
+//! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or
+//! a TCP stream.
+//!
+//! ```
+//! use serde_json::{Result, Value};
+//!
+//! fn untyped_example() -> Result<()> {
+//! // Some JSON input data as a &str. Maybe this comes from the user.
+//! let data = r#"
+//! {
+//! "name": "John Doe",
+//! "age": 43,
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! }"#;
+//!
+//! // Parse the string of data into serde_json::Value.
+//! let v: Value = serde_json::from_str(data)?;
+//!
+//! // Access parts of the data by indexing with square brackets.
+//! println!("Please call {} at the number {}", v["name"], v["phones"][0]);
+//!
+//! Ok(())
+//! }
+//! #
+//! # fn main() {
+//! # untyped_example().unwrap();
+//! # }
+//! ```
+//!
+//! The result of square bracket indexing like `v["name"]` is a borrow of the
+//! data at that index, so the type is `&Value`. A JSON map can be indexed with
+//! string keys, while a JSON array can be indexed with integer keys. If the
+//! type of the data is not right for the type with which it is being indexed,
+//! or if a map does not contain the key being indexed, or if the index into a
+//! vector is out of bounds, the returned element is `Value::Null`.
+//!
+//! When a `Value` is printed, it is printed as a JSON string. So in the code
+//! above, the output looks like `Please call "John Doe" at the number "+44
+//! 1234567"`. The quotation marks appear because `v["name"]` is a `&Value`
+//! containing a JSON string and its JSON representation is `"John Doe"`.
+//! Printing as a plain string without quotation marks involves converting from
+//! a JSON string to a Rust string with [`as_str()`] or avoiding the use of
+//! `Value` as described in the following section.
+//!
+//! [`as_str()`]: https://docs.serde.rs/serde_json/enum.Value.html#method.as_str
+//!
+//! The `Value` representation is sufficient for very basic tasks but can be
+//! tedious to work with for anything more significant. Error handling is
+//! verbose to implement correctly, for example imagine trying to detect the
+//! presence of unrecognized fields in the input data. The compiler is powerless
+//! to help you when you make a mistake, for example imagine typoing `v["name"]`
+//! as `v["nmae"]` in one of the dozens of places it is used in your code.
+//!
+//! # Parsing JSON as strongly typed data structures
+//!
+//! Serde provides a powerful way of mapping JSON data into Rust data structures
+//! largely automatically.
+//!
+//! ```
+//! use serde::{Deserialize, Serialize};
+//! use serde_json::Result;
+//!
+//! #[derive(Serialize, Deserialize)]
+//! struct Person {
+//! name: String,
+//! age: u8,
+//! phones: Vec<String>,
+//! }
+//!
+//! fn typed_example() -> Result<()> {
+//! // Some JSON input data as a &str. Maybe this comes from the user.
+//! let data = r#"
+//! {
+//! "name": "John Doe",
+//! "age": 43,
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! }"#;
+//!
+//! // Parse the string of data into a Person object. This is exactly the
+//! // same function as the one that produced serde_json::Value above, but
+//! // now we are asking it for a Person as output.
+//! let p: Person = serde_json::from_str(data)?;
+//!
+//! // Do things just like with any other Rust data structure.
+//! println!("Please call {} at the number {}", p.name, p.phones[0]);
+//!
+//! Ok(())
+//! }
+//! #
+//! # fn main() {
+//! # typed_example().unwrap();
+//! # }
+//! ```
+//!
+//! This is the same `serde_json::from_str` function as before, but this time we
+//! assign the return value to a variable of type `Person` so Serde will
+//! automatically interpret the input data as a `Person` and produce informative
+//! error messages if the layout does not conform to what a `Person` is expected
+//! to look like.
+//!
+//! Any type that implements Serde's `Deserialize` trait can be deserialized
+//! this way. This includes built-in Rust standard library types like `Vec<T>`
+//! and `HashMap<K, V>`, as well as any structs or enums annotated with
+//! `#[derive(Deserialize)]`.
+//!
+//! Once we have `p` of type `Person`, our IDE and the Rust compiler can help us
+//! use it correctly like they do for any other Rust code. The IDE can
+//! autocomplete field names to prevent typos, which was impossible in the
+//! `serde_json::Value` representation. And the Rust compiler can check that
+//! when we write `p.phones[0]`, then `p.phones` is guaranteed to be a
+//! `Vec<String>` so indexing into it makes sense and produces a `String`.
+//!
+//! # Constructing JSON values
+//!
+//! Serde JSON provides a [`json!` macro][macro] to build `serde_json::Value`
+//! objects with very natural JSON syntax.
+//!
+//! ```
+//! use serde_json::json;
+//!
+//! fn main() {
+//! // The type of `john` is `serde_json::Value`
+//! let john = json!({
+//! "name": "John Doe",
+//! "age": 43,
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! });
+//!
+//! println!("first phone number: {}", john["phones"][0]);
+//!
+//! // Convert to a string of JSON and print it out
+//! println!("{}", john.to_string());
+//! }
+//! ```
+//!
+//! The `Value::to_string()` function converts a `serde_json::Value` into a
+//! `String` of JSON text.
+//!
+//! One neat thing about the `json!` macro is that variables and expressions can
+//! be interpolated directly into the JSON value as you are building it. Serde
+//! will check at compile time that the value you are interpolating is able to
+//! be represented as JSON.
+//!
+//! ```
+//! # use serde_json::json;
+//! #
+//! # fn random_phone() -> u16 { 0 }
+//! #
+//! let full_name = "John Doe";
+//! let age_last_year = 42;
+//!
+//! // The type of `john` is `serde_json::Value`
+//! let john = json!({
+//! "name": full_name,
+//! "age": age_last_year + 1,
+//! "phones": [
+//! format!("+44 {}", random_phone())
+//! ]
+//! });
+//! ```
+//!
+//! This is amazingly convenient, but we have the problem we had before with
+//! `Value`: the IDE and Rust compiler cannot help us if we get it wrong. Serde
+//! JSON provides a better way of serializing strongly-typed data structures
+//! into JSON text.
+//!
+//! # Creating JSON by serializing data structures
+//!
+//! A data structure can be converted to a JSON string by
+//! [`serde_json::to_string`][to_string]. There is also
+//! [`serde_json::to_vec`][to_vec] which serializes to a `Vec<u8>` and
+//! [`serde_json::to_writer`][to_writer] which serializes to any `io::Write`
+//! such as a File or a TCP stream.
+//!
+//! ```
+//! use serde::{Deserialize, Serialize};
+//! use serde_json::Result;
+//!
+//! #[derive(Serialize, Deserialize)]
+//! struct Address {
+//! street: String,
+//! city: String,
+//! }
+//!
+//! fn print_an_address() -> Result<()> {
+//! // Some data structure.
+//! let address = Address {
+//! street: "10 Downing Street".to_owned(),
+//! city: "London".to_owned(),
+//! };
+//!
+//! // Serialize it to a JSON string.
+//! let j = serde_json::to_string(&address)?;
+//!
+//! // Print, write to a file, or send to an HTTP server.
+//! println!("{}", j);
+//!
+//! Ok(())
+//! }
+//! #
+//! # fn main() {
+//! # print_an_address().unwrap();
+//! # }
+//! ```
+//!
+//! Any type that implements Serde's `Serialize` trait can be serialized this
+//! way. This includes built-in Rust standard library types like `Vec<T>` and
+//! `HashMap<K, V>`, as well as any structs or enums annotated with
+//! `#[derive(Serialize)]`.
+//!
+//! # No-std support
+//!
+//! As long as there is a memory allocator, it is possible to use serde_json
+//! without the rest of the Rust standard library. Disable the default "std"
+//! feature and enable the "alloc" feature:
+//!
+//! ```toml
+//! [dependencies]
+//! serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
+//! ```
+//!
+//! For JSON support in Serde without a memory allocator, please see the
+//! [`serde-json-core`] crate.
+//!
+//! [value]: https://docs.serde.rs/serde_json/value/enum.Value.html
+//! [from_str]: https://docs.serde.rs/serde_json/de/fn.from_str.html
+//! [from_slice]: https://docs.serde.rs/serde_json/de/fn.from_slice.html
+//! [from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html
+//! [to_string]: https://docs.serde.rs/serde_json/ser/fn.to_string.html
+//! [to_vec]: https://docs.serde.rs/serde_json/ser/fn.to_vec.html
+//! [to_writer]: https://docs.serde.rs/serde_json/ser/fn.to_writer.html
+//! [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.91")]
+// Ignored clippy lints
+#![allow(
+ clippy::collapsible_else_if,
+ clippy::comparison_chain,
+ clippy::deprecated_cfg_attr,
+ clippy::doc_markdown,
+ clippy::excessive_precision,
+ clippy::explicit_auto_deref,
+ clippy::float_cmp,
+ clippy::manual_range_contains,
+ 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
+ clippy::unnested_or_patterns,
+)]
+// 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
+ clippy::cast_possible_truncation,
+ clippy::cast_possible_wrap,
+ clippy::cast_precision_loss,
+ clippy::cast_sign_loss,
+ // correctly used
+ clippy::enum_glob_use,
+ clippy::if_not_else,
+ clippy::integer_division,
+ clippy::map_err_ignore,
+ clippy::match_same_arms,
+ clippy::similar_names,
+ clippy::unused_self,
+ clippy::wildcard_imports,
+ // things are often more readable this way
+ clippy::cast_lossless,
+ clippy::module_name_repetitions,
+ clippy::redundant_else,
+ clippy::shadow_unrelated,
+ clippy::single_match_else,
+ clippy::too_many_lines,
+ clippy::unreadable_literal,
+ clippy::unseparated_literal_suffix,
+ clippy::use_self,
+ clippy::zero_prefixed_literal,
+ // we support older compilers
+ clippy::checked_conversions,
+ clippy::mem_replace_with_default,
+ // noisy
+ clippy::missing_errors_doc,
+ clippy::must_use_candidate,
+)]
+#![allow(non_upper_case_globals)]
+#![deny(missing_docs)]
+#![cfg_attr(not(feature = "std"), no_std)]
+#![cfg_attr(docsrs, feature(doc_cfg))]
+
+extern crate alloc;
+
+#[cfg(feature = "std")]
+#[doc(inline)]
+pub use crate::de::from_reader;
+#[doc(inline)]
+pub use crate::de::{from_slice, from_str, Deserializer, StreamDeserializer};
+#[doc(inline)]
+pub use crate::error::{Error, Result};
+#[doc(inline)]
+pub use crate::ser::{to_string, to_string_pretty, to_vec, to_vec_pretty};
+#[cfg(feature = "std")]
+#[doc(inline)]
+pub use crate::ser::{to_writer, to_writer_pretty, Serializer};
+#[doc(inline)]
+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 $(,)?) => {
+ match $e {
+ core::result::Result::Ok(val) => val,
+ core::result::Result::Err(err) => return core::result::Result::Err(err),
+ }
+ };
+}
+
+#[macro_use]
+mod macros;
+
+pub mod de;
+pub mod error;
+pub mod map;
+#[cfg(feature = "std")]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub mod ser;
+#[cfg(not(feature = "std"))]
+mod ser;
+pub mod value;
+
+mod features_check;
+
+mod io;
+#[cfg(feature = "std")]
+mod iter;
+#[cfg(feature = "float_roundtrip")]
+mod lexical;
+mod number;
+mod read;
+
+#[cfg(feature = "raw_value")]
+mod raw;
diff --git a/third_party/rust/serde_json/src/macros.rs b/third_party/rust/serde_json/src/macros.rs
new file mode 100644
index 0000000000..5287998b46
--- /dev/null
+++ b/third_party/rust/serde_json/src/macros.rs
@@ -0,0 +1,303 @@
+/// Construct a `serde_json::Value` from a JSON literal.
+///
+/// ```
+/// # use serde_json::json;
+/// #
+/// let value = json!({
+/// "code": 200,
+/// "success": true,
+/// "payload": {
+/// "features": [
+/// "serde",
+/// "json"
+/// ]
+/// }
+/// });
+/// ```
+///
+/// Variables or expressions can be interpolated into the JSON literal. Any type
+/// interpolated into an array element or object value must implement Serde's
+/// `Serialize` trait, while any type interpolated into a object key must
+/// implement `Into<String>`. If the `Serialize` implementation of the
+/// interpolated type decides to fail, or if the interpolated type contains a
+/// map with non-string keys, the `json!` macro will panic.
+///
+/// ```
+/// # use serde_json::json;
+/// #
+/// let code = 200;
+/// let features = vec!["serde", "json"];
+///
+/// let value = json!({
+/// "code": code,
+/// "success": code == 200,
+/// "payload": {
+/// features[0]: features[1]
+/// }
+/// });
+/// ```
+///
+/// Trailing commas are allowed inside both arrays and objects.
+///
+/// ```
+/// # use serde_json::json;
+/// #
+/// let value = json!([
+/// "notice",
+/// "the",
+/// "trailing",
+/// "comma -->",
+/// ]);
+/// ```
+#[macro_export(local_inner_macros)]
+macro_rules! json {
+ // Hide distracting implementation details from the generated rustdoc.
+ ($($json:tt)+) => {
+ json_internal!($($json)+)
+ };
+}
+
+// Rocket relies on this because they export their own `json!` with a different
+// doc comment than ours, and various Rust bugs prevent them from calling our
+// `json!` from their `json!` so they call `json_internal!` directly. Check with
+// @SergioBenitez before making breaking changes to this macro.
+//
+// Changes are fine as long as `json_internal!` does not call any new helper
+// macros and can still be invoked as `json_internal!($($json)+)`.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! json_internal {
+ //////////////////////////////////////////////////////////////////////////
+ // TT muncher for parsing the inside of an array [...]. Produces a vec![...]
+ // of the elements.
+ //
+ // Must be invoked as: json_internal!(@array [] $($tt)*)
+ //////////////////////////////////////////////////////////////////////////
+
+ // Done with trailing comma.
+ (@array [$($elems:expr,)*]) => {
+ json_internal_vec![$($elems,)*]
+ };
+
+ // Done without trailing comma.
+ (@array [$($elems:expr),*]) => {
+ json_internal_vec![$($elems),*]
+ };
+
+ // Next element is `null`.
+ (@array [$($elems:expr,)*] null $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!(null)] $($rest)*)
+ };
+
+ // Next element is `true`.
+ (@array [$($elems:expr,)*] true $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!(true)] $($rest)*)
+ };
+
+ // Next element is `false`.
+ (@array [$($elems:expr,)*] false $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!(false)] $($rest)*)
+ };
+
+ // Next element is an array.
+ (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!([$($array)*])] $($rest)*)
+ };
+
+ // Next element is a map.
+ (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!({$($map)*})] $($rest)*)
+ };
+
+ // Next element is an expression followed by comma.
+ (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)* json_internal!($next),] $($rest)*)
+ };
+
+ // Last element is an expression with no trailing comma.
+ (@array [$($elems:expr,)*] $last:expr) => {
+ json_internal!(@array [$($elems,)* json_internal!($last)])
+ };
+
+ // Comma after the most recent element.
+ (@array [$($elems:expr),*] , $($rest:tt)*) => {
+ json_internal!(@array [$($elems,)*] $($rest)*)
+ };
+
+ // Unexpected token after most recent element.
+ (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
+ json_unexpected!($unexpected)
+ };
+
+ //////////////////////////////////////////////////////////////////////////
+ // TT muncher for parsing the inside of an object {...}. Each entry is
+ // inserted into the given map variable.
+ //
+ // Must be invoked as: json_internal!(@object $map () ($($tt)*) ($($tt)*))
+ //
+ // We require two copies of the input tokens so that we can match on one
+ // copy and trigger errors on the other copy.
+ //////////////////////////////////////////////////////////////////////////
+
+ // Done.
+ (@object $object:ident () () ()) => {};
+
+ // Insert the current entry followed by trailing comma.
+ (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
+ let _ = $object.insert(($($key)+).into(), $value);
+ json_internal!(@object $object () ($($rest)*) ($($rest)*));
+ };
+
+ // Current entry followed by unexpected token.
+ (@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
+ json_unexpected!($unexpected);
+ };
+
+ // Insert the last entry without trailing comma.
+ (@object $object:ident [$($key:tt)+] ($value:expr)) => {
+ let _ = $object.insert(($($key)+).into(), $value);
+ };
+
+ // Next value is `null`.
+ (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!(null)) $($rest)*);
+ };
+
+ // Next value is `true`.
+ (@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!(true)) $($rest)*);
+ };
+
+ // Next value is `false`.
+ (@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!(false)) $($rest)*);
+ };
+
+ // Next value is an array.
+ (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!([$($array)*])) $($rest)*);
+ };
+
+ // Next value is a map.
+ (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!({$($map)*})) $($rest)*);
+ };
+
+ // Next value is an expression followed by comma.
+ (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!($value)) , $($rest)*);
+ };
+
+ // Last value is an expression with no trailing comma.
+ (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
+ json_internal!(@object $object [$($key)+] (json_internal!($value)));
+ };
+
+ // Missing value for last entry. Trigger a reasonable error message.
+ (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
+ // "unexpected end of macro invocation"
+ json_internal!();
+ };
+
+ // Missing colon and value for last entry. Trigger a reasonable error
+ // message.
+ (@object $object:ident ($($key:tt)+) () $copy:tt) => {
+ // "unexpected end of macro invocation"
+ json_internal!();
+ };
+
+ // Misplaced colon. Trigger a reasonable error message.
+ (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
+ // Takes no arguments so "no rules expected the token `:`".
+ json_unexpected!($colon);
+ };
+
+ // Found a comma inside a key. Trigger a reasonable error message.
+ (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
+ // Takes no arguments so "no rules expected the token `,`".
+ json_unexpected!($comma);
+ };
+
+ // Key is fully parenthesized. This avoids clippy double_parens false
+ // positives because the parenthesization may be necessary here.
+ (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
+ };
+
+ // Refuse to absorb colon token into key expression.
+ (@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
+ json_expect_expr_comma!($($unexpected)+);
+ };
+
+ // Munch a token into the current key.
+ (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
+ json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
+ };
+
+ //////////////////////////////////////////////////////////////////////////
+ // The main implementation.
+ //
+ // Must be invoked as: json_internal!($($json)+)
+ //////////////////////////////////////////////////////////////////////////
+
+ (null) => {
+ $crate::Value::Null
+ };
+
+ (true) => {
+ $crate::Value::Bool(true)
+ };
+
+ (false) => {
+ $crate::Value::Bool(false)
+ };
+
+ ([]) => {
+ $crate::Value::Array(json_internal_vec![])
+ };
+
+ ([ $($tt:tt)+ ]) => {
+ $crate::Value::Array(json_internal!(@array [] $($tt)+))
+ };
+
+ ({}) => {
+ $crate::Value::Object($crate::Map::new())
+ };
+
+ ({ $($tt:tt)+ }) => {
+ $crate::Value::Object({
+ let mut object = $crate::Map::new();
+ json_internal!(@object object () ($($tt)+) ($($tt)+));
+ object
+ })
+ };
+
+ // Any Serialize type: numbers, strings, struct literals, variables etc.
+ // Must be below every other rule.
+ ($other:expr) => {
+ $crate::to_value(&$other).unwrap()
+ };
+}
+
+// The json_internal macro above cannot invoke vec directly because it uses
+// local_inner_macros. A vec invocation there would resolve to $crate::vec.
+// Instead invoke vec here outside of local_inner_macros.
+#[macro_export]
+#[doc(hidden)]
+macro_rules! json_internal_vec {
+ ($($content:tt)*) => {
+ vec![$($content)*]
+ };
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! json_unexpected {
+ () => {};
+}
+
+#[macro_export]
+#[doc(hidden)]
+macro_rules! json_expect_expr_comma {
+ ($e:expr , $($tt:tt)*) => {};
+}
diff --git a/third_party/rust/serde_json/src/map.rs b/third_party/rust/serde_json/src/map.rs
new file mode 100644
index 0000000000..3e8a3814c9
--- /dev/null
+++ b/third_party/rust/serde_json/src/map.rs
@@ -0,0 +1,940 @@
+//! A map of String to serde_json::Value.
+//!
+//! By default the map is backed by a [`BTreeMap`]. Enable the `preserve_order`
+//! feature of serde_json to use [`IndexMap`] instead.
+//!
+//! [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
+//! [`IndexMap`]: https://docs.rs/indexmap/*/indexmap/map/struct.IndexMap.html
+
+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};
+
+/// Represents a JSON key/value type.
+pub struct Map<K, V> {
+ map: MapImpl<K, V>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type MapImpl<K, V> = BTreeMap<K, V>;
+#[cfg(feature = "preserve_order")]
+type MapImpl<K, V> = IndexMap<K, V>;
+
+impl Map<String, Value> {
+ /// Makes a new empty Map.
+ #[inline]
+ pub fn new() -> Self {
+ Map {
+ map: MapImpl::new(),
+ }
+ }
+
+ /// Makes a new empty Map with the given initial capacity.
+ #[inline]
+ pub fn with_capacity(capacity: usize) -> Self {
+ Map {
+ #[cfg(not(feature = "preserve_order"))]
+ map: {
+ // does not support with_capacity
+ let _ = capacity;
+ BTreeMap::new()
+ },
+ #[cfg(feature = "preserve_order")]
+ map: IndexMap::with_capacity(capacity),
+ }
+ }
+
+ /// Clears the map, removing all values.
+ #[inline]
+ pub fn clear(&mut self) {
+ self.map.clear();
+ }
+
+ /// Returns a reference to the value corresponding to the 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]
+ pub fn get<Q>(&self, key: &Q) -> Option<&Value>
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ self.map.get(key)
+ }
+
+ /// Returns true if the map contains a value for the specified 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]
+ pub fn contains_key<Q>(&self, key: &Q) -> bool
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ self.map.contains_key(key)
+ }
+
+ /// Returns a mutable reference to the value corresponding to the 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]
+ pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ 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.
+ ///
+ /// If the map did have this key present, the value is updated, and the old
+ /// value is returned.
+ #[inline]
+ pub fn insert(&mut self, k: String, v: Value) -> Option<Value> {
+ self.map.insert(k, v)
+ }
+
+ /// Removes a key from the map, returning the value at the key if the key
+ /// was previously in the map.
+ ///
+ /// 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]
+ pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ #[cfg(feature = "preserve_order")]
+ return self.map.swap_remove(key);
+ #[cfg(not(feature = "preserve_order"))]
+ return self.map.remove(key);
+ }
+
+ /// Removes a key from the map, returning the stored key and value if the
+ /// key was previously in the map.
+ ///
+ /// 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.
+ pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
+ where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+ {
+ #[cfg(any(feature = "preserve_order", not(no_btreemap_remove_entry)))]
+ return self.map.remove_entry(key);
+ #[cfg(all(
+ not(feature = "preserve_order"),
+ no_btreemap_remove_entry,
+ not(no_btreemap_get_key_value),
+ ))]
+ {
+ let (key, _value) = self.map.get_key_value(key)?;
+ let key = key.clone();
+ let value = self.map.remove::<String>(&key)?;
+ Some((key, value))
+ }
+ #[cfg(all(
+ not(feature = "preserve_order"),
+ no_btreemap_remove_entry,
+ 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> {
+ fn start_bound(&self) -> Bound<&Q> {
+ Bound::Included(self.0)
+ }
+ fn end_bound(&self) -> Bound<&Q> {
+ Bound::Included(self.0)
+ }
+ }
+
+ let mut range = self.map.range(Key(key));
+ let (key, _value) = range.next()?;
+ let key = key.clone();
+ let value = self.map.remove::<String>(&key)?;
+ Some((key, value))
+ }
+ }
+
+ /// Moves all elements from other into self, leaving other empty.
+ #[inline]
+ pub fn append(&mut self, other: &mut Self) {
+ #[cfg(feature = "preserve_order")]
+ self.map
+ .extend(mem::replace(&mut other.map, MapImpl::default()));
+ #[cfg(not(feature = "preserve_order"))]
+ self.map.append(&mut other.map);
+ }
+
+ /// Gets the given key's corresponding entry in the map for in-place
+ /// manipulation.
+ pub fn entry<S>(&mut self, key: S) -> Entry
+ where
+ S: Into<String>,
+ {
+ #[cfg(not(feature = "preserve_order"))]
+ use alloc::collections::btree_map::Entry as EntryImpl;
+ #[cfg(feature = "preserve_order")]
+ use indexmap::map::Entry as EntryImpl;
+
+ match self.map.entry(key.into()) {
+ EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }),
+ EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }),
+ }
+ }
+
+ /// Returns the number of elements in the map.
+ #[inline]
+ pub fn len(&self) -> usize {
+ self.map.len()
+ }
+
+ /// Returns true if the map contains no elements.
+ #[inline]
+ pub fn is_empty(&self) -> bool {
+ self.map.is_empty()
+ }
+
+ /// Gets an iterator over the entries of the map.
+ #[inline]
+ pub fn iter(&self) -> Iter {
+ Iter {
+ iter: self.map.iter(),
+ }
+ }
+
+ /// Gets a mutable iterator over the entries of the map.
+ #[inline]
+ pub fn iter_mut(&mut self) -> IterMut {
+ IterMut {
+ iter: self.map.iter_mut(),
+ }
+ }
+
+ /// Gets an iterator over the keys of the map.
+ #[inline]
+ pub fn keys(&self) -> Keys {
+ Keys {
+ iter: self.map.keys(),
+ }
+ }
+
+ /// Gets an iterator over the values of the map.
+ #[inline]
+ pub fn values(&self) -> Values {
+ Values {
+ iter: self.map.values(),
+ }
+ }
+
+ /// Gets an iterator over mutable values of the map.
+ #[inline]
+ pub fn values_mut(&mut self) -> ValuesMut {
+ ValuesMut {
+ 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
+impl Default for Map<String, Value> {
+ #[inline]
+ fn default() -> Self {
+ Map {
+ map: MapImpl::new(),
+ }
+ }
+}
+
+impl Clone for Map<String, Value> {
+ #[inline]
+ fn clone(&self) -> Self {
+ Map {
+ map: self.map.clone(),
+ }
+ }
+
+ #[inline]
+ fn clone_from(&mut self, source: &Self) {
+ self.map.clone_from(&source.map);
+ }
+}
+
+impl PartialEq for Map<String, Value> {
+ #[inline]
+ fn eq(&self, other: &Self) -> bool {
+ self.map.eq(&other.map)
+ }
+}
+
+impl Eq for Map<String, Value> {}
+
+/// Access an element of this map. Panics if the given key is not present in the
+/// map.
+///
+/// ```
+/// # use serde_json::Value;
+/// #
+/// # let val = &Value::String("".to_owned());
+/// # let _ =
+/// match val {
+/// Value::String(s) => Some(s.as_str()),
+/// Value::Array(arr) => arr[0].as_str(),
+/// Value::Object(map) => map["type"].as_str(),
+/// _ => None,
+/// }
+/// # ;
+/// ```
+impl<'a, Q> ops::Index<&'a Q> for Map<String, Value>
+where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+{
+ type Output = Value;
+
+ fn index(&self, index: &Q) -> &Value {
+ self.map.index(index)
+ }
+}
+
+/// Mutably access an element of this map. Panics if the given key is not
+/// present in the map.
+///
+/// ```
+/// # use serde_json::json;
+/// #
+/// # let mut map = serde_json::Map::new();
+/// # map.insert("key".to_owned(), serde_json::Value::Null);
+/// #
+/// map["key"] = json!("value");
+/// ```
+impl<'a, Q> ops::IndexMut<&'a Q> for Map<String, Value>
+where
+ String: Borrow<Q>,
+ Q: ?Sized + Ord + Eq + Hash,
+{
+ fn index_mut(&mut self, index: &Q) -> &mut Value {
+ self.map.get_mut(index).expect("no entry found for key")
+ }
+}
+
+impl Debug for Map<String, Value> {
+ #[inline]
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ self.map.fmt(formatter)
+ }
+}
+
+#[cfg(any(feature = "std", feature = "alloc"))]
+impl serde::ser::Serialize for Map<String, Value> {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::ser::Serializer,
+ {
+ use serde::ser::SerializeMap;
+ let mut map = tri!(serializer.serialize_map(Some(self.len())));
+ for (k, v) in self {
+ tri!(map.serialize_entry(k, v));
+ }
+ map.end()
+ }
+}
+
+impl<'de> de::Deserialize<'de> for Map<String, Value> {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ struct Visitor;
+
+ impl<'de> de::Visitor<'de> for Visitor {
+ type Value = Map<String, Value>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a map")
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(Map::new())
+ }
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[inline]
+ fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
+ where
+ V: de::MapAccess<'de>,
+ {
+ let mut values = Map::new();
+
+ while let Some((key, value)) = tri!(visitor.next_entry()) {
+ values.insert(key, value);
+ }
+
+ Ok(values)
+ }
+ }
+
+ deserializer.deserialize_map(Visitor)
+ }
+}
+
+impl FromIterator<(String, Value)> for Map<String, Value> {
+ fn from_iter<T>(iter: T) -> Self
+ where
+ T: IntoIterator<Item = (String, Value)>,
+ {
+ Map {
+ map: FromIterator::from_iter(iter),
+ }
+ }
+}
+
+impl Extend<(String, Value)> for Map<String, Value> {
+ fn extend<T>(&mut self, iter: T)
+ where
+ T: IntoIterator<Item = (String, Value)>,
+ {
+ self.map.extend(iter);
+ }
+}
+
+macro_rules! delegate_iterator {
+ (($name:ident $($generics:tt)*) => $item:ty) => {
+ impl $($generics)* Iterator for $name $($generics)* {
+ type Item = $item;
+ #[inline]
+ fn next(&mut self) -> Option<Self::Item> {
+ self.iter.next()
+ }
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.iter.size_hint()
+ }
+ }
+
+ impl $($generics)* DoubleEndedIterator for $name $($generics)* {
+ #[inline]
+ fn next_back(&mut self) -> Option<Self::Item> {
+ self.iter.next_back()
+ }
+ }
+
+ impl $($generics)* ExactSizeIterator for $name $($generics)* {
+ #[inline]
+ fn len(&self) -> usize {
+ self.iter.len()
+ }
+ }
+
+ impl $($generics)* FusedIterator for $name $($generics)* {}
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// A view into a single entry in a map, which may either be vacant or occupied.
+/// This enum is constructed from the [`entry`] method on [`Map`].
+///
+/// [`entry`]: struct.Map.html#method.entry
+/// [`Map`]: struct.Map.html
+pub enum Entry<'a> {
+ /// A vacant Entry.
+ Vacant(VacantEntry<'a>),
+ /// An occupied Entry.
+ Occupied(OccupiedEntry<'a>),
+}
+
+/// A vacant Entry. It is part of the [`Entry`] enum.
+///
+/// [`Entry`]: enum.Entry.html
+pub struct VacantEntry<'a> {
+ vacant: VacantEntryImpl<'a>,
+}
+
+/// An occupied Entry. It is part of the [`Entry`] enum.
+///
+/// [`Entry`]: enum.Entry.html
+pub struct OccupiedEntry<'a> {
+ occupied: OccupiedEntryImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type VacantEntryImpl<'a> = btree_map::VacantEntry<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type VacantEntryImpl<'a> = indexmap::map::VacantEntry<'a, String, Value>;
+
+#[cfg(not(feature = "preserve_order"))]
+type OccupiedEntryImpl<'a> = btree_map::OccupiedEntry<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type OccupiedEntryImpl<'a> = indexmap::map::OccupiedEntry<'a, String, Value>;
+
+impl<'a> Entry<'a> {
+ /// Returns a reference to this entry's key.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let mut map = serde_json::Map::new();
+ /// assert_eq!(map.entry("serde").key(), &"serde");
+ /// ```
+ pub fn key(&self) -> &String {
+ match self {
+ Entry::Vacant(e) => e.key(),
+ Entry::Occupied(e) => e.key(),
+ }
+ }
+
+ /// Ensures a value is in the entry by inserting the default if empty, and
+ /// returns a mutable reference to the value in the entry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut map = serde_json::Map::new();
+ /// map.entry("serde").or_insert(json!(12));
+ ///
+ /// assert_eq!(map["serde"], 12);
+ /// ```
+ pub fn or_insert(self, default: Value) -> &'a mut Value {
+ match self {
+ Entry::Vacant(entry) => entry.insert(default),
+ Entry::Occupied(entry) => entry.into_mut(),
+ }
+ }
+
+ /// Ensures a value is in the entry by inserting the result of the default
+ /// function if empty, and returns a mutable reference to the value in the
+ /// entry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut map = serde_json::Map::new();
+ /// map.entry("serde").or_insert_with(|| json!("hoho"));
+ ///
+ /// assert_eq!(map["serde"], "hoho".to_owned());
+ /// ```
+ pub fn or_insert_with<F>(self, default: F) -> &'a mut Value
+ where
+ F: FnOnce() -> Value,
+ {
+ match self {
+ Entry::Vacant(entry) => entry.insert(default()),
+ Entry::Occupied(entry) => entry.into_mut(),
+ }
+ }
+
+ /// Provides in-place mutable access to an occupied entry before any
+ /// potential inserts into the map.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut map = serde_json::Map::new();
+ /// map.entry("serde")
+ /// .and_modify(|e| *e = json!("rust"))
+ /// .or_insert(json!("cpp"));
+ ///
+ /// assert_eq!(map["serde"], "cpp");
+ ///
+ /// map.entry("serde")
+ /// .and_modify(|e| *e = json!("rust"))
+ /// .or_insert(json!("cpp"));
+ ///
+ /// assert_eq!(map["serde"], "rust");
+ /// ```
+ pub fn and_modify<F>(self, f: F) -> Self
+ where
+ F: FnOnce(&mut Value),
+ {
+ match self {
+ Entry::Occupied(mut entry) => {
+ f(entry.get_mut());
+ Entry::Occupied(entry)
+ }
+ Entry::Vacant(entry) => Entry::Vacant(entry),
+ }
+ }
+}
+
+impl<'a> VacantEntry<'a> {
+ /// Gets a reference to the key that would be used when inserting a value
+ /// through the VacantEntry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Vacant(vacant) => {
+ /// assert_eq!(vacant.key(), &"serde");
+ /// }
+ /// Entry::Occupied(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn key(&self) -> &String {
+ self.vacant.key()
+ }
+
+ /// Sets the value of the entry with the VacantEntry's key, and returns a
+ /// mutable reference to it.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Vacant(vacant) => {
+ /// vacant.insert(json!("hoho"));
+ /// }
+ /// Entry::Occupied(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn insert(self, value: Value) -> &'a mut Value {
+ self.vacant.insert(value)
+ }
+}
+
+impl<'a> OccupiedEntry<'a> {
+ /// Gets a reference to the key in the entry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!(12));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(occupied) => {
+ /// assert_eq!(occupied.key(), &"serde");
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn key(&self) -> &String {
+ self.occupied.key()
+ }
+
+ /// Gets a reference to the value in the entry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!(12));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(occupied) => {
+ /// assert_eq!(occupied.get(), 12);
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn get(&self) -> &Value {
+ self.occupied.get()
+ }
+
+ /// Gets a mutable reference to the value in the entry.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!([1, 2, 3]));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(mut occupied) => {
+ /// occupied.get_mut().as_array_mut().unwrap().push(json!(4));
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ ///
+ /// assert_eq!(map["serde"].as_array().unwrap().len(), 4);
+ /// ```
+ #[inline]
+ pub fn get_mut(&mut self) -> &mut Value {
+ self.occupied.get_mut()
+ }
+
+ /// Converts the entry into a mutable reference to its value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!([1, 2, 3]));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(mut occupied) => {
+ /// occupied.into_mut().as_array_mut().unwrap().push(json!(4));
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ ///
+ /// assert_eq!(map["serde"].as_array().unwrap().len(), 4);
+ /// ```
+ #[inline]
+ pub fn into_mut(self) -> &'a mut Value {
+ self.occupied.into_mut()
+ }
+
+ /// Sets the value of the entry with the `OccupiedEntry`'s key, and returns
+ /// the entry's old value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!(12));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(mut occupied) => {
+ /// assert_eq!(occupied.insert(json!(13)), 12);
+ /// assert_eq!(occupied.get(), 13);
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn insert(&mut self, value: Value) -> Value {
+ self.occupied.insert(value)
+ }
+
+ /// Takes the value of the entry out of the map, and returns it.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// use serde_json::map::Entry;
+ ///
+ /// let mut map = serde_json::Map::new();
+ /// map.insert("serde".to_owned(), json!(12));
+ ///
+ /// match map.entry("serde") {
+ /// Entry::Occupied(occupied) => {
+ /// assert_eq!(occupied.remove(), 12);
+ /// }
+ /// Entry::Vacant(_) => unimplemented!(),
+ /// }
+ /// ```
+ #[inline]
+ pub fn remove(self) -> Value {
+ #[cfg(feature = "preserve_order")]
+ return self.occupied.swap_remove();
+ #[cfg(not(feature = "preserve_order"))]
+ return self.occupied.remove();
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl<'a> IntoIterator for &'a Map<String, Value> {
+ type Item = (&'a String, &'a Value);
+ type IntoIter = Iter<'a>;
+ #[inline]
+ fn into_iter(self) -> Self::IntoIter {
+ Iter {
+ iter: self.map.iter(),
+ }
+ }
+}
+
+/// An iterator over a serde_json::Map's entries.
+pub struct Iter<'a> {
+ iter: IterImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type IterImpl<'a> = btree_map::Iter<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type IterImpl<'a> = indexmap::map::Iter<'a, String, Value>;
+
+delegate_iterator!((Iter<'a>) => (&'a String, &'a Value));
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl<'a> IntoIterator for &'a mut Map<String, Value> {
+ type Item = (&'a String, &'a mut Value);
+ type IntoIter = IterMut<'a>;
+ #[inline]
+ fn into_iter(self) -> Self::IntoIter {
+ IterMut {
+ iter: self.map.iter_mut(),
+ }
+ }
+}
+
+/// A mutable iterator over a serde_json::Map's entries.
+pub struct IterMut<'a> {
+ iter: IterMutImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type IterMutImpl<'a> = btree_map::IterMut<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type IterMutImpl<'a> = indexmap::map::IterMut<'a, String, Value>;
+
+delegate_iterator!((IterMut<'a>) => (&'a String, &'a mut Value));
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl IntoIterator for Map<String, Value> {
+ type Item = (String, Value);
+ type IntoIter = IntoIter;
+ #[inline]
+ fn into_iter(self) -> Self::IntoIter {
+ IntoIter {
+ iter: self.map.into_iter(),
+ }
+ }
+}
+
+/// An owning iterator over a serde_json::Map's entries.
+pub struct IntoIter {
+ iter: IntoIterImpl,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type IntoIterImpl = btree_map::IntoIter<String, Value>;
+#[cfg(feature = "preserve_order")]
+type IntoIterImpl = indexmap::map::IntoIter<String, Value>;
+
+delegate_iterator!((IntoIter) => (String, Value));
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// An iterator over a serde_json::Map's keys.
+pub struct Keys<'a> {
+ iter: KeysImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type KeysImpl<'a> = btree_map::Keys<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type KeysImpl<'a> = indexmap::map::Keys<'a, String, Value>;
+
+delegate_iterator!((Keys<'a>) => &'a String);
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// An iterator over a serde_json::Map's values.
+pub struct Values<'a> {
+ iter: ValuesImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type ValuesImpl<'a> = btree_map::Values<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type ValuesImpl<'a> = indexmap::map::Values<'a, String, Value>;
+
+delegate_iterator!((Values<'a>) => &'a Value);
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// A mutable iterator over a serde_json::Map's values.
+pub struct ValuesMut<'a> {
+ iter: ValuesMutImpl<'a>,
+}
+
+#[cfg(not(feature = "preserve_order"))]
+type ValuesMutImpl<'a> = btree_map::ValuesMut<'a, String, Value>;
+#[cfg(feature = "preserve_order")]
+type ValuesMutImpl<'a> = indexmap::map::ValuesMut<'a, String, Value>;
+
+delegate_iterator!((ValuesMut<'a>) => &'a mut Value);
diff --git a/third_party/rust/serde_json/src/number.rs b/third_party/rust/serde_json/src/number.rs
new file mode 100644
index 0000000000..21a76411cc
--- /dev/null
+++ b/third_party/rust/serde_json/src/number.rs
@@ -0,0 +1,745 @@
+use crate::de::ParserNumber;
+use crate::error::Error;
+#[cfg(feature = "arbitrary_precision")]
+use crate::error::ErrorCode;
+#[cfg(feature = "arbitrary_precision")]
+use alloc::borrow::ToOwned;
+#[cfg(feature = "arbitrary_precision")]
+use alloc::string::{String, ToString};
+use core::fmt::{self, Debug, Display};
+#[cfg(not(feature = "arbitrary_precision"))]
+use core::hash::{Hash, Hasher};
+use serde::de::{self, Unexpected, Visitor};
+#[cfg(feature = "arbitrary_precision")]
+use serde::de::{IntoDeserializer, MapAccess};
+use serde::{forward_to_deserialize_any, Deserialize, Deserializer, Serialize, Serializer};
+
+#[cfg(feature = "arbitrary_precision")]
+pub(crate) const TOKEN: &str = "$serde_json::private::Number";
+
+/// Represents a JSON number, whether integer or floating point.
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct Number {
+ n: N,
+}
+
+#[cfg(not(feature = "arbitrary_precision"))]
+#[derive(Copy, Clone)]
+enum N {
+ PosInt(u64),
+ /// Always less than zero.
+ NegInt(i64),
+ /// Always finite.
+ 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;
+
+impl Number {
+ /// Returns true if the `Number` is an integer between `i64::MIN` and
+ /// `i64::MAX`.
+ ///
+ /// For any Number on which `is_i64` returns true, `as_i64` is guaranteed to
+ /// return the integer value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let big = i64::max_value() as u64 + 10;
+ /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
+ ///
+ /// assert!(v["a"].is_i64());
+ ///
+ /// // Greater than i64::MAX.
+ /// assert!(!v["b"].is_i64());
+ ///
+ /// // Numbers with a decimal point are not considered integers.
+ /// assert!(!v["c"].is_i64());
+ /// ```
+ #[inline]
+ pub fn is_i64(&self) -> bool {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::PosInt(v) => v <= i64::max_value() as u64,
+ N::NegInt(_) => true,
+ N::Float(_) => false,
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ self.as_i64().is_some()
+ }
+
+ /// Returns true if the `Number` is an integer between zero and `u64::MAX`.
+ ///
+ /// For any Number on which `is_u64` returns true, `as_u64` is guaranteed to
+ /// return the integer value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
+ ///
+ /// assert!(v["a"].is_u64());
+ ///
+ /// // Negative integer.
+ /// assert!(!v["b"].is_u64());
+ ///
+ /// // Numbers with a decimal point are not considered integers.
+ /// assert!(!v["c"].is_u64());
+ /// ```
+ #[inline]
+ pub fn is_u64(&self) -> bool {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::PosInt(_) => true,
+ N::NegInt(_) | N::Float(_) => false,
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ self.as_u64().is_some()
+ }
+
+ /// Returns true if the `Number` can be represented by f64.
+ ///
+ /// For any Number on which `is_f64` returns true, `as_f64` is guaranteed to
+ /// return the floating point value.
+ ///
+ /// Currently this function returns true if and only if both `is_i64` and
+ /// `is_u64` return false but this is not a guarantee in the future.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
+ ///
+ /// assert!(v["a"].is_f64());
+ ///
+ /// // Integers.
+ /// assert!(!v["b"].is_f64());
+ /// assert!(!v["c"].is_f64());
+ /// ```
+ #[inline]
+ pub fn is_f64(&self) -> bool {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::Float(_) => true,
+ N::PosInt(_) | N::NegInt(_) => false,
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ for c in self.n.chars() {
+ if c == '.' || c == 'e' || c == 'E' {
+ return self.n.parse::<f64>().ok().map_or(false, f64::is_finite);
+ }
+ }
+ false
+ }
+ }
+
+ /// If the `Number` is an integer, represent it as i64 if possible. Returns
+ /// None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let big = i64::max_value() as u64 + 10;
+ /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
+ ///
+ /// assert_eq!(v["a"].as_i64(), Some(64));
+ /// assert_eq!(v["b"].as_i64(), None);
+ /// assert_eq!(v["c"].as_i64(), None);
+ /// ```
+ #[inline]
+ pub fn as_i64(&self) -> Option<i64> {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::PosInt(n) => {
+ if n <= i64::max_value() as u64 {
+ Some(n as i64)
+ } else {
+ None
+ }
+ }
+ N::NegInt(n) => Some(n),
+ N::Float(_) => None,
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ self.n.parse().ok()
+ }
+
+ /// If the `Number` is an integer, represent it as u64 if possible. Returns
+ /// None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
+ ///
+ /// assert_eq!(v["a"].as_u64(), Some(64));
+ /// assert_eq!(v["b"].as_u64(), None);
+ /// assert_eq!(v["c"].as_u64(), None);
+ /// ```
+ #[inline]
+ pub fn as_u64(&self) -> Option<u64> {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::PosInt(n) => Some(n),
+ N::NegInt(_) | N::Float(_) => None,
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ self.n.parse().ok()
+ }
+
+ /// Represents the number as f64 if possible. Returns None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
+ ///
+ /// assert_eq!(v["a"].as_f64(), Some(256.0));
+ /// assert_eq!(v["b"].as_f64(), Some(64.0));
+ /// assert_eq!(v["c"].as_f64(), Some(-64.0));
+ /// ```
+ #[inline]
+ pub fn as_f64(&self) -> Option<f64> {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ match self.n {
+ N::PosInt(n) => Some(n as f64),
+ N::NegInt(n) => Some(n as f64),
+ N::Float(n) => Some(n),
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ self.n.parse::<f64>().ok().filter(|float| float.is_finite())
+ }
+
+ /// Converts a finite `f64` to a `Number`. Infinite or NaN values are not JSON
+ /// numbers.
+ ///
+ /// ```
+ /// # use std::f64;
+ /// #
+ /// # use serde_json::Number;
+ /// #
+ /// assert!(Number::from_f64(256.0).is_some());
+ ///
+ /// assert!(Number::from_f64(f64::NAN).is_none());
+ /// ```
+ #[inline]
+ pub fn from_f64(f: f64) -> Option<Number> {
+ if f.is_finite() {
+ let n = {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ {
+ N::Float(f)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ ryu::Buffer::new().format_finite(f).to_owned()
+ }
+ };
+ Some(Number { n })
+ } else {
+ None
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ /// Not public API. Only tests use this.
+ #[doc(hidden)]
+ #[inline]
+ pub fn from_string_unchecked(n: String) -> Self {
+ Number { n }
+ }
+}
+
+impl Display for Number {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match self.n {
+ N::PosInt(u) => formatter.write_str(itoa::Buffer::new().format(u)),
+ N::NegInt(i) => formatter.write_str(itoa::Buffer::new().format(i)),
+ N::Float(f) => formatter.write_str(ryu::Buffer::new().format_finite(f)),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ Display::fmt(&self.n, formatter)
+ }
+}
+
+impl Debug for Number {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "Number({})", self)
+ }
+}
+
+impl Serialize for Number {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match self.n {
+ N::PosInt(u) => serializer.serialize_u64(u),
+ N::NegInt(i) => serializer.serialize_i64(i),
+ N::Float(f) => serializer.serialize_f64(f),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ use serde::ser::SerializeStruct;
+
+ let mut s = serializer.serialize_struct(TOKEN, 1)?;
+ s.serialize_field(TOKEN, &self.n)?;
+ s.end()
+ }
+}
+
+impl<'de> Deserialize<'de> for Number {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Number, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct NumberVisitor;
+
+ impl<'de> Visitor<'de> for NumberVisitor {
+ type Value = Number;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a JSON number")
+ }
+
+ #[inline]
+ fn visit_i64<E>(self, value: i64) -> Result<Number, E> {
+ Ok(value.into())
+ }
+
+ #[inline]
+ fn visit_u64<E>(self, value: u64) -> Result<Number, E> {
+ Ok(value.into())
+ }
+
+ #[inline]
+ fn visit_f64<E>(self, value: f64) -> Result<Number, E>
+ where
+ E: de::Error,
+ {
+ Number::from_f64(value).ok_or_else(|| de::Error::custom("not a JSON number"))
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ #[inline]
+ fn visit_map<V>(self, mut visitor: V) -> Result<Number, V::Error>
+ where
+ V: de::MapAccess<'de>,
+ {
+ let value = visitor.next_key::<NumberKey>()?;
+ if value.is_none() {
+ return Err(de::Error::invalid_type(Unexpected::Map, &self));
+ }
+ let v: NumberFromString = visitor.next_value()?;
+ Ok(v.value)
+ }
+ }
+
+ deserializer.deserialize_any(NumberVisitor)
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+struct NumberKey;
+
+#[cfg(feature = "arbitrary_precision")]
+impl<'de> de::Deserialize<'de> for NumberKey {
+ fn deserialize<D>(deserializer: D) -> Result<NumberKey, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> de::Visitor<'de> for FieldVisitor {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a valid number field")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<(), E>
+ where
+ E: de::Error,
+ {
+ if s == TOKEN {
+ Ok(())
+ } else {
+ Err(de::Error::custom("expected field with custom name"))
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)?;
+ Ok(NumberKey)
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+pub struct NumberFromString {
+ pub value: Number,
+}
+
+#[cfg(feature = "arbitrary_precision")]
+impl<'de> de::Deserialize<'de> for NumberFromString {
+ fn deserialize<D>(deserializer: D) -> Result<NumberFromString, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ struct Visitor;
+
+ impl<'de> de::Visitor<'de> for Visitor {
+ type Value = NumberFromString;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("string containing a number")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<NumberFromString, E>
+ where
+ E: de::Error,
+ {
+ let n = tri!(s.parse().map_err(de::Error::custom));
+ Ok(NumberFromString { value: n })
+ }
+ }
+
+ deserializer.deserialize_str(Visitor)
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+fn invalid_number() -> Error {
+ Error::syntax(ErrorCode::InvalidNumber, 0, 0)
+}
+
+macro_rules! deserialize_any {
+ (@expand [$($num_string:tt)*]) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ #[inline]
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self.n {
+ N::PosInt(u) => visitor.visit_u64(u),
+ N::NegInt(i) => visitor.visit_i64(i),
+ N::Float(f) => visitor.visit_f64(f),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ #[inline]
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where V: Visitor<'de>
+ {
+ if let Some(u) = self.as_u64() {
+ return visitor.visit_u64(u);
+ } else if let Some(i) = self.as_i64() {
+ return visitor.visit_i64(i);
+ } else if let Some(f) = self.as_f64() {
+ if ryu::Buffer::new().format_finite(f) == self.n || f.to_string() == self.n {
+ return visitor.visit_f64(f);
+ }
+ }
+
+ visitor.visit_map(NumberDeserializer {
+ number: Some(self.$($num_string)*),
+ })
+ }
+ };
+
+ (owned) => {
+ deserialize_any!(@expand [n]);
+ };
+
+ (ref) => {
+ deserialize_any!(@expand [n.clone()]);
+ };
+}
+
+macro_rules! deserialize_number {
+ ($deserialize:ident => $visit:ident) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_any(visitor)
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.$visit(self.n.parse().map_err(|_| invalid_number())?)
+ }
+ };
+}
+
+impl<'de> Deserializer<'de> for Number {
+ type Error = Error;
+
+ deserialize_any!(owned);
+
+ deserialize_number!(deserialize_i8 => visit_i8);
+ deserialize_number!(deserialize_i16 => visit_i16);
+ deserialize_number!(deserialize_i32 => visit_i32);
+ deserialize_number!(deserialize_i64 => visit_i64);
+ deserialize_number!(deserialize_i128 => visit_i128);
+ deserialize_number!(deserialize_u8 => visit_u8);
+ deserialize_number!(deserialize_u16 => visit_u16);
+ deserialize_number!(deserialize_u32 => visit_u32);
+ deserialize_number!(deserialize_u64 => visit_u64);
+ deserialize_number!(deserialize_u128 => visit_u128);
+ deserialize_number!(deserialize_f32 => visit_f32);
+ deserialize_number!(deserialize_f64 => visit_f64);
+
+ forward_to_deserialize_any! {
+ bool char str string bytes byte_buf option unit unit_struct
+ newtype_struct seq tuple tuple_struct map struct enum identifier
+ ignored_any
+ }
+}
+
+impl<'de, 'a> Deserializer<'de> for &'a Number {
+ type Error = Error;
+
+ deserialize_any!(ref);
+
+ deserialize_number!(deserialize_i8 => visit_i8);
+ deserialize_number!(deserialize_i16 => visit_i16);
+ deserialize_number!(deserialize_i32 => visit_i32);
+ deserialize_number!(deserialize_i64 => visit_i64);
+ deserialize_number!(deserialize_i128 => visit_i128);
+ deserialize_number!(deserialize_u8 => visit_u8);
+ deserialize_number!(deserialize_u16 => visit_u16);
+ deserialize_number!(deserialize_u32 => visit_u32);
+ deserialize_number!(deserialize_u64 => visit_u64);
+ deserialize_number!(deserialize_u128 => visit_u128);
+ deserialize_number!(deserialize_f32 => visit_f32);
+ deserialize_number!(deserialize_f64 => visit_f64);
+
+ forward_to_deserialize_any! {
+ bool char str string bytes byte_buf option unit unit_struct
+ newtype_struct seq tuple tuple_struct map struct enum identifier
+ ignored_any
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+pub(crate) struct NumberDeserializer {
+ pub number: Option<String>,
+}
+
+#[cfg(feature = "arbitrary_precision")]
+impl<'de> MapAccess<'de> for NumberDeserializer {
+ type Error = Error;
+
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
+ where
+ K: de::DeserializeSeed<'de>,
+ {
+ if self.number.is_none() {
+ return Ok(None);
+ }
+ seed.deserialize(NumberFieldDeserializer).map(Some)
+ }
+
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self.number.take().unwrap().into_deserializer())
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+struct NumberFieldDeserializer;
+
+#[cfg(feature = "arbitrary_precision")]
+impl<'de> Deserializer<'de> for NumberFieldDeserializer {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_borrowed_str(TOKEN)
+ }
+
+ forward_to_deserialize_any! {
+ bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 char str string seq
+ bytes byte_buf map struct option unit newtype_struct ignored_any
+ unit_struct tuple_struct tuple enum identifier
+ }
+}
+
+impl From<ParserNumber> for Number {
+ fn from(value: ParserNumber) -> Self {
+ let n = match value {
+ ParserNumber::F64(f) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ {
+ N::Float(f)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ f.to_string()
+ }
+ }
+ ParserNumber::U64(u) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ {
+ N::PosInt(u)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ u.to_string()
+ }
+ }
+ ParserNumber::I64(i) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ {
+ N::NegInt(i)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ i.to_string()
+ }
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ ParserNumber::String(s) => s,
+ };
+ Number { n }
+ }
+}
+
+macro_rules! impl_from_unsigned {
+ (
+ $($ty:ty),*
+ ) => {
+ $(
+ impl From<$ty> for Number {
+ #[inline]
+ fn from(u: $ty) -> Self {
+ let n = {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ { N::PosInt(u as u64) }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ itoa::Buffer::new().format(u).to_owned()
+ }
+ };
+ Number { n }
+ }
+ }
+ )*
+ };
+}
+
+macro_rules! impl_from_signed {
+ (
+ $($ty:ty),*
+ ) => {
+ $(
+ impl From<$ty> for Number {
+ #[inline]
+ fn from(i: $ty) -> Self {
+ let n = {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ {
+ if i < 0 {
+ N::NegInt(i as i64)
+ } else {
+ N::PosInt(i as u64)
+ }
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ {
+ itoa::Buffer::new().format(i).to_owned()
+ }
+ };
+ Number { n }
+ }
+ }
+ )*
+ };
+}
+
+impl_from_unsigned!(u8, u16, u32, u64, usize);
+impl_from_signed!(i8, i16, i32, i64, isize);
+
+#[cfg(feature = "arbitrary_precision")]
+impl_from_unsigned!(u128);
+#[cfg(feature = "arbitrary_precision")]
+impl_from_signed!(i128);
+
+impl Number {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ #[cold]
+ pub(crate) fn unexpected(&self) -> Unexpected {
+ match self.n {
+ N::PosInt(u) => Unexpected::Unsigned(u),
+ N::NegInt(i) => Unexpected::Signed(i),
+ N::Float(f) => Unexpected::Float(f),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ #[cold]
+ pub(crate) fn unexpected(&self) -> Unexpected {
+ Unexpected::Other("number")
+ }
+}
diff --git a/third_party/rust/serde_json/src/raw.rs b/third_party/rust/serde_json/src/raw.rs
new file mode 100644
index 0000000000..c8377ac82c
--- /dev/null
+++ b/third_party/rust/serde_json/src/raw.rs
@@ -0,0 +1,531 @@
+use crate::error::Error;
+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,
+ Visitor,
+};
+use serde::forward_to_deserialize_any;
+use serde::ser::{Serialize, SerializeStruct, Serializer};
+
+/// Reference to a range of bytes encompassing a single valid JSON value in the
+/// input data.
+///
+/// A `RawValue` can be used to defer parsing parts of a payload until later,
+/// or to avoid parsing it at all in the case that part of the payload just
+/// needs to be transferred verbatim into a different output object.
+///
+/// When serializing, a value of this type will retain its original formatting
+/// and will not be minified or pretty-printed.
+///
+/// # Note
+///
+/// `RawValue` is only available if serde\_json is built with the `"raw_value"`
+/// feature.
+///
+/// ```toml
+/// [dependencies]
+/// serde_json = { version = "1.0", features = ["raw_value"] }
+/// ```
+///
+/// # Example
+///
+/// ```
+/// use serde::{Deserialize, Serialize};
+/// use serde_json::{Result, value::RawValue};
+///
+/// #[derive(Deserialize)]
+/// struct Input<'a> {
+/// code: u32,
+/// #[serde(borrow)]
+/// payload: &'a RawValue,
+/// }
+///
+/// #[derive(Serialize)]
+/// struct Output<'a> {
+/// info: (u32, &'a RawValue),
+/// }
+///
+/// // Efficiently rearrange JSON input containing separate "code" and "payload"
+/// // keys into a single "info" key holding an array of code and payload.
+/// //
+/// // This could be done equivalently using serde_json::Value as the type for
+/// // payload, but &RawValue will perform better because it does not require
+/// // memory allocation. The correct range of bytes is borrowed from the input
+/// // data and pasted verbatim into the output.
+/// fn rearrange(input: &str) -> Result<String> {
+/// let input: Input = serde_json::from_str(input)?;
+///
+/// let output = Output {
+/// info: (input.code, input.payload),
+/// };
+///
+/// serde_json::to_string(&output)
+/// }
+///
+/// fn main() -> Result<()> {
+/// let out = rearrange(r#" {"code": 200, "payload": {}} "#)?;
+///
+/// assert_eq!(out, r#"{"info":[200,{}]}"#);
+///
+/// Ok(())
+/// }
+/// ```
+///
+/// # Ownership
+///
+/// The typical usage of `RawValue` will be in the borrowed form:
+///
+/// ```
+/// # use serde::Deserialize;
+/// # use serde_json::value::RawValue;
+/// #
+/// #[derive(Deserialize)]
+/// struct SomeStruct<'a> {
+/// #[serde(borrow)]
+/// raw_value: &'a RawValue,
+/// }
+/// ```
+///
+/// The borrowed form is suitable when deserializing through
+/// [`serde_json::from_str`] and [`serde_json::from_slice`] which support
+/// borrowing from the input data without memory allocation.
+///
+/// When deserializing through [`serde_json::from_reader`] you will need to use
+/// the boxed form of `RawValue` instead. This is almost as efficient but
+/// involves buffering the raw value from the I/O stream into memory.
+///
+/// [`serde_json::from_str`]: ../fn.from_str.html
+/// [`serde_json::from_slice`]: ../fn.from_slice.html
+/// [`serde_json::from_reader`]: ../fn.from_reader.html
+///
+/// ```
+/// # use serde::Deserialize;
+/// # use serde_json::value::RawValue;
+/// #
+/// #[derive(Deserialize)]
+/// struct SomeStruct {
+/// raw_value: Box<RawValue>,
+/// }
+/// ```
+#[repr(C)]
+#[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))]
+pub struct RawValue {
+ json: str,
+}
+
+impl RawValue {
+ fn from_borrowed(json: &str) -> &Self {
+ unsafe { mem::transmute::<&str, &RawValue>(json) }
+ }
+
+ 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> {
+ fn clone(&self) -> Self {
+ (**self).to_owned()
+ }
+}
+
+impl ToOwned for RawValue {
+ type Owned = Box<RawValue>;
+
+ fn to_owned(&self) -> Self::Owned {
+ RawValue::from_owned(self.json.to_owned().into_boxed_str())
+ }
+}
+
+impl Default for Box<RawValue> {
+ fn default() -> Self {
+ RawValue::from_borrowed("null").to_owned()
+ }
+}
+
+impl Debug for RawValue {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter
+ .debug_tuple("RawValue")
+ .field(&format_args!("{}", &self.json))
+ .finish()
+ }
+}
+
+impl Display for RawValue {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str(&self.json)
+ }
+}
+
+impl RawValue {
+ /// Convert an owned `String` of JSON data to an owned `RawValue`.
+ ///
+ /// This function is equivalent to `serde_json::from_str::<Box<RawValue>>`
+ /// except that we avoid an allocation and memcpy if both of the following
+ /// are true:
+ ///
+ /// - the input has no leading or trailing whitespace, and
+ /// - the input has capacity equal to its length.
+ pub fn from_string(json: String) -> Result<Box<Self>, Error> {
+ {
+ let borrowed = crate::from_str::<&Self>(&json)?;
+ if borrowed.json.len() < json.len() {
+ return Ok(borrowed.to_owned());
+ }
+ }
+ Ok(Self::from_owned(json.into_boxed_str()))
+ }
+
+ /// Access the JSON text underlying a raw value.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use serde::Deserialize;
+ /// use serde_json::{Result, value::RawValue};
+ ///
+ /// #[derive(Deserialize)]
+ /// struct Response<'a> {
+ /// code: u32,
+ /// #[serde(borrow)]
+ /// payload: &'a RawValue,
+ /// }
+ ///
+ /// fn process(input: &str) -> Result<()> {
+ /// let response: Response = serde_json::from_str(input)?;
+ ///
+ /// let payload = response.payload.get();
+ /// if payload.starts_with('{') {
+ /// // handle a payload which is a JSON map
+ /// } else {
+ /// // handle any other type
+ /// }
+ ///
+ /// Ok(())
+ /// }
+ ///
+ /// fn main() -> Result<()> {
+ /// process(r#" {"code": 200, "payload": {}} "#)?;
+ /// Ok(())
+ /// }
+ /// ```
+ pub fn get(&self) -> &str {
+ &self.json
+ }
+}
+
+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
+///
+/// ```
+/// // Upstream crate
+/// # #[derive(Serialize)]
+/// pub struct Thing {
+/// foo: String,
+/// bar: Option<String>,
+/// extra_data: Box<RawValue>,
+/// }
+///
+/// // Local crate
+/// use serde::Serialize;
+/// use serde_json::value::{to_raw_value, RawValue};
+///
+/// #[derive(Serialize)]
+/// struct MyExtraData {
+/// a: u32,
+/// b: u32,
+/// }
+///
+/// let my_thing = Thing {
+/// foo: "FooVal".into(),
+/// bar: None,
+/// extra_data: to_raw_value(&MyExtraData { a: 1, b: 2 }).unwrap(),
+/// };
+/// # assert_eq!(
+/// # serde_json::to_value(my_thing).unwrap(),
+/// # serde_json::json!({
+/// # "foo": "FooVal",
+/// # "bar": null,
+/// # "extra_data": { "a": 1, "b": 2 }
+/// # })
+/// # );
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+///
+/// ```
+/// use std::collections::BTreeMap;
+///
+/// // The keys in this map are vectors, not strings.
+/// let mut map = BTreeMap::new();
+/// map.insert(vec![32, 64], "x86");
+///
+/// println!("{}", serde_json::value::to_raw_value(&map).unwrap_err());
+/// ```
+#[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))]
+pub fn to_raw_value<T>(value: &T) -> Result<Box<RawValue>, Error>
+where
+ T: ?Sized + Serialize,
+{
+ let json_string = crate::to_string(value)?;
+ Ok(RawValue::from_owned(json_string.into_boxed_str()))
+}
+
+pub const TOKEN: &str = "$serde_json::private::RawValue";
+
+impl Serialize for RawValue {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let mut s = serializer.serialize_struct(TOKEN, 1)?;
+ s.serialize_field(TOKEN, &self.json)?;
+ s.end()
+ }
+}
+
+impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct ReferenceVisitor;
+
+ impl<'de> Visitor<'de> for ReferenceVisitor {
+ type Value = &'de RawValue;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "any valid JSON value")
+ }
+
+ fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
+ where
+ V: MapAccess<'de>,
+ {
+ let value = visitor.next_key::<RawKey>()?;
+ if value.is_none() {
+ return Err(de::Error::invalid_type(Unexpected::Map, &self));
+ }
+ visitor.next_value_seed(ReferenceFromString)
+ }
+ }
+
+ deserializer.deserialize_newtype_struct(TOKEN, ReferenceVisitor)
+ }
+}
+
+impl<'de> Deserialize<'de> for Box<RawValue> {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct BoxedVisitor;
+
+ impl<'de> Visitor<'de> for BoxedVisitor {
+ type Value = Box<RawValue>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "any valid JSON value")
+ }
+
+ fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
+ where
+ V: MapAccess<'de>,
+ {
+ let value = visitor.next_key::<RawKey>()?;
+ if value.is_none() {
+ return Err(de::Error::invalid_type(Unexpected::Map, &self));
+ }
+ visitor.next_value_seed(BoxedFromString)
+ }
+ }
+
+ deserializer.deserialize_newtype_struct(TOKEN, BoxedVisitor)
+ }
+}
+
+struct RawKey;
+
+impl<'de> Deserialize<'de> for RawKey {
+ fn deserialize<D>(deserializer: D) -> Result<RawKey, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct FieldVisitor;
+
+ impl<'de> Visitor<'de> for FieldVisitor {
+ type Value = ();
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("raw value")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<(), E>
+ where
+ E: de::Error,
+ {
+ if s == TOKEN {
+ Ok(())
+ } else {
+ Err(de::Error::custom("unexpected raw value"))
+ }
+ }
+ }
+
+ deserializer.deserialize_identifier(FieldVisitor)?;
+ Ok(RawKey)
+ }
+}
+
+pub struct ReferenceFromString;
+
+impl<'de> DeserializeSeed<'de> for ReferenceFromString {
+ type Value = &'de RawValue;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_str(self)
+ }
+}
+
+impl<'de> Visitor<'de> for ReferenceFromString {
+ type Value = &'de RawValue;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("raw value")
+ }
+
+ fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ Ok(RawValue::from_borrowed(s))
+ }
+}
+
+pub struct BoxedFromString;
+
+impl<'de> DeserializeSeed<'de> for BoxedFromString {
+ type Value = Box<RawValue>;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_str(self)
+ }
+}
+
+impl<'de> Visitor<'de> for BoxedFromString {
+ type Value = Box<RawValue>;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("raw value")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ 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,
+ {
+ Ok(RawValue::from_owned(s.into_boxed_str()))
+ }
+}
+
+struct RawKeyDeserializer;
+
+impl<'de> Deserializer<'de> for RawKeyDeserializer {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_borrowed_str(TOKEN)
+ }
+
+ forward_to_deserialize_any! {
+ bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 char str string seq
+ bytes byte_buf map struct option unit newtype_struct ignored_any
+ unit_struct tuple_struct tuple enum identifier
+ }
+}
+
+pub struct OwnedRawDeserializer {
+ pub raw_value: Option<String>,
+}
+
+impl<'de> MapAccess<'de> for OwnedRawDeserializer {
+ type Error = Error;
+
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
+ where
+ K: de::DeserializeSeed<'de>,
+ {
+ if self.raw_value.is_none() {
+ return Ok(None);
+ }
+ seed.deserialize(RawKeyDeserializer).map(Some)
+ }
+
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(self.raw_value.take().unwrap().into_deserializer())
+ }
+}
+
+pub struct BorrowedRawDeserializer<'de> {
+ pub raw_value: Option<&'de str>,
+}
+
+impl<'de> MapAccess<'de> for BorrowedRawDeserializer<'de> {
+ type Error = Error;
+
+ fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
+ where
+ K: de::DeserializeSeed<'de>,
+ {
+ if self.raw_value.is_none() {
+ return Ok(None);
+ }
+ seed.deserialize(RawKeyDeserializer).map(Some)
+ }
+
+ fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
+ where
+ V: de::DeserializeSeed<'de>,
+ {
+ seed.deserialize(BorrowedStrDeserializer::new(self.raw_value.take().unwrap()))
+ }
+}
diff --git a/third_party/rust/serde_json/src/read.rs b/third_party/rust/serde_json/src/read.rs
new file mode 100644
index 0000000000..fc3a3ca748
--- /dev/null
+++ b/third_party/rust/serde_json/src/read.rs
@@ -0,0 +1,1004 @@
+use crate::error::{Error, ErrorCode, Result};
+use alloc::vec::Vec;
+use core::char;
+use core::cmp;
+use core::ops::Deref;
+use core::str;
+
+#[cfg(feature = "std")]
+use crate::io;
+#[cfg(feature = "std")]
+use crate::iter::LineColIterator;
+
+#[cfg(feature = "raw_value")]
+use crate::raw::BorrowedRawDeserializer;
+#[cfg(all(feature = "raw_value", feature = "std"))]
+use crate::raw::OwnedRawDeserializer;
+#[cfg(feature = "raw_value")]
+use serde::de::Visitor;
+
+/// Trait used by the deserializer for iterating over input. This is manually
+/// "specialized" for iterating over &[u8]. Once feature(specialization) is
+/// stable we can use actual specialization.
+///
+/// This trait is sealed and cannot be implemented for types outside of
+/// `serde_json`.
+pub trait Read<'de>: private::Sealed {
+ #[doc(hidden)]
+ fn next(&mut self) -> Result<Option<u8>>;
+ #[doc(hidden)]
+ fn peek(&mut self) -> Result<Option<u8>>;
+
+ /// Only valid after a call to peek(). Discards the peeked byte.
+ #[doc(hidden)]
+ fn discard(&mut self);
+
+ /// Position of the most recent call to next().
+ ///
+ /// The most recent call was probably next() and not peek(), but this method
+ /// should try to return a sensible result if the most recent call was
+ /// actually peek() because we don't always know.
+ ///
+ /// Only called in case of an error, so performance is not important.
+ #[doc(hidden)]
+ fn position(&self) -> Position;
+
+ /// Position of the most recent call to peek().
+ ///
+ /// The most recent call was probably peek() and not next(), but this method
+ /// should try to return a sensible result if the most recent call was
+ /// actually next() because we don't always know.
+ ///
+ /// Only called in case of an error, so performance is not important.
+ #[doc(hidden)]
+ fn peek_position(&self) -> Position;
+
+ /// Offset from the beginning of the input to the next byte that would be
+ /// returned by next() or peek().
+ #[doc(hidden)]
+ fn byte_offset(&self) -> usize;
+
+ /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
+ /// string until the next quotation mark using the given scratch space if
+ /// necessary. The scratch space is initially empty.
+ #[doc(hidden)]
+ fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
+
+ /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
+ /// string until the next quotation mark using the given scratch space if
+ /// necessary. The scratch space is initially empty.
+ ///
+ /// This function returns the raw bytes in the string with escape sequences
+ /// expanded but without performing unicode validation.
+ #[doc(hidden)]
+ fn parse_str_raw<'s>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ ) -> Result<Reference<'de, 's, [u8]>>;
+
+ /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
+ /// string until the next quotation mark but discards the data.
+ #[doc(hidden)]
+ fn ignore_str(&mut self) -> Result<()>;
+
+ /// Assumes the previous byte was a hex escape sequnce ('\u') in a string.
+ /// Parses next hexadecimal sequence.
+ #[doc(hidden)]
+ fn decode_hex_escape(&mut self) -> Result<u16>;
+
+ /// Switch raw buffering mode on.
+ ///
+ /// This is used when deserializing `RawValue`.
+ #[cfg(feature = "raw_value")]
+ #[doc(hidden)]
+ fn begin_raw_buffering(&mut self);
+
+ /// Switch raw buffering mode off and provides the raw buffered data to the
+ /// given visitor.
+ #[cfg(feature = "raw_value")]
+ #[doc(hidden)]
+ fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>;
+
+ /// Whether StreamDeserializer::next needs to check the failed flag. True
+ /// for IoRead, false for StrRead and SliceRead which can track failure by
+ /// truncating their input slice to avoid the extra check on every next
+ /// call.
+ #[doc(hidden)]
+ const should_early_return_if_failed: bool;
+
+ /// Mark a persistent failure of StreamDeserializer, either by setting the
+ /// flag or by truncating the input data.
+ #[doc(hidden)]
+ fn set_failed(&mut self, failed: &mut bool);
+}
+
+pub struct Position {
+ pub line: usize,
+ pub column: usize,
+}
+
+pub enum Reference<'b, 'c, T>
+where
+ T: ?Sized + 'static,
+{
+ Borrowed(&'b T),
+ Copied(&'c T),
+}
+
+impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
+where
+ T: ?Sized + 'static,
+{
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ match *self {
+ Reference::Borrowed(b) => b,
+ Reference::Copied(c) => c,
+ }
+ }
+}
+
+/// JSON input source that reads from a std::io input stream.
+#[cfg(feature = "std")]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub struct IoRead<R>
+where
+ R: io::Read,
+{
+ iter: LineColIterator<io::Bytes<R>>,
+ /// Temporary storage of peeked byte.
+ ch: Option<u8>,
+ #[cfg(feature = "raw_value")]
+ raw_buffer: Option<Vec<u8>>,
+}
+
+/// JSON input source that reads from a slice of bytes.
+//
+// This is more efficient than other iterators because peek() can be read-only
+// and we can compute line/col position only if an error happens.
+pub struct SliceRead<'a> {
+ slice: &'a [u8],
+ /// Index of the *next* byte that will be returned by next() or peek().
+ index: usize,
+ #[cfg(feature = "raw_value")]
+ raw_buffering_start_index: usize,
+}
+
+/// JSON input source that reads from a UTF-8 string.
+//
+// Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
+pub struct StrRead<'a> {
+ delegate: SliceRead<'a>,
+ #[cfg(feature = "raw_value")]
+ data: &'a str,
+}
+
+// Prevent users from implementing the Read trait.
+mod private {
+ pub trait Sealed {}
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+#[cfg(feature = "std")]
+impl<R> IoRead<R>
+where
+ R: io::Read,
+{
+ /// Create a JSON input source to read from a std::io input stream.
+ pub fn new(reader: R) -> Self {
+ IoRead {
+ iter: LineColIterator::new(reader.bytes()),
+ ch: None,
+ #[cfg(feature = "raw_value")]
+ raw_buffer: None,
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<R> private::Sealed for IoRead<R> where R: io::Read {}
+
+#[cfg(feature = "std")]
+impl<R> IoRead<R>
+where
+ R: io::Read,
+{
+ fn parse_str_bytes<'s, T, F>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ validate: bool,
+ result: F,
+ ) -> Result<T>
+ where
+ T: 's,
+ F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
+ {
+ loop {
+ let ch = tri!(next_or_eof(self));
+ if !ESCAPE[ch as usize] {
+ scratch.push(ch);
+ continue;
+ }
+ match ch {
+ b'"' => {
+ return result(self, scratch);
+ }
+ b'\\' => {
+ tri!(parse_escape(self, validate, scratch));
+ }
+ _ => {
+ if validate {
+ return error(self, ErrorCode::ControlCharacterWhileParsingString);
+ }
+ scratch.push(ch);
+ }
+ }
+ }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<'de, R> Read<'de> for IoRead<R>
+where
+ R: io::Read,
+{
+ #[inline]
+ fn next(&mut self) -> Result<Option<u8>> {
+ match self.ch.take() {
+ Some(ch) => {
+ #[cfg(feature = "raw_value")]
+ {
+ if let Some(buf) = &mut self.raw_buffer {
+ buf.push(ch);
+ }
+ }
+ Ok(Some(ch))
+ }
+ None => match self.iter.next() {
+ Some(Err(err)) => Err(Error::io(err)),
+ Some(Ok(ch)) => {
+ #[cfg(feature = "raw_value")]
+ {
+ if let Some(buf) = &mut self.raw_buffer {
+ buf.push(ch);
+ }
+ }
+ Ok(Some(ch))
+ }
+ None => Ok(None),
+ },
+ }
+ }
+
+ #[inline]
+ fn peek(&mut self) -> Result<Option<u8>> {
+ match self.ch {
+ Some(ch) => Ok(Some(ch)),
+ None => match self.iter.next() {
+ Some(Err(err)) => Err(Error::io(err)),
+ Some(Ok(ch)) => {
+ self.ch = Some(ch);
+ Ok(self.ch)
+ }
+ None => Ok(None),
+ },
+ }
+ }
+
+ #[cfg(not(feature = "raw_value"))]
+ #[inline]
+ fn discard(&mut self) {
+ self.ch = None;
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn discard(&mut self) {
+ if let Some(ch) = self.ch.take() {
+ if let Some(buf) = &mut self.raw_buffer {
+ buf.push(ch);
+ }
+ }
+ }
+
+ fn position(&self) -> Position {
+ Position {
+ line: self.iter.line(),
+ column: self.iter.col(),
+ }
+ }
+
+ fn peek_position(&self) -> Position {
+ // The LineColIterator updates its position during peek() so it has the
+ // right one here.
+ self.position()
+ }
+
+ fn byte_offset(&self) -> usize {
+ match self.ch {
+ Some(_) => self.iter.byte_offset() - 1,
+ None => self.iter.byte_offset(),
+ }
+ }
+
+ fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
+ self.parse_str_bytes(scratch, true, as_str)
+ .map(Reference::Copied)
+ }
+
+ fn parse_str_raw<'s>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ ) -> Result<Reference<'de, 's, [u8]>> {
+ self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
+ .map(Reference::Copied)
+ }
+
+ fn ignore_str(&mut self) -> Result<()> {
+ loop {
+ let ch = tri!(next_or_eof(self));
+ if !ESCAPE[ch as usize] {
+ continue;
+ }
+ match ch {
+ b'"' => {
+ return Ok(());
+ }
+ b'\\' => {
+ tri!(ignore_escape(self));
+ }
+ _ => {
+ return error(self, ErrorCode::ControlCharacterWhileParsingString);
+ }
+ }
+ }
+ }
+
+ fn decode_hex_escape(&mut self) -> Result<u16> {
+ let mut n = 0;
+ for _ in 0..4 {
+ match decode_hex_val(tri!(next_or_eof(self))) {
+ None => return error(self, ErrorCode::InvalidEscape),
+ Some(val) => {
+ n = (n << 4) + val;
+ }
+ }
+ }
+ Ok(n)
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn begin_raw_buffering(&mut self) {
+ self.raw_buffer = Some(Vec::new());
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ let raw = self.raw_buffer.take().unwrap();
+ let raw = match String::from_utf8(raw) {
+ Ok(raw) => raw,
+ Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
+ };
+ visitor.visit_map(OwnedRawDeserializer {
+ raw_value: Some(raw),
+ })
+ }
+
+ const should_early_return_if_failed: bool = true;
+
+ #[inline]
+ #[cold]
+ fn set_failed(&mut self, failed: &mut bool) {
+ *failed = true;
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl<'a> SliceRead<'a> {
+ /// Create a JSON input source to read from a slice of bytes.
+ pub fn new(slice: &'a [u8]) -> Self {
+ SliceRead {
+ slice,
+ index: 0,
+ #[cfg(feature = "raw_value")]
+ raw_buffering_start_index: 0,
+ }
+ }
+
+ fn position_of_index(&self, i: usize) -> Position {
+ let mut position = Position { line: 1, column: 0 };
+ for ch in &self.slice[..i] {
+ match *ch {
+ b'\n' => {
+ position.line += 1;
+ position.column = 0;
+ }
+ _ => {
+ position.column += 1;
+ }
+ }
+ }
+ position
+ }
+
+ /// The big optimization here over IoRead is that if the string contains no
+ /// backslash escape sequences, the returned &str is a slice of the raw JSON
+ /// data so we avoid copying into the scratch space.
+ fn parse_str_bytes<'s, T, F>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ validate: bool,
+ result: F,
+ ) -> Result<Reference<'a, 's, T>>
+ where
+ T: ?Sized + 's,
+ F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
+ {
+ // Index of the first byte not yet copied into the scratch space.
+ let mut start = self.index;
+
+ loop {
+ while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
+ self.index += 1;
+ }
+ if self.index == self.slice.len() {
+ return error(self, ErrorCode::EofWhileParsingString);
+ }
+ match self.slice[self.index] {
+ b'"' => {
+ if scratch.is_empty() {
+ // Fast path: return a slice of the raw JSON without any
+ // copying.
+ let borrowed = &self.slice[start..self.index];
+ self.index += 1;
+ return result(self, borrowed).map(Reference::Borrowed);
+ } else {
+ scratch.extend_from_slice(&self.slice[start..self.index]);
+ self.index += 1;
+ return result(self, scratch).map(Reference::Copied);
+ }
+ }
+ b'\\' => {
+ scratch.extend_from_slice(&self.slice[start..self.index]);
+ self.index += 1;
+ tri!(parse_escape(self, validate, scratch));
+ start = self.index;
+ }
+ _ => {
+ self.index += 1;
+ if validate {
+ return error(self, ErrorCode::ControlCharacterWhileParsingString);
+ }
+ }
+ }
+ }
+ }
+}
+
+impl<'a> private::Sealed for SliceRead<'a> {}
+
+impl<'a> Read<'a> for SliceRead<'a> {
+ #[inline]
+ fn next(&mut self) -> Result<Option<u8>> {
+ // `Ok(self.slice.get(self.index).map(|ch| { self.index += 1; *ch }))`
+ // is about 10% slower.
+ Ok(if self.index < self.slice.len() {
+ let ch = self.slice[self.index];
+ self.index += 1;
+ Some(ch)
+ } else {
+ None
+ })
+ }
+
+ #[inline]
+ fn peek(&mut self) -> Result<Option<u8>> {
+ // `Ok(self.slice.get(self.index).map(|ch| *ch))` is about 10% slower
+ // for some reason.
+ Ok(if self.index < self.slice.len() {
+ Some(self.slice[self.index])
+ } else {
+ None
+ })
+ }
+
+ #[inline]
+ fn discard(&mut self) {
+ self.index += 1;
+ }
+
+ fn position(&self) -> Position {
+ self.position_of_index(self.index)
+ }
+
+ fn peek_position(&self) -> Position {
+ // Cap it at slice.len() just in case the most recent call was next()
+ // and it returned the last byte.
+ self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
+ }
+
+ fn byte_offset(&self) -> usize {
+ self.index
+ }
+
+ fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
+ self.parse_str_bytes(scratch, true, as_str)
+ }
+
+ fn parse_str_raw<'s>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ ) -> Result<Reference<'a, 's, [u8]>> {
+ self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
+ }
+
+ fn ignore_str(&mut self) -> Result<()> {
+ loop {
+ while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
+ self.index += 1;
+ }
+ if self.index == self.slice.len() {
+ return error(self, ErrorCode::EofWhileParsingString);
+ }
+ match self.slice[self.index] {
+ b'"' => {
+ self.index += 1;
+ return Ok(());
+ }
+ b'\\' => {
+ self.index += 1;
+ tri!(ignore_escape(self));
+ }
+ _ => {
+ return error(self, ErrorCode::ControlCharacterWhileParsingString);
+ }
+ }
+ }
+ }
+
+ fn decode_hex_escape(&mut self) -> Result<u16> {
+ if self.index + 4 > self.slice.len() {
+ self.index = self.slice.len();
+ return error(self, ErrorCode::EofWhileParsingString);
+ }
+
+ let mut n = 0;
+ for _ in 0..4 {
+ let ch = decode_hex_val(self.slice[self.index]);
+ self.index += 1;
+ match ch {
+ None => return error(self, ErrorCode::InvalidEscape),
+ Some(val) => {
+ n = (n << 4) + val;
+ }
+ }
+ }
+ Ok(n)
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn begin_raw_buffering(&mut self) {
+ self.raw_buffering_start_index = self.index;
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'a>,
+ {
+ let raw = &self.slice[self.raw_buffering_start_index..self.index];
+ let raw = match str::from_utf8(raw) {
+ Ok(raw) => raw,
+ Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
+ };
+ visitor.visit_map(BorrowedRawDeserializer {
+ raw_value: Some(raw),
+ })
+ }
+
+ const should_early_return_if_failed: bool = false;
+
+ #[inline]
+ #[cold]
+ fn set_failed(&mut self, _failed: &mut bool) {
+ self.slice = &self.slice[..self.index];
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl<'a> StrRead<'a> {
+ /// Create a JSON input source to read from a UTF-8 string.
+ pub fn new(s: &'a str) -> Self {
+ StrRead {
+ delegate: SliceRead::new(s.as_bytes()),
+ #[cfg(feature = "raw_value")]
+ data: s,
+ }
+ }
+}
+
+impl<'a> private::Sealed for StrRead<'a> {}
+
+impl<'a> Read<'a> for StrRead<'a> {
+ #[inline]
+ fn next(&mut self) -> Result<Option<u8>> {
+ self.delegate.next()
+ }
+
+ #[inline]
+ fn peek(&mut self) -> Result<Option<u8>> {
+ self.delegate.peek()
+ }
+
+ #[inline]
+ fn discard(&mut self) {
+ self.delegate.discard();
+ }
+
+ fn position(&self) -> Position {
+ self.delegate.position()
+ }
+
+ fn peek_position(&self) -> Position {
+ self.delegate.peek_position()
+ }
+
+ fn byte_offset(&self) -> usize {
+ self.delegate.byte_offset()
+ }
+
+ 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 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) })
+ })
+ }
+
+ fn parse_str_raw<'s>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ ) -> Result<Reference<'a, 's, [u8]>> {
+ self.delegate.parse_str_raw(scratch)
+ }
+
+ fn ignore_str(&mut self) -> Result<()> {
+ self.delegate.ignore_str()
+ }
+
+ fn decode_hex_escape(&mut self) -> Result<u16> {
+ self.delegate.decode_hex_escape()
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn begin_raw_buffering(&mut self) {
+ self.delegate.begin_raw_buffering();
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'a>,
+ {
+ let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
+ visitor.visit_map(BorrowedRawDeserializer {
+ raw_value: Some(raw),
+ })
+ }
+
+ const should_early_return_if_failed: bool = false;
+
+ #[inline]
+ #[cold]
+ fn set_failed(&mut self, failed: &mut bool) {
+ self.delegate.set_failed(failed);
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+impl<'a, 'de, R> private::Sealed for &'a mut R where R: Read<'de> {}
+
+impl<'a, 'de, R> Read<'de> for &'a mut R
+where
+ R: Read<'de>,
+{
+ fn next(&mut self) -> Result<Option<u8>> {
+ R::next(self)
+ }
+
+ fn peek(&mut self) -> Result<Option<u8>> {
+ R::peek(self)
+ }
+
+ fn discard(&mut self) {
+ R::discard(self);
+ }
+
+ fn position(&self) -> Position {
+ R::position(self)
+ }
+
+ fn peek_position(&self) -> Position {
+ R::peek_position(self)
+ }
+
+ fn byte_offset(&self) -> usize {
+ R::byte_offset(self)
+ }
+
+ fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
+ R::parse_str(self, scratch)
+ }
+
+ fn parse_str_raw<'s>(
+ &'s mut self,
+ scratch: &'s mut Vec<u8>,
+ ) -> Result<Reference<'de, 's, [u8]>> {
+ R::parse_str_raw(self, scratch)
+ }
+
+ fn ignore_str(&mut self) -> Result<()> {
+ R::ignore_str(self)
+ }
+
+ fn decode_hex_escape(&mut self) -> Result<u16> {
+ R::decode_hex_escape(self)
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn begin_raw_buffering(&mut self) {
+ R::begin_raw_buffering(self);
+ }
+
+ #[cfg(feature = "raw_value")]
+ fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ R::end_raw_buffering(self, visitor)
+ }
+
+ const should_early_return_if_failed: bool = R::should_early_return_if_failed;
+
+ fn set_failed(&mut self, failed: &mut bool) {
+ R::set_failed(self, failed);
+ }
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+/// Marker for whether StreamDeserializer can implement FusedIterator.
+pub trait Fused: private::Sealed {}
+impl<'a> Fused for SliceRead<'a> {}
+impl<'a> Fused for StrRead<'a> {}
+
+// Lookup table of bytes that must be escaped. A value of true at index i means
+// that byte i requires an escape sequence in the input.
+static ESCAPE: [bool; 256] = {
+ const CT: bool = true; // control character \x00..=\x1F
+ const QU: bool = true; // quote \x22
+ const BS: bool = true; // backslash \x5C
+ const __: bool = false; // allow unescaped
+ [
+ // 1 2 3 4 5 6 7 8 9 A B C D E F
+ CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
+ CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
+ __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
+ __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
+ ]
+};
+
+fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
+where
+ R: ?Sized + Read<'de>,
+{
+ match tri!(read.next()) {
+ Some(b) => Ok(b),
+ None => error(read, ErrorCode::EofWhileParsingString),
+ }
+}
+
+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>,
+{
+ let position = read.position();
+ Err(Error::syntax(reason, position.line, position.column))
+}
+
+fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
+ str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
+}
+
+/// 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,
+ validate: bool,
+ scratch: &mut Vec<u8>,
+) -> Result<()> {
+ let ch = tri!(next_or_eof(read));
+
+ match ch {
+ b'"' => scratch.push(b'"'),
+ b'\\' => scratch.push(b'\\'),
+ b'/' => scratch.push(b'/'),
+ b'b' => scratch.push(b'\x08'),
+ b'f' => scratch.push(b'\x0c'),
+ b'n' => scratch.push(b'\n'),
+ 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()) {
+ 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. 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!(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!(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());
+
+ if n2 < 0xDC00 || n2 > 0xDFFF {
+ return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
+ }
+
+ let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
+
+ match char::from_u32(n) {
+ 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());
+ }
+ _ => {
+ return error(read, ErrorCode::InvalidEscape);
+ }
+ }
+
+ Ok(())
+}
+
+/// Parses a JSON escape sequence and discards the value. Assumes the previous
+/// byte read was a backslash.
+fn ignore_escape<'de, R>(read: &mut R) -> Result<()>
+where
+ R: ?Sized + Read<'de>,
+{
+ let ch = tri!(next_or_eof(read));
+
+ match ch {
+ b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
+ b'u' => {
+ // 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.
+
+ tri!(read.decode_hex_escape());
+ }
+ _ => {
+ return error(read, ErrorCode::InvalidEscape);
+ }
+ }
+
+ Ok(())
+}
+
+static HEX: [u8; 256] = {
+ const __: u8 = 255; // not a hex digit
+ [
+ // 1 2 3 4 5 6 7 8 9 A B C D E F
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 0
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 1
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
+ 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, // 3
+ __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 4
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 5
+ __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 6
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
+ ]
+};
+
+fn decode_hex_val(val: u8) -> Option<u16> {
+ let n = HEX[val as usize] as u16;
+ if n == 255 {
+ None
+ } else {
+ Some(n)
+ }
+}
diff --git a/third_party/rust/serde_json/src/ser.rs b/third_party/rust/serde_json/src/ser.rs
new file mode 100644
index 0000000000..80c2deb0c1
--- /dev/null
+++ b/third_party/rust/serde_json/src/ser.rs
@@ -0,0 +1,2179 @@
+//! Serialize a Rust data structure into JSON data.
+
+use crate::error::{Error, ErrorCode, Result};
+use crate::io;
+use alloc::string::{String, ToString};
+use alloc::vec::Vec;
+use core::fmt::{self, Display};
+use core::num::FpCategory;
+use serde::ser::{self, Impossible, Serialize};
+
+/// A structure for serializing Rust values into JSON.
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub struct Serializer<W, F = CompactFormatter> {
+ writer: W,
+ formatter: F,
+}
+
+impl<W> Serializer<W>
+where
+ W: io::Write,
+{
+ /// Creates a new JSON serializer.
+ #[inline]
+ pub fn new(writer: W) -> Self {
+ Serializer::with_formatter(writer, CompactFormatter)
+ }
+}
+
+impl<'a, W> Serializer<W, PrettyFormatter<'a>>
+where
+ W: io::Write,
+{
+ /// Creates a new JSON pretty print serializer.
+ #[inline]
+ pub fn pretty(writer: W) -> Self {
+ Serializer::with_formatter(writer, PrettyFormatter::new())
+ }
+}
+
+impl<W, F> Serializer<W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ /// Creates a new JSON visitor whose output will be written to the writer
+ /// specified.
+ #[inline]
+ pub fn with_formatter(writer: W, formatter: F) -> Self {
+ Serializer { writer, formatter }
+ }
+
+ /// Unwrap the `Writer` from the `Serializer`.
+ #[inline]
+ pub fn into_inner(self) -> W {
+ self.writer
+ }
+}
+
+impl<'a, W, F> ser::Serializer for &'a mut Serializer<W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ type SerializeSeq = Compound<'a, W, F>;
+ type SerializeTuple = Compound<'a, W, F>;
+ type SerializeTupleStruct = Compound<'a, W, F>;
+ type SerializeTupleVariant = Compound<'a, W, F>;
+ type SerializeMap = Compound<'a, W, F>;
+ type SerializeStruct = Compound<'a, W, F>;
+ type SerializeStructVariant = Compound<'a, W, F>;
+
+ #[inline]
+ fn serialize_bool(self, value: bool) -> Result<()> {
+ self.formatter
+ .write_bool(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_i8(self, value: i8) -> Result<()> {
+ self.formatter
+ .write_i8(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_i16(self, value: i16) -> Result<()> {
+ self.formatter
+ .write_i16(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_i32(self, value: i32) -> Result<()> {
+ self.formatter
+ .write_i32(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_i64(self, value: i64) -> Result<()> {
+ self.formatter
+ .write_i64(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ fn serialize_i128(self, value: i128) -> Result<()> {
+ self.formatter
+ .write_i128(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_u8(self, value: u8) -> Result<()> {
+ self.formatter
+ .write_u8(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_u16(self, value: u16) -> Result<()> {
+ self.formatter
+ .write_u16(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_u32(self, value: u32) -> Result<()> {
+ self.formatter
+ .write_u32(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_u64(self, value: u64) -> Result<()> {
+ self.formatter
+ .write_u64(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u128(self, value: u128) -> Result<()> {
+ self.formatter
+ .write_u128(&mut self.writer, value)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_f32(self, value: f32) -> Result<()> {
+ match value.classify() {
+ FpCategory::Nan | FpCategory::Infinite => self
+ .formatter
+ .write_null(&mut self.writer)
+ .map_err(Error::io),
+ _ => self
+ .formatter
+ .write_f32(&mut self.writer, value)
+ .map_err(Error::io),
+ }
+ }
+
+ #[inline]
+ fn serialize_f64(self, value: f64) -> Result<()> {
+ match value.classify() {
+ FpCategory::Nan | FpCategory::Infinite => self
+ .formatter
+ .write_null(&mut self.writer)
+ .map_err(Error::io),
+ _ => self
+ .formatter
+ .write_f64(&mut self.writer, value)
+ .map_err(Error::io),
+ }
+ }
+
+ #[inline]
+ fn serialize_char(self, value: char) -> Result<()> {
+ // A char encoded as UTF-8 takes 4 bytes at most.
+ let mut buf = [0; 4];
+ self.serialize_str(value.encode_utf8(&mut buf))
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<()> {
+ format_escaped_str(&mut self.writer, &mut self.formatter, value).map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_bytes(self, value: &[u8]) -> Result<()> {
+ use serde::ser::SerializeSeq;
+ let mut seq = tri!(self.serialize_seq(Some(value.len())));
+ for byte in value {
+ tri!(seq.serialize_element(byte));
+ }
+ seq.end()
+ }
+
+ #[inline]
+ fn serialize_unit(self) -> Result<()> {
+ self.formatter
+ .write_null(&mut self.writer)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<()> {
+ self.serialize_str(variant)
+ }
+
+ /// Serialize newtypes without an object wrapper.
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ #[inline]
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ value: &T,
+ ) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ tri!(self
+ .formatter
+ .begin_object(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_key(&mut self.writer, true)
+ .map_err(Error::io));
+ tri!(self.serialize_str(variant));
+ tri!(self
+ .formatter
+ .end_object_key(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_value(&mut self.writer)
+ .map_err(Error::io));
+ tri!(value.serialize(&mut *self));
+ tri!(self
+ .formatter
+ .end_object_value(&mut self.writer)
+ .map_err(Error::io));
+ self.formatter
+ .end_object(&mut self.writer)
+ .map_err(Error::io)
+ }
+
+ #[inline]
+ fn serialize_none(self) -> Result<()> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_some<T>(self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ #[inline]
+ fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
+ tri!(self
+ .formatter
+ .begin_array(&mut self.writer)
+ .map_err(Error::io));
+ if len == Some(0) {
+ tri!(self
+ .formatter
+ .end_array(&mut self.writer)
+ .map_err(Error::io));
+ Ok(Compound::Map {
+ ser: self,
+ state: State::Empty,
+ })
+ } else {
+ Ok(Compound::Map {
+ ser: self,
+ state: State::First,
+ })
+ }
+ }
+
+ #[inline]
+ fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
+ self.serialize_seq(Some(len))
+ }
+
+ #[inline]
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ self.serialize_seq(Some(len))
+ }
+
+ #[inline]
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ tri!(self
+ .formatter
+ .begin_object(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_key(&mut self.writer, true)
+ .map_err(Error::io));
+ tri!(self.serialize_str(variant));
+ tri!(self
+ .formatter
+ .end_object_key(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_value(&mut self.writer)
+ .map_err(Error::io));
+ self.serialize_seq(Some(len))
+ }
+
+ #[inline]
+ fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
+ tri!(self
+ .formatter
+ .begin_object(&mut self.writer)
+ .map_err(Error::io));
+ if len == Some(0) {
+ tri!(self
+ .formatter
+ .end_object(&mut self.writer)
+ .map_err(Error::io));
+ Ok(Compound::Map {
+ ser: self,
+ state: State::Empty,
+ })
+ } else {
+ Ok(Compound::Map {
+ ser: self,
+ state: State::First,
+ })
+ }
+ }
+
+ #[inline]
+ fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
+ match name {
+ #[cfg(feature = "arbitrary_precision")]
+ crate::number::TOKEN => Ok(Compound::Number { ser: self }),
+ #[cfg(feature = "raw_value")]
+ crate::raw::TOKEN => Ok(Compound::RawValue { ser: self }),
+ _ => self.serialize_map(Some(len)),
+ }
+ }
+
+ #[inline]
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ tri!(self
+ .formatter
+ .begin_object(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_key(&mut self.writer, true)
+ .map_err(Error::io));
+ tri!(self.serialize_str(variant));
+ tri!(self
+ .formatter
+ .end_object_key(&mut self.writer)
+ .map_err(Error::io));
+ tri!(self
+ .formatter
+ .begin_object_value(&mut self.writer)
+ .map_err(Error::io));
+ self.serialize_map(Some(len))
+ }
+
+ fn collect_str<T>(self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Display,
+ {
+ use self::fmt::Write;
+
+ struct Adapter<'ser, W: 'ser, F: 'ser> {
+ writer: &'ser mut W,
+ formatter: &'ser mut F,
+ error: Option<io::Error>,
+ }
+
+ impl<'ser, W, F> Write for Adapter<'ser, W, F>
+ where
+ W: io::Write,
+ F: Formatter,
+ {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ debug_assert!(self.error.is_none());
+ match format_escaped_str_contents(self.writer, self.formatter, s) {
+ Ok(()) => Ok(()),
+ Err(err) => {
+ self.error = Some(err);
+ Err(fmt::Error)
+ }
+ }
+ }
+ }
+
+ tri!(self
+ .formatter
+ .begin_string(&mut self.writer)
+ .map_err(Error::io));
+ {
+ let mut adapter = Adapter {
+ writer: &mut self.writer,
+ formatter: &mut self.formatter,
+ error: None,
+ };
+ match write!(adapter, "{}", value) {
+ Ok(()) => debug_assert!(adapter.error.is_none()),
+ Err(fmt::Error) => {
+ return Err(Error::io(adapter.error.expect("there should be an error")));
+ }
+ }
+ }
+ self.formatter
+ .end_string(&mut self.writer)
+ .map_err(Error::io)
+ }
+}
+
+// Not public API. Should be pub(crate).
+#[doc(hidden)]
+#[derive(Eq, PartialEq)]
+pub enum State {
+ Empty,
+ First,
+ Rest,
+}
+
+// Not public API. Should be pub(crate).
+#[doc(hidden)]
+pub enum Compound<'a, W: 'a, F: 'a> {
+ Map {
+ ser: &'a mut Serializer<W, F>,
+ state: State,
+ },
+ #[cfg(feature = "arbitrary_precision")]
+ Number { ser: &'a mut Serializer<W, F> },
+ #[cfg(feature = "raw_value")]
+ RawValue { ser: &'a mut Serializer<W, F> },
+}
+
+impl<'a, W, F> ser::SerializeSeq for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ Compound::Map { ser, state } => {
+ tri!(ser
+ .formatter
+ .begin_array_value(&mut ser.writer, *state == State::First)
+ .map_err(Error::io));
+ *state = State::Rest;
+ tri!(value.serialize(&mut **ser));
+ ser.formatter
+ .end_array_value(&mut ser.writer)
+ .map_err(Error::io)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ match self {
+ Compound::Map { ser, state } => match state {
+ State::Empty => Ok(()),
+ _ => ser.formatter.end_array(&mut ser.writer).map_err(Error::io),
+ },
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+}
+
+impl<'a, W, F> ser::SerializeTuple for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ ser::SerializeSeq::end(self)
+ }
+}
+
+impl<'a, W, F> ser::SerializeTupleStruct for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ ser::SerializeSeq::end(self)
+ }
+}
+
+impl<'a, W, F> ser::SerializeTupleVariant for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ match self {
+ Compound::Map { ser, state } => {
+ match state {
+ State::Empty => {}
+ _ => tri!(ser.formatter.end_array(&mut ser.writer).map_err(Error::io)),
+ }
+ tri!(ser
+ .formatter
+ .end_object_value(&mut ser.writer)
+ .map_err(Error::io));
+ ser.formatter.end_object(&mut ser.writer).map_err(Error::io)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+}
+
+impl<'a, W, F> ser::SerializeMap for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_key<T>(&mut self, key: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ Compound::Map { ser, state } => {
+ tri!(ser
+ .formatter
+ .begin_object_key(&mut ser.writer, *state == State::First)
+ .map_err(Error::io));
+ *state = State::Rest;
+
+ tri!(key.serialize(MapKeySerializer { ser: *ser }));
+
+ ser.formatter
+ .end_object_key(&mut ser.writer)
+ .map_err(Error::io)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ #[inline]
+ fn serialize_value<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ Compound::Map { ser, .. } => {
+ tri!(ser
+ .formatter
+ .begin_object_value(&mut ser.writer)
+ .map_err(Error::io));
+ tri!(value.serialize(&mut **ser));
+ ser.formatter
+ .end_object_value(&mut ser.writer)
+ .map_err(Error::io)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ match self {
+ Compound::Map { ser, state } => match state {
+ State::Empty => Ok(()),
+ _ => ser.formatter.end_object(&mut ser.writer).map_err(Error::io),
+ },
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+}
+
+impl<'a, W, F> ser::SerializeStruct for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ Compound::Map { .. } => ser::SerializeMap::serialize_entry(self, key, value),
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { ser, .. } => {
+ if key == crate::number::TOKEN {
+ value.serialize(NumberStrEmitter(ser))
+ } else {
+ Err(invalid_number())
+ }
+ }
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { ser, .. } => {
+ if key == crate::raw::TOKEN {
+ value.serialize(RawValueStrEmitter(ser))
+ } else {
+ Err(invalid_raw_value())
+ }
+ }
+ }
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ match self {
+ Compound::Map { .. } => ser::SerializeMap::end(self),
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => Ok(()),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => Ok(()),
+ }
+ }
+}
+
+impl<'a, W, F> ser::SerializeStructVariant for Compound<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match *self {
+ Compound::Map { .. } => ser::SerializeStruct::serialize_field(self, key, value),
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ #[inline]
+ fn end(self) -> Result<()> {
+ match self {
+ Compound::Map { ser, state } => {
+ match state {
+ State::Empty => {}
+ _ => tri!(ser.formatter.end_object(&mut ser.writer).map_err(Error::io)),
+ }
+ tri!(ser
+ .formatter
+ .end_object_value(&mut ser.writer)
+ .map_err(Error::io));
+ ser.formatter.end_object(&mut ser.writer).map_err(Error::io)
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ Compound::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ Compound::RawValue { .. } => unreachable!(),
+ }
+ }
+}
+
+struct MapKeySerializer<'a, W: 'a, F: 'a> {
+ ser: &'a mut Serializer<W, F>,
+}
+
+#[cfg(feature = "arbitrary_precision")]
+fn invalid_number() -> Error {
+ Error::syntax(ErrorCode::InvalidNumber, 0, 0)
+}
+
+#[cfg(feature = "raw_value")]
+fn invalid_raw_value() -> Error {
+ Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
+}
+
+fn key_must_be_a_string() -> Error {
+ Error::syntax(ErrorCode::KeyMustBeAString, 0, 0)
+}
+
+impl<'a, W, F> ser::Serializer for MapKeySerializer<'a, W, F>
+where
+ W: io::Write,
+ F: Formatter,
+{
+ type Ok = ();
+ type Error = Error;
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<()> {
+ self.ser.serialize_str(value)
+ }
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<()> {
+ self.ser.serialize_str(variant)
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ type SerializeSeq = Impossible<(), Error>;
+ type SerializeTuple = Impossible<(), Error>;
+ type SerializeTupleStruct = Impossible<(), Error>;
+ type SerializeTupleVariant = Impossible<(), Error>;
+ type SerializeMap = Impossible<(), Error>;
+ type SerializeStruct = Impossible<(), Error>;
+ type SerializeStructVariant = Impossible<(), Error>;
+
+ fn serialize_bool(self, _value: bool) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_i8(self, value: i8) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_i8(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_i16(self, value: i16) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_i16(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_i32(self, value: i32) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_i32(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_i64(self, value: i64) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_i64(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_i128(self, value: i128) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_i128(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u8(self, value: u8) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_u8(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u16(self, value: u16) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_u16(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u32(self, value: u32) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_u32(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u64(self, value: u64) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_u64(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_u128(self, value: u128) -> Result<()> {
+ tri!(self
+ .ser
+ .formatter
+ .begin_string(&mut self.ser.writer)
+ .map_err(Error::io));
+ tri!(self
+ .ser
+ .formatter
+ .write_u128(&mut self.ser.writer, value)
+ .map_err(Error::io));
+ self.ser
+ .formatter
+ .end_string(&mut self.ser.writer)
+ .map_err(Error::io)
+ }
+
+ fn serialize_f32(self, _value: f32) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_f64(self, _value: f64) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_char(self, value: char) -> Result<()> {
+ self.ser.serialize_str(&value.to_string())
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_unit(self) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_none(self) -> Result<()> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Err(key_must_be_a_string())
+ }
+
+ fn collect_str<T>(self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Display,
+ {
+ self.ser.collect_str(value)
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+struct NumberStrEmitter<'a, W: 'a + io::Write, F: 'a + Formatter>(&'a mut Serializer<W, F>);
+
+#[cfg(feature = "arbitrary_precision")]
+impl<'a, W: io::Write, F: Formatter> ser::Serializer for NumberStrEmitter<'a, W, F> {
+ type Ok = ();
+ type Error = Error;
+
+ type SerializeSeq = Impossible<(), Error>;
+ type SerializeTuple = Impossible<(), Error>;
+ type SerializeTupleStruct = Impossible<(), Error>;
+ type SerializeTupleVariant = Impossible<(), Error>;
+ type SerializeMap = Impossible<(), Error>;
+ type SerializeStruct = Impossible<(), Error>;
+ type SerializeStructVariant = Impossible<(), Error>;
+
+ fn serialize_bool(self, _v: bool) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i8(self, _v: i8) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i16(self, _v: i16) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i32(self, _v: i32) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i64(self, _v: i64) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i128(self, _v: i128) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u8(self, _v: u8) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u16(self, _v: u16) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u32(self, _v: u32) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u64(self, _v: u64) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u128(self, _v: u128) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_f32(self, _v: f32) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_f64(self, _v: f64) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_char(self, _v: char) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_str(self, value: &str) -> Result<()> {
+ let NumberStrEmitter(serializer) = self;
+ serializer
+ .formatter
+ .write_number_str(&mut serializer.writer, value)
+ .map_err(Error::io)
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_none(self) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit(self) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ ) -> Result<()> {
+ Err(invalid_number())
+ }
+
+ fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(invalid_number())
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(invalid_number())
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(invalid_number())
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Err(invalid_number())
+ }
+}
+
+#[cfg(feature = "raw_value")]
+struct RawValueStrEmitter<'a, W: 'a + io::Write, F: 'a + Formatter>(&'a mut Serializer<W, F>);
+
+#[cfg(feature = "raw_value")]
+impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, W, F> {
+ type Ok = ();
+ type Error = Error;
+
+ type SerializeSeq = Impossible<(), Error>;
+ type SerializeTuple = Impossible<(), Error>;
+ type SerializeTupleStruct = Impossible<(), Error>;
+ type SerializeTupleVariant = Impossible<(), Error>;
+ type SerializeMap = Impossible<(), Error>;
+ type SerializeStruct = Impossible<(), Error>;
+ type SerializeStructVariant = Impossible<(), Error>;
+
+ fn serialize_bool(self, _v: bool) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_i8(self, _v: i8) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_i16(self, _v: i16) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_i32(self, _v: i32) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_i64(self, _v: i64) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_i128(self, _v: i128) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_u8(self, _v: u8) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_u16(self, _v: u16) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_u32(self, _v: u32) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_u64(self, _v: u64) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_u128(self, _v: u128) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_f32(self, _v: f32) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_f64(self, _v: f64) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_char(self, _v: char) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_str(self, value: &str) -> Result<()> {
+ let RawValueStrEmitter(serializer) = self;
+ serializer
+ .formatter
+ .write_raw_fragment(&mut serializer.writer, value)
+ .map_err(Error::io)
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_none(self) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_unit(self) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ ) -> Result<()> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(ser::Error::custom("expected RawValue"))
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> 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.
+pub enum CharEscape {
+ /// An escaped quote `"`
+ Quote,
+ /// An escaped reverse solidus `\`
+ ReverseSolidus,
+ /// An escaped solidus `/`
+ Solidus,
+ /// An escaped backspace character (usually escaped as `\b`)
+ Backspace,
+ /// An escaped form feed character (usually escaped as `\f`)
+ FormFeed,
+ /// An escaped line feed character (usually escaped as `\n`)
+ LineFeed,
+ /// An escaped carriage return character (usually escaped as `\r`)
+ CarriageReturn,
+ /// An escaped tab character (usually escaped as `\t`)
+ Tab,
+ /// An escaped ASCII plane control character (usually escaped as
+ /// `\u00XX` where `XX` are two hex characters)
+ AsciiControl(u8),
+}
+
+impl CharEscape {
+ #[inline]
+ fn from_escape_table(escape: u8, byte: u8) -> CharEscape {
+ match escape {
+ self::BB => CharEscape::Backspace,
+ self::TT => CharEscape::Tab,
+ self::NN => CharEscape::LineFeed,
+ self::FF => CharEscape::FormFeed,
+ self::RR => CharEscape::CarriageReturn,
+ self::QU => CharEscape::Quote,
+ self::BS => CharEscape::ReverseSolidus,
+ self::UU => CharEscape::AsciiControl(byte),
+ _ => unreachable!(),
+ }
+ }
+}
+
+/// This trait abstracts away serializing the JSON control characters, which allows the user to
+/// optionally pretty print the JSON output.
+pub trait Formatter {
+ /// Writes a `null` value to the specified writer.
+ #[inline]
+ fn write_null<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"null")
+ }
+
+ /// Writes a `true` or `false` value to the specified writer.
+ #[inline]
+ fn write_bool<W>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let s = if value {
+ b"true" as &[u8]
+ } else {
+ b"false" as &[u8]
+ };
+ writer.write_all(s)
+ }
+
+ /// Writes an integer value like `-123` to the specified writer.
+ #[inline]
+ fn write_i8<W>(&mut self, writer: &mut W, value: i8) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `-123` to the specified writer.
+ #[inline]
+ fn write_i16<W>(&mut self, writer: &mut W, value: i16) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `-123` to the specified writer.
+ #[inline]
+ fn write_i32<W>(&mut self, writer: &mut W, value: i32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `-123` to the specified writer.
+ #[inline]
+ fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `-123` to the specified writer.
+ #[inline]
+ fn write_i128<W>(&mut self, writer: &mut W, value: i128) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `123` to the specified writer.
+ #[inline]
+ fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `123` to the specified writer.
+ #[inline]
+ fn write_u16<W>(&mut self, writer: &mut W, value: u16) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `123` to the specified writer.
+ #[inline]
+ fn write_u32<W>(&mut self, writer: &mut W, value: u32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `123` to the specified writer.
+ #[inline]
+ fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes an integer value like `123` to the specified writer.
+ #[inline]
+ fn write_u128<W>(&mut self, writer: &mut W, value: u128) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = itoa::Buffer::new();
+ let s = buffer.format(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes a floating point value like `-31.26e+12` to the specified writer.
+ #[inline]
+ fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = ryu::Buffer::new();
+ let s = buffer.format_finite(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes a floating point value like `-31.26e+12` to the specified writer.
+ #[inline]
+ fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ let mut buffer = ryu::Buffer::new();
+ let s = buffer.format_finite(value);
+ writer.write_all(s.as_bytes())
+ }
+
+ /// Writes a number that has already been rendered to a string.
+ #[inline]
+ fn write_number_str<W>(&mut self, writer: &mut W, value: &str) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(value.as_bytes())
+ }
+
+ /// Called before each series of `write_string_fragment` and
+ /// `write_char_escape`. Writes a `"` to the specified writer.
+ #[inline]
+ fn begin_string<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"\"")
+ }
+
+ /// Called after each series of `write_string_fragment` and
+ /// `write_char_escape`. Writes a `"` to the specified writer.
+ #[inline]
+ fn end_string<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"\"")
+ }
+
+ /// Writes a string fragment that doesn't need any escaping to the
+ /// specified writer.
+ #[inline]
+ fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(fragment.as_bytes())
+ }
+
+ /// Writes a character escape code to the specified writer.
+ #[inline]
+ fn write_char_escape<W>(&mut self, writer: &mut W, char_escape: CharEscape) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ use self::CharEscape::*;
+
+ let s = match char_escape {
+ Quote => b"\\\"",
+ ReverseSolidus => b"\\\\",
+ Solidus => b"\\/",
+ Backspace => b"\\b",
+ FormFeed => b"\\f",
+ LineFeed => b"\\n",
+ CarriageReturn => b"\\r",
+ Tab => b"\\t",
+ AsciiControl(byte) => {
+ static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";
+ let bytes = &[
+ b'\\',
+ b'u',
+ b'0',
+ b'0',
+ HEX_DIGITS[(byte >> 4) as usize],
+ HEX_DIGITS[(byte & 0xF) as usize],
+ ];
+ return writer.write_all(bytes);
+ }
+ };
+
+ writer.write_all(s)
+ }
+
+ /// Called before every array. Writes a `[` to the specified
+ /// writer.
+ #[inline]
+ fn begin_array<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"[")
+ }
+
+ /// Called after every array. Writes a `]` to the specified
+ /// writer.
+ #[inline]
+ fn end_array<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"]")
+ }
+
+ /// Called before every array value. Writes a `,` if needed to
+ /// the specified writer.
+ #[inline]
+ fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ if first {
+ Ok(())
+ } else {
+ writer.write_all(b",")
+ }
+ }
+
+ /// Called after every array value.
+ #[inline]
+ fn end_array_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ Ok(())
+ }
+
+ /// Called before every object. Writes a `{` to the specified
+ /// writer.
+ #[inline]
+ fn begin_object<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"{")
+ }
+
+ /// Called after every object. Writes a `}` to the specified
+ /// writer.
+ #[inline]
+ fn end_object<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b"}")
+ }
+
+ /// Called before every object key.
+ #[inline]
+ fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ if first {
+ Ok(())
+ } else {
+ writer.write_all(b",")
+ }
+ }
+
+ /// Called after every object key. A `:` should be written to the
+ /// specified writer by either this method or
+ /// `begin_object_value`.
+ #[inline]
+ fn end_object_key<W>(&mut self, _writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ Ok(())
+ }
+
+ /// Called before every object value. A `:` should be written to
+ /// the specified writer by either this method or
+ /// `end_object_key`.
+ #[inline]
+ fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b":")
+ }
+
+ /// Called after every object value.
+ #[inline]
+ fn end_object_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ Ok(())
+ }
+
+ /// Writes a raw JSON fragment that doesn't need any escaping to the
+ /// specified writer.
+ #[inline]
+ fn write_raw_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(fragment.as_bytes())
+ }
+}
+
+/// This structure compacts a JSON value with no extra whitespace.
+#[derive(Clone, Debug)]
+pub struct CompactFormatter;
+
+impl Formatter for CompactFormatter {}
+
+/// This structure pretty prints a JSON value to make it human readable.
+#[derive(Clone, Debug)]
+pub struct PrettyFormatter<'a> {
+ current_indent: usize,
+ has_value: bool,
+ indent: &'a [u8],
+}
+
+impl<'a> PrettyFormatter<'a> {
+ /// Construct a pretty printer formatter that defaults to using two spaces for indentation.
+ pub fn new() -> Self {
+ PrettyFormatter::with_indent(b" ")
+ }
+
+ /// Construct a pretty printer formatter that uses the `indent` string for indentation.
+ pub fn with_indent(indent: &'a [u8]) -> Self {
+ PrettyFormatter {
+ current_indent: 0,
+ has_value: false,
+ indent,
+ }
+ }
+}
+
+impl<'a> Default for PrettyFormatter<'a> {
+ fn default() -> Self {
+ PrettyFormatter::new()
+ }
+}
+
+impl<'a> Formatter for PrettyFormatter<'a> {
+ #[inline]
+ fn begin_array<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.current_indent += 1;
+ self.has_value = false;
+ writer.write_all(b"[")
+ }
+
+ #[inline]
+ fn end_array<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.current_indent -= 1;
+
+ if self.has_value {
+ tri!(writer.write_all(b"\n"));
+ tri!(indent(writer, self.current_indent, self.indent));
+ }
+
+ writer.write_all(b"]")
+ }
+
+ #[inline]
+ fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ tri!(writer.write_all(if first { b"\n" } else { b",\n" }));
+ indent(writer, self.current_indent, self.indent)
+ }
+
+ #[inline]
+ fn end_array_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.has_value = true;
+ Ok(())
+ }
+
+ #[inline]
+ fn begin_object<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.current_indent += 1;
+ self.has_value = false;
+ writer.write_all(b"{")
+ }
+
+ #[inline]
+ fn end_object<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.current_indent -= 1;
+
+ if self.has_value {
+ tri!(writer.write_all(b"\n"));
+ tri!(indent(writer, self.current_indent, self.indent));
+ }
+
+ writer.write_all(b"}")
+ }
+
+ #[inline]
+ fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ tri!(writer.write_all(if first { b"\n" } else { b",\n" }));
+ indent(writer, self.current_indent, self.indent)
+ }
+
+ #[inline]
+ fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ writer.write_all(b": ")
+ }
+
+ #[inline]
+ fn end_object_value<W>(&mut self, _writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
+ self.has_value = true;
+ Ok(())
+ }
+}
+
+fn format_escaped_str<W, F>(writer: &mut W, formatter: &mut F, value: &str) -> io::Result<()>
+where
+ W: ?Sized + io::Write,
+ F: ?Sized + Formatter,
+{
+ tri!(formatter.begin_string(writer));
+ tri!(format_escaped_str_contents(writer, formatter, value));
+ formatter.end_string(writer)
+}
+
+fn format_escaped_str_contents<W, F>(
+ writer: &mut W,
+ formatter: &mut F,
+ value: &str,
+) -> io::Result<()>
+where
+ W: ?Sized + io::Write,
+ F: ?Sized + Formatter,
+{
+ let bytes = value.as_bytes();
+
+ let mut start = 0;
+
+ for (i, &byte) in bytes.iter().enumerate() {
+ let escape = ESCAPE[byte as usize];
+ if escape == 0 {
+ continue;
+ }
+
+ if start < i {
+ tri!(formatter.write_string_fragment(writer, &value[start..i]));
+ }
+
+ let char_escape = CharEscape::from_escape_table(escape, byte);
+ tri!(formatter.write_char_escape(writer, char_escape));
+
+ start = i + 1;
+ }
+
+ if start == bytes.len() {
+ return Ok(());
+ }
+
+ formatter.write_string_fragment(writer, &value[start..])
+}
+
+const BB: u8 = b'b'; // \x08
+const TT: u8 = b't'; // \x09
+const NN: u8 = b'n'; // \x0A
+const FF: u8 = b'f'; // \x0C
+const RR: u8 = b'r'; // \x0D
+const QU: u8 = b'"'; // \x22
+const BS: u8 = b'\\'; // \x5C
+const UU: u8 = b'u'; // \x00...\x1F except the ones above
+const __: u8 = 0;
+
+// Lookup table of escape sequences. A value of b'x' at index i means that byte
+// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.
+static ESCAPE: [u8; 256] = [
+ // 1 2 3 4 5 6 7 8 9 A B C D E F
+ UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0
+ UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1
+ __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
+ __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
+ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
+];
+
+/// Serialize the given data structure as JSON into the IO stream.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub fn to_writer<W, T>(writer: W, value: &T) -> Result<()>
+where
+ W: io::Write,
+ T: ?Sized + Serialize,
+{
+ let mut ser = Serializer::new(writer);
+ value.serialize(&mut ser)
+}
+
+/// Serialize the given data structure as pretty-printed JSON into the IO
+/// stream.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
+pub fn to_writer_pretty<W, T>(writer: W, value: &T) -> Result<()>
+where
+ W: io::Write,
+ T: ?Sized + Serialize,
+{
+ let mut ser = Serializer::pretty(writer);
+ value.serialize(&mut ser)
+}
+
+/// Serialize the given data structure as a JSON byte vector.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
+where
+ T: ?Sized + Serialize,
+{
+ let mut writer = Vec::with_capacity(128);
+ tri!(to_writer(&mut writer, value));
+ Ok(writer)
+}
+
+/// Serialize the given data structure as a pretty-printed JSON byte vector.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+pub fn to_vec_pretty<T>(value: &T) -> Result<Vec<u8>>
+where
+ T: ?Sized + Serialize,
+{
+ let mut writer = Vec::with_capacity(128);
+ tri!(to_writer_pretty(&mut writer, value));
+ Ok(writer)
+}
+
+/// Serialize the given data structure as a String of JSON.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+pub fn to_string<T>(value: &T) -> Result<String>
+where
+ T: ?Sized + Serialize,
+{
+ let vec = tri!(to_vec(value));
+ let string = unsafe {
+ // We do not emit invalid UTF-8.
+ String::from_utf8_unchecked(vec)
+ };
+ Ok(string)
+}
+
+/// Serialize the given data structure as a pretty-printed String of JSON.
+///
+/// # Errors
+///
+/// Serialization can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+#[inline]
+pub fn to_string_pretty<T>(value: &T) -> Result<String>
+where
+ T: ?Sized + Serialize,
+{
+ let vec = tri!(to_vec_pretty(value));
+ let string = unsafe {
+ // We do not emit invalid UTF-8.
+ String::from_utf8_unchecked(vec)
+ };
+ Ok(string)
+}
+
+fn indent<W>(wr: &mut W, n: usize, s: &[u8]) -> io::Result<()>
+where
+ W: ?Sized + io::Write,
+{
+ for _ in 0..n {
+ tri!(wr.write_all(s));
+ }
+
+ Ok(())
+}
diff --git a/third_party/rust/serde_json/src/value/de.rs b/third_party/rust/serde_json/src/value/de.rs
new file mode 100644
index 0000000000..9c266d08ae
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/de.rs
@@ -0,0 +1,1377 @@
+use crate::error::Error;
+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,
+};
+use serde::forward_to_deserialize_any;
+
+#[cfg(feature = "arbitrary_precision")]
+use crate::number::NumberFromString;
+
+impl<'de> Deserialize<'de> for Value {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ struct ValueVisitor;
+
+ impl<'de> Visitor<'de> for ValueVisitor {
+ type Value = Value;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("any valid JSON value")
+ }
+
+ #[inline]
+ fn visit_bool<E>(self, value: bool) -> Result<Value, E> {
+ Ok(Value::Bool(value))
+ }
+
+ #[inline]
+ fn visit_i64<E>(self, value: i64) -> Result<Value, E> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[inline]
+ fn visit_u64<E>(self, value: u64) -> Result<Value, E> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[inline]
+ fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
+ Ok(Number::from_f64(value).map_or(Value::Null, Value::Number))
+ }
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[inline]
+ fn visit_str<E>(self, value: &str) -> Result<Value, E>
+ where
+ E: serde::de::Error,
+ {
+ self.visit_string(String::from(value))
+ }
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ #[inline]
+ fn visit_string<E>(self, value: String) -> Result<Value, E> {
+ Ok(Value::String(value))
+ }
+
+ #[inline]
+ fn visit_none<E>(self) -> Result<Value, E> {
+ Ok(Value::Null)
+ }
+
+ #[inline]
+ fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ Deserialize::deserialize(deserializer)
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<Value, E> {
+ Ok(Value::Null)
+ }
+
+ #[inline]
+ fn visit_seq<V>(self, mut visitor: V) -> Result<Value, V::Error>
+ where
+ V: SeqAccess<'de>,
+ {
+ let mut vec = Vec::new();
+
+ while let Some(elem) = tri!(visitor.next_element()) {
+ vec.push(elem);
+ }
+
+ Ok(Value::Array(vec))
+ }
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error>
+ where
+ V: MapAccess<'de>,
+ {
+ match visitor.next_key_seed(KeyClassifier)? {
+ #[cfg(feature = "arbitrary_precision")]
+ Some(KeyClass::Number) => {
+ let number: NumberFromString = visitor.next_value()?;
+ Ok(Value::Number(number.value))
+ }
+ #[cfg(feature = "raw_value")]
+ Some(KeyClass::RawValue) => {
+ let value = visitor.next_value_seed(crate::raw::BoxedFromString)?;
+ crate::from_str(value.get()).map_err(de::Error::custom)
+ }
+ Some(KeyClass::Map(first_key)) => {
+ let mut values = Map::new();
+
+ values.insert(first_key, tri!(visitor.next_value()));
+ while let Some((key, value)) = tri!(visitor.next_entry()) {
+ values.insert(key, value);
+ }
+
+ Ok(Value::Object(values))
+ }
+ None => Ok(Value::Object(Map::new())),
+ }
+ }
+ }
+
+ deserializer.deserialize_any(ValueVisitor)
+ }
+}
+
+impl FromStr for Value {
+ type Err = Error;
+ fn from_str(s: &str) -> Result<Value, Error> {
+ super::super::de::from_str(s)
+ }
+}
+
+macro_rules! deserialize_number {
+ ($method:ident) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Number(n) => n.deserialize_any(visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Number(n) => n.$method(visitor),
+ _ => self.deserialize_any(visitor),
+ }
+ }
+ };
+}
+
+fn visit_array<'de, V>(array: Vec<Value>, visitor: V) -> Result<V::Value, Error>
+where
+ V: Visitor<'de>,
+{
+ let len = array.len();
+ let mut deserializer = SeqDeserializer::new(array);
+ let seq = tri!(visitor.visit_seq(&mut deserializer));
+ let remaining = deserializer.iter.len();
+ if remaining == 0 {
+ Ok(seq)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ len,
+ &"fewer elements in array",
+ ))
+ }
+}
+
+fn visit_object<'de, V>(object: Map<String, Value>, visitor: V) -> Result<V::Value, Error>
+where
+ V: Visitor<'de>,
+{
+ let len = object.len();
+ let mut deserializer = MapDeserializer::new(object);
+ let map = tri!(visitor.visit_map(&mut deserializer));
+ let remaining = deserializer.iter.len();
+ if remaining == 0 {
+ Ok(map)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ len,
+ &"fewer elements in map",
+ ))
+ }
+}
+
+impl<'de> serde::Deserializer<'de> for Value {
+ type Error = Error;
+
+ #[inline]
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Null => visitor.visit_unit(),
+ Value::Bool(v) => visitor.visit_bool(v),
+ Value::Number(n) => n.deserialize_any(visitor),
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ Value::String(v) => visitor.visit_string(v),
+ Value::Array(v) => visit_array(v, visitor),
+ Value::Object(v) => visit_object(v, visitor),
+ }
+ }
+
+ deserialize_number!(deserialize_i8);
+ deserialize_number!(deserialize_i16);
+ deserialize_number!(deserialize_i32);
+ deserialize_number!(deserialize_i64);
+ deserialize_number!(deserialize_i128);
+ deserialize_number!(deserialize_u8);
+ deserialize_number!(deserialize_u16);
+ deserialize_number!(deserialize_u32);
+ deserialize_number!(deserialize_u64);
+ deserialize_number!(deserialize_u128);
+ deserialize_number!(deserialize_f32);
+ deserialize_number!(deserialize_f64);
+
+ #[inline]
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Null => visitor.visit_none(),
+ _ => visitor.visit_some(self),
+ }
+ }
+
+ #[inline]
+ fn deserialize_enum<V>(
+ self,
+ _name: &str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ let (variant, value) = match self {
+ Value::Object(value) => {
+ let mut iter = value.into_iter();
+ let (variant, value) = match iter.next() {
+ Some(v) => v,
+ None => {
+ return Err(serde::de::Error::invalid_value(
+ Unexpected::Map,
+ &"map with a single key",
+ ));
+ }
+ };
+ // enums are encoded in json as maps with a single key:value pair
+ if iter.next().is_some() {
+ return Err(serde::de::Error::invalid_value(
+ Unexpected::Map,
+ &"map with a single key",
+ ));
+ }
+ (variant, Some(value))
+ }
+ Value::String(variant) => (variant, None),
+ other => {
+ return Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"string or map",
+ ));
+ }
+ };
+
+ visitor.visit_enum(EnumDeserializer { variant, value })
+ }
+
+ #[inline]
+ fn deserialize_newtype_struct<V>(
+ self,
+ name: &'static str,
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ #[cfg(feature = "raw_value")]
+ {
+ if name == crate::raw::TOKEN {
+ return visitor.visit_map(crate::raw::OwnedRawDeserializer {
+ raw_value: Some(self.to_string()),
+ });
+ }
+ }
+
+ let _ = name;
+ visitor.visit_newtype_struct(self)
+ }
+
+ fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Bool(v) => visitor.visit_bool(v),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_string(visitor)
+ }
+
+ fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_string(visitor)
+ }
+
+ fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ Value::String(v) => visitor.visit_string(v),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_byte_buf(visitor)
+ }
+
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ Value::String(v) => visitor.visit_string(v),
+ Value::Array(v) => visit_array(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Null => visitor.visit_unit(),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_unit(visitor)
+ }
+
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Array(v) => visit_array(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_tuple_struct<V>(
+ self,
+ _name: &'static str,
+ _len: usize,
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Object(v) => visit_object(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_struct<V>(
+ self,
+ _name: &'static str,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Array(v) => visit_array(v, visitor),
+ Value::Object(v) => visit_object(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_string(visitor)
+ }
+
+ fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ drop(self);
+ visitor.visit_unit()
+ }
+}
+
+struct EnumDeserializer {
+ variant: String,
+ value: Option<Value>,
+}
+
+impl<'de> EnumAccess<'de> for EnumDeserializer {
+ type Error = Error;
+ type Variant = VariantDeserializer;
+
+ fn variant_seed<V>(self, seed: V) -> Result<(V::Value, VariantDeserializer), Error>
+ where
+ V: DeserializeSeed<'de>,
+ {
+ let variant = self.variant.into_deserializer();
+ let visitor = VariantDeserializer { value: self.value };
+ seed.deserialize(variant).map(|v| (v, visitor))
+ }
+}
+
+impl<'de> IntoDeserializer<'de, Error> for Value {
+ type Deserializer = Self;
+
+ fn into_deserializer(self) -> Self::Deserializer {
+ self
+ }
+}
+
+struct VariantDeserializer {
+ value: Option<Value>,
+}
+
+impl<'de> VariantAccess<'de> for VariantDeserializer {
+ type Error = Error;
+
+ fn unit_variant(self) -> Result<(), Error> {
+ match self.value {
+ Some(value) => Deserialize::deserialize(value),
+ None => Ok(()),
+ }
+ }
+
+ fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.value {
+ Some(value) => seed.deserialize(value),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"newtype variant",
+ )),
+ }
+ }
+
+ fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self.value {
+ Some(Value::Array(v)) => {
+ if v.is_empty() {
+ visitor.visit_unit()
+ } else {
+ visit_array(v, visitor)
+ }
+ }
+ Some(other) => Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"tuple variant",
+ )),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"tuple variant",
+ )),
+ }
+ }
+
+ fn struct_variant<V>(
+ self,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self.value {
+ Some(Value::Object(v)) => visit_object(v, visitor),
+ Some(other) => Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"struct variant",
+ )),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"struct variant",
+ )),
+ }
+ }
+}
+
+struct SeqDeserializer {
+ iter: vec::IntoIter<Value>,
+}
+
+impl SeqDeserializer {
+ fn new(vec: Vec<Value>) -> Self {
+ SeqDeserializer {
+ iter: vec.into_iter(),
+ }
+ }
+}
+
+impl<'de> SeqAccess<'de> for SeqDeserializer {
+ type Error = Error;
+
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.iter.next() {
+ Some(value) => seed.deserialize(value).map(Some),
+ None => Ok(None),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ match self.iter.size_hint() {
+ (lower, Some(upper)) if lower == upper => Some(upper),
+ _ => None,
+ }
+ }
+}
+
+struct MapDeserializer {
+ iter: <Map<String, Value> as IntoIterator>::IntoIter,
+ value: Option<Value>,
+}
+
+impl MapDeserializer {
+ fn new(map: Map<String, Value>) -> Self {
+ MapDeserializer {
+ iter: map.into_iter(),
+ value: None,
+ }
+ }
+}
+
+impl<'de> MapAccess<'de> for MapDeserializer {
+ type Error = Error;
+
+ fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.iter.next() {
+ Some((key, value)) => {
+ self.value = Some(value);
+ let key_de = MapKeyDeserializer {
+ key: Cow::Owned(key),
+ };
+ seed.deserialize(key_de).map(Some)
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.value.take() {
+ Some(value) => seed.deserialize(value),
+ None => Err(serde::de::Error::custom("value is missing")),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ match self.iter.size_hint() {
+ (lower, Some(upper)) if lower == upper => Some(upper),
+ _ => None,
+ }
+ }
+}
+
+macro_rules! deserialize_value_ref_number {
+ ($method:ident) => {
+ #[cfg(not(feature = "arbitrary_precision"))]
+ fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Number(n) => n.deserialize_any(visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Number(n) => n.$method(visitor),
+ _ => self.deserialize_any(visitor),
+ }
+ }
+ };
+}
+
+fn visit_array_ref<'de, V>(array: &'de [Value], visitor: V) -> Result<V::Value, Error>
+where
+ V: Visitor<'de>,
+{
+ let len = array.len();
+ let mut deserializer = SeqRefDeserializer::new(array);
+ let seq = tri!(visitor.visit_seq(&mut deserializer));
+ let remaining = deserializer.iter.len();
+ if remaining == 0 {
+ Ok(seq)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ len,
+ &"fewer elements in array",
+ ))
+ }
+}
+
+fn visit_object_ref<'de, V>(object: &'de Map<String, Value>, visitor: V) -> Result<V::Value, Error>
+where
+ V: Visitor<'de>,
+{
+ let len = object.len();
+ let mut deserializer = MapRefDeserializer::new(object);
+ let map = tri!(visitor.visit_map(&mut deserializer));
+ let remaining = deserializer.iter.len();
+ if remaining == 0 {
+ Ok(map)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ len,
+ &"fewer elements in map",
+ ))
+ }
+}
+
+impl<'de> serde::Deserializer<'de> for &'de Value {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Null => visitor.visit_unit(),
+ Value::Bool(v) => visitor.visit_bool(*v),
+ Value::Number(n) => n.deserialize_any(visitor),
+ Value::String(v) => visitor.visit_borrowed_str(v),
+ Value::Array(v) => visit_array_ref(v, visitor),
+ Value::Object(v) => visit_object_ref(v, visitor),
+ }
+ }
+
+ deserialize_value_ref_number!(deserialize_i8);
+ deserialize_value_ref_number!(deserialize_i16);
+ deserialize_value_ref_number!(deserialize_i32);
+ deserialize_value_ref_number!(deserialize_i64);
+ deserialize_number!(deserialize_i128);
+ deserialize_value_ref_number!(deserialize_u8);
+ deserialize_value_ref_number!(deserialize_u16);
+ deserialize_value_ref_number!(deserialize_u32);
+ deserialize_value_ref_number!(deserialize_u64);
+ deserialize_number!(deserialize_u128);
+ deserialize_value_ref_number!(deserialize_f32);
+ deserialize_value_ref_number!(deserialize_f64);
+
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match *self {
+ Value::Null => visitor.visit_none(),
+ _ => visitor.visit_some(self),
+ }
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ _name: &str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ let (variant, value) = match self {
+ Value::Object(value) => {
+ let mut iter = value.into_iter();
+ let (variant, value) = match iter.next() {
+ Some(v) => v,
+ None => {
+ return Err(serde::de::Error::invalid_value(
+ Unexpected::Map,
+ &"map with a single key",
+ ));
+ }
+ };
+ // enums are encoded in json as maps with a single key:value pair
+ if iter.next().is_some() {
+ return Err(serde::de::Error::invalid_value(
+ Unexpected::Map,
+ &"map with a single key",
+ ));
+ }
+ (variant, Some(value))
+ }
+ Value::String(variant) => (variant, None),
+ other => {
+ return Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"string or map",
+ ));
+ }
+ };
+
+ visitor.visit_enum(EnumRefDeserializer { variant, value })
+ }
+
+ #[inline]
+ fn deserialize_newtype_struct<V>(
+ self,
+ name: &'static str,
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ #[cfg(feature = "raw_value")]
+ {
+ if name == crate::raw::TOKEN {
+ return visitor.visit_map(crate::raw::OwnedRawDeserializer {
+ raw_value: Some(self.to_string()),
+ });
+ }
+ }
+
+ let _ = name;
+ visitor.visit_newtype_struct(self)
+ }
+
+ fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match *self {
+ Value::Bool(v) => visitor.visit_bool(v),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::String(v) => visitor.visit_borrowed_str(v),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::String(v) => visitor.visit_borrowed_str(v),
+ Value::Array(v) => visit_array_ref(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_bytes(visitor)
+ }
+
+ fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match *self {
+ Value::Null => visitor.visit_unit(),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_unit(visitor)
+ }
+
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Array(v) => visit_array_ref(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_tuple_struct<V>(
+ self,
+ _name: &'static str,
+ _len: usize,
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Object(v) => visit_object_ref(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_struct<V>(
+ self,
+ _name: &'static str,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self {
+ Value::Array(v) => visit_array_ref(v, visitor),
+ Value::Object(v) => visit_object_ref(v, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ visitor.visit_unit()
+ }
+}
+
+struct EnumRefDeserializer<'de> {
+ variant: &'de str,
+ value: Option<&'de Value>,
+}
+
+impl<'de> EnumAccess<'de> for EnumRefDeserializer<'de> {
+ type Error = Error;
+ type Variant = VariantRefDeserializer<'de>;
+
+ fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Error>
+ where
+ V: DeserializeSeed<'de>,
+ {
+ let variant = self.variant.into_deserializer();
+ let visitor = VariantRefDeserializer { value: self.value };
+ seed.deserialize(variant).map(|v| (v, visitor))
+ }
+}
+
+struct VariantRefDeserializer<'de> {
+ value: Option<&'de Value>,
+}
+
+impl<'de> VariantAccess<'de> for VariantRefDeserializer<'de> {
+ type Error = Error;
+
+ fn unit_variant(self) -> Result<(), Error> {
+ match self.value {
+ Some(value) => Deserialize::deserialize(value),
+ None => Ok(()),
+ }
+ }
+
+ fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.value {
+ Some(value) => seed.deserialize(value),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"newtype variant",
+ )),
+ }
+ }
+
+ fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self.value {
+ Some(Value::Array(v)) => {
+ if v.is_empty() {
+ visitor.visit_unit()
+ } else {
+ visit_array_ref(v, visitor)
+ }
+ }
+ Some(other) => Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"tuple variant",
+ )),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"tuple variant",
+ )),
+ }
+ }
+
+ fn struct_variant<V>(
+ self,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match self.value {
+ Some(Value::Object(v)) => visit_object_ref(v, visitor),
+ Some(other) => Err(serde::de::Error::invalid_type(
+ other.unexpected(),
+ &"struct variant",
+ )),
+ None => Err(serde::de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"struct variant",
+ )),
+ }
+ }
+}
+
+struct SeqRefDeserializer<'de> {
+ iter: slice::Iter<'de, Value>,
+}
+
+impl<'de> SeqRefDeserializer<'de> {
+ fn new(slice: &'de [Value]) -> Self {
+ SeqRefDeserializer { iter: slice.iter() }
+ }
+}
+
+impl<'de> SeqAccess<'de> for SeqRefDeserializer<'de> {
+ type Error = Error;
+
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.iter.next() {
+ Some(value) => seed.deserialize(value).map(Some),
+ None => Ok(None),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ match self.iter.size_hint() {
+ (lower, Some(upper)) if lower == upper => Some(upper),
+ _ => None,
+ }
+ }
+}
+
+struct MapRefDeserializer<'de> {
+ iter: <&'de Map<String, Value> as IntoIterator>::IntoIter,
+ value: Option<&'de Value>,
+}
+
+impl<'de> MapRefDeserializer<'de> {
+ fn new(map: &'de Map<String, Value>) -> Self {
+ MapRefDeserializer {
+ iter: map.into_iter(),
+ value: None,
+ }
+ }
+}
+
+impl<'de> MapAccess<'de> for MapRefDeserializer<'de> {
+ type Error = Error;
+
+ fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.iter.next() {
+ Some((key, value)) => {
+ self.value = Some(value);
+ let key_de = MapKeyDeserializer {
+ key: Cow::Borrowed(&**key),
+ };
+ seed.deserialize(key_de).map(Some)
+ }
+ None => Ok(None),
+ }
+ }
+
+ fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Error>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ match self.value.take() {
+ Some(value) => seed.deserialize(value),
+ None => Err(serde::de::Error::custom("value is missing")),
+ }
+ }
+
+ fn size_hint(&self) -> Option<usize> {
+ match self.iter.size_hint() {
+ (lower, Some(upper)) if lower == upper => Some(upper),
+ _ => None,
+ }
+ }
+}
+
+struct MapKeyDeserializer<'de> {
+ key: Cow<'de, str>,
+}
+
+macro_rules! deserialize_integer_key {
+ ($method:ident => $visit:ident) => {
+ fn $method<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ match (self.key.parse(), self.key) {
+ (Ok(integer), _) => visitor.$visit(integer),
+ (Err(_), Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ (Err(_), Cow::Owned(s)) => visitor.visit_string(s),
+ }
+ }
+ };
+}
+
+impl<'de> serde::Deserializer<'de> for MapKeyDeserializer<'de> {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ BorrowedCowStrDeserializer::new(self.key).deserialize_any(visitor)
+ }
+
+ deserialize_integer_key!(deserialize_i8 => visit_i8);
+ deserialize_integer_key!(deserialize_i16 => visit_i16);
+ deserialize_integer_key!(deserialize_i32 => visit_i32);
+ deserialize_integer_key!(deserialize_i64 => visit_i64);
+ deserialize_integer_key!(deserialize_i128 => visit_i128);
+ deserialize_integer_key!(deserialize_u8 => visit_u8);
+ deserialize_integer_key!(deserialize_u16 => visit_u16);
+ deserialize_integer_key!(deserialize_u32 => visit_u32);
+ deserialize_integer_key!(deserialize_u64 => visit_u64);
+ deserialize_integer_key!(deserialize_u128 => visit_u128);
+
+ #[inline]
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ // Map keys cannot be null.
+ visitor.visit_some(self)
+ }
+
+ #[inline]
+ fn deserialize_newtype_struct<V>(
+ self,
+ _name: &'static str,
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ visitor.visit_newtype_struct(self)
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ name: &'static str,
+ variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.key
+ .into_deserializer()
+ .deserialize_enum(name, variants, visitor)
+ }
+
+ forward_to_deserialize_any! {
+ bool f32 f64 char str string bytes byte_buf unit unit_struct seq tuple
+ tuple_struct map struct identifier ignored_any
+ }
+}
+
+struct KeyClassifier;
+
+enum KeyClass {
+ Map(String),
+ #[cfg(feature = "arbitrary_precision")]
+ Number,
+ #[cfg(feature = "raw_value")]
+ RawValue,
+}
+
+impl<'de> DeserializeSeed<'de> for KeyClassifier {
+ type Value = KeyClass;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ deserializer.deserialize_str(self)
+ }
+}
+
+impl<'de> Visitor<'de> for KeyClassifier {
+ type Value = KeyClass;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("a string key")
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ match s {
+ #[cfg(feature = "arbitrary_precision")]
+ crate::number::TOKEN => Ok(KeyClass::Number),
+ #[cfg(feature = "raw_value")]
+ crate::raw::TOKEN => Ok(KeyClass::RawValue),
+ _ => Ok(KeyClass::Map(s.to_owned())),
+ }
+ }
+
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
+ where
+ E: de::Error,
+ {
+ match s.as_str() {
+ #[cfg(feature = "arbitrary_precision")]
+ crate::number::TOKEN => Ok(KeyClass::Number),
+ #[cfg(feature = "raw_value")]
+ crate::raw::TOKEN => Ok(KeyClass::RawValue),
+ _ => Ok(KeyClass::Map(s)),
+ }
+ }
+}
+
+impl Value {
+ #[cold]
+ fn invalid_type<E>(&self, exp: &dyn Expected) -> E
+ where
+ E: serde::de::Error,
+ {
+ serde::de::Error::invalid_type(self.unexpected(), exp)
+ }
+
+ #[cold]
+ fn unexpected(&self) -> Unexpected {
+ match self {
+ Value::Null => Unexpected::Unit,
+ Value::Bool(b) => Unexpected::Bool(*b),
+ Value::Number(n) => n.unexpected(),
+ Value::String(s) => Unexpected::Str(s),
+ Value::Array(_) => Unexpected::Seq,
+ Value::Object(_) => Unexpected::Map,
+ }
+ }
+}
+
+struct BorrowedCowStrDeserializer<'de> {
+ value: Cow<'de, str>,
+}
+
+impl<'de> BorrowedCowStrDeserializer<'de> {
+ fn new(value: Cow<'de, str>) -> Self {
+ BorrowedCowStrDeserializer { value }
+ }
+}
+
+impl<'de> de::Deserializer<'de> for BorrowedCowStrDeserializer<'de> {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ match self.value {
+ Cow::Borrowed(string) => visitor.visit_borrowed_str(string),
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ Cow::Owned(string) => visitor.visit_string(string),
+ }
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ _name: &str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ visitor.visit_enum(self)
+ }
+
+ forward_to_deserialize_any! {
+ bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+ bytes byte_buf option unit unit_struct newtype_struct seq tuple
+ tuple_struct map struct identifier ignored_any
+ }
+}
+
+impl<'de> de::EnumAccess<'de> for BorrowedCowStrDeserializer<'de> {
+ type Error = Error;
+ type Variant = UnitOnly;
+
+ fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ let value = seed.deserialize(self)?;
+ Ok((value, UnitOnly))
+ }
+}
+
+struct UnitOnly;
+
+impl<'de> de::VariantAccess<'de> for UnitOnly {
+ type Error = Error;
+
+ fn unit_variant(self) -> Result<(), Error> {
+ Ok(())
+ }
+
+ fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Error>
+ where
+ T: de::DeserializeSeed<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"newtype variant",
+ ))
+ }
+
+ fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"tuple variant",
+ ))
+ }
+
+ fn struct_variant<V>(
+ self,
+ _fields: &'static [&'static str],
+ _visitor: V,
+ ) -> Result<V::Value, Error>
+ where
+ V: de::Visitor<'de>,
+ {
+ Err(de::Error::invalid_type(
+ Unexpected::UnitVariant,
+ &"struct variant",
+ ))
+ }
+}
diff --git a/third_party/rust/serde_json/src/value/from.rs b/third_party/rust/serde_json/src/value/from.rs
new file mode 100644
index 0000000000..c5a6a3960b
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/from.rs
@@ -0,0 +1,277 @@
+use super::Value;
+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;
+
+macro_rules! from_integer {
+ ($($ty:ident)*) => {
+ $(
+ impl From<$ty> for Value {
+ fn from(n: $ty) -> Self {
+ Value::Number(n.into())
+ }
+ }
+ )*
+ };
+}
+
+from_integer! {
+ i8 i16 i32 i64 isize
+ u8 u16 u32 u64 usize
+}
+
+#[cfg(feature = "arbitrary_precision")]
+from_integer! {
+ i128 u128
+}
+
+impl From<f32> for Value {
+ /// Convert 32-bit floating point number to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let f: f32 = 13.37;
+ /// let x: Value = f.into();
+ /// ```
+ fn from(f: f32) -> Self {
+ From::from(f as f64)
+ }
+}
+
+impl From<f64> for Value {
+ /// Convert 64-bit floating point number to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let f: f64 = 13.37;
+ /// let x: Value = f.into();
+ /// ```
+ fn from(f: f64) -> Self {
+ Number::from_f64(f).map_or(Value::Null, Value::Number)
+ }
+}
+
+impl From<bool> for Value {
+ /// Convert boolean to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let b = false;
+ /// let x: Value = b.into();
+ /// ```
+ fn from(f: bool) -> Self {
+ Value::Bool(f)
+ }
+}
+
+impl From<String> for Value {
+ /// Convert `String` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let s: String = "lorem".to_string();
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: String) -> Self {
+ Value::String(f)
+ }
+}
+
+impl<'a> From<&'a str> for Value {
+ /// Convert string slice to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let s: &str = "lorem";
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: &str) -> Self {
+ Value::String(f.to_string())
+ }
+}
+
+impl<'a> From<Cow<'a, str>> for Value {
+ /// Convert copy-on-write string to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ /// use std::borrow::Cow;
+ ///
+ /// let s: Cow<str> = Cow::Borrowed("lorem");
+ /// let x: Value = s.into();
+ /// ```
+ ///
+ /// ```
+ /// use serde_json::Value;
+ /// use std::borrow::Cow;
+ ///
+ /// let s: Cow<str> = Cow::Owned("lorem".to_string());
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: Cow<'a, str>) -> Self {
+ Value::String(f.into_owned())
+ }
+}
+
+impl From<Number> for Value {
+ /// Convert `Number` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::{Number, Value};
+ ///
+ /// let n = Number::from(7);
+ /// let x: Value = n.into();
+ /// ```
+ fn from(f: Number) -> Self {
+ Value::Number(f)
+ }
+}
+
+impl From<Map<String, Value>> for Value {
+ /// Convert map (with string keys) to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::{Map, Value};
+ ///
+ /// let mut m = Map::new();
+ /// m.insert("Lorem".to_string(), "ipsum".into());
+ /// let x: Value = m.into();
+ /// ```
+ fn from(f: Map<String, Value>) -> Self {
+ Value::Object(f)
+ }
+}
+
+impl<T: Into<Value>> From<Vec<T>> for Value {
+ /// Convert a `Vec` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let v = vec!["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into();
+ /// ```
+ fn from(f: Vec<T>) -> Self {
+ Value::Array(f.into_iter().map(Into::into).collect())
+ }
+}
+
+impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
+ /// Convert a slice to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into();
+ /// ```
+ fn from(f: &'a [T]) -> Self {
+ Value::Array(f.iter().cloned().map(Into::into).collect())
+ }
+}
+
+impl<T: Into<Value>> FromIterator<T> for Value {
+ /// Convert an iteratable type to a `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let v = std::iter::repeat(42).take(5);
+ /// let x: Value = v.collect();
+ /// ```
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into_iter().collect();
+ /// ```
+ ///
+ /// ```
+ /// use std::iter::FromIterator;
+ /// use serde_json::Value;
+ ///
+ /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
+ /// ```
+ fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
+ Value::Array(iter.into_iter().map(Into::into).collect())
+ }
+}
+
+impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Value {
+ /// Convert an iteratable type to a `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)];
+ /// let x: Value = v.into_iter().collect();
+ /// ```
+ fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
+ Value::Object(
+ iter.into_iter()
+ .map(|(k, v)| (k.into(), v.into()))
+ .collect(),
+ )
+ }
+}
+
+impl From<()> for Value {
+ /// Convert `()` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// let u = ();
+ /// let x: Value = u.into();
+ /// ```
+ fn from((): ()) -> Self {
+ Value::Null
+ }
+}
+
+impl<T> From<Option<T>> for Value
+where
+ T: Into<Value>,
+{
+ fn from(opt: Option<T>) -> Self {
+ match opt {
+ None => Value::Null,
+ Some(value) => Into::into(value),
+ }
+ }
+}
diff --git a/third_party/rust/serde_json/src/value/index.rs b/third_party/rust/serde_json/src/value/index.rs
new file mode 100644
index 0000000000..c74042b75a
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/index.rs
@@ -0,0 +1,258 @@
+use super::Value;
+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`.
+///
+/// The [`get`] and [`get_mut`] methods of `Value` accept any type that
+/// implements `Index`, as does the [square-bracket indexing operator]. This
+/// trait is implemented for strings which are used as the index into a JSON
+/// map, and for `usize` which is used as the index into a JSON array.
+///
+/// [`get`]: ../enum.Value.html#method.get
+/// [`get_mut`]: ../enum.Value.html#method.get_mut
+/// [square-bracket indexing operator]: ../enum.Value.html#impl-Index%3CI%3E
+///
+/// This trait is sealed and cannot be implemented for types outside of
+/// `serde_json`.
+///
+/// # Examples
+///
+/// ```
+/// # use serde_json::json;
+/// #
+/// let data = json!({ "inner": [1, 2, 3] });
+///
+/// // Data is a JSON map so it can be indexed with a string.
+/// let inner = &data["inner"];
+///
+/// // Inner is a JSON array so it can be indexed with an integer.
+/// let first = &inner[0];
+///
+/// assert_eq!(first, 1);
+/// ```
+pub trait Index: private::Sealed {
+ /// Return None if the key is not already in the array or object.
+ #[doc(hidden)]
+ fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>;
+
+ /// Return None if the key is not already in the array or object.
+ #[doc(hidden)]
+ fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value>;
+
+ /// Panic if array index out of bounds. If key is not already in the object,
+ /// insert it with a value of null. Panic if Value is a type that cannot be
+ /// indexed into, except if Value is null then it can be treated as an empty
+ /// object.
+ #[doc(hidden)]
+ fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value;
+}
+
+impl Index for usize {
+ fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
+ match v {
+ Value::Array(vec) => vec.get(*self),
+ _ => None,
+ }
+ }
+ fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
+ match v {
+ Value::Array(vec) => vec.get_mut(*self),
+ _ => None,
+ }
+ }
+ fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
+ match v {
+ Value::Array(vec) => {
+ let len = vec.len();
+ vec.get_mut(*self).unwrap_or_else(|| {
+ panic!(
+ "cannot access index {} of JSON array of length {}",
+ self, len
+ )
+ })
+ }
+ _ => panic!("cannot access index {} of JSON {}", self, Type(v)),
+ }
+ }
+}
+
+impl Index for str {
+ fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
+ match v {
+ Value::Object(map) => map.get(self),
+ _ => None,
+ }
+ }
+ fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
+ match v {
+ Value::Object(map) => map.get_mut(self),
+ _ => None,
+ }
+ }
+ fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
+ if let Value::Null = v {
+ *v = Value::Object(Map::new());
+ }
+ match v {
+ Value::Object(map) => map.entry(self.to_owned()).or_insert(Value::Null),
+ _ => panic!("cannot access key {:?} in JSON {}", self, Type(v)),
+ }
+ }
+}
+
+impl Index for String {
+ fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
+ self[..].index_into(v)
+ }
+ fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
+ self[..].index_into_mut(v)
+ }
+ fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
+ self[..].index_or_insert(v)
+ }
+}
+
+impl<'a, T> Index for &'a T
+where
+ T: ?Sized + Index,
+{
+ fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
+ (**self).index_into(v)
+ }
+ fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
+ (**self).index_into_mut(v)
+ }
+ fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
+ (**self).index_or_insert(v)
+ }
+}
+
+// Prevent users from implementing the Index trait.
+mod private {
+ pub trait Sealed {}
+ impl Sealed for usize {}
+ impl Sealed for str {}
+ 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> Display for Type<'a> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match *self.0 {
+ Value::Null => formatter.write_str("null"),
+ Value::Bool(_) => formatter.write_str("boolean"),
+ Value::Number(_) => formatter.write_str("number"),
+ Value::String(_) => formatter.write_str("string"),
+ Value::Array(_) => formatter.write_str("array"),
+ Value::Object(_) => formatter.write_str("object"),
+ }
+ }
+}
+
+// The usual semantics of Index is to panic on invalid indexing.
+//
+// That said, the usual semantics are for things like Vec and BTreeMap which
+// have different use cases than Value. If you are working with a Vec, you know
+// that you are working with a Vec and you can get the len of the Vec and make
+// sure your indices are within bounds. The Value use cases are more
+// loosey-goosey. You got some JSON from an endpoint and you want to pull values
+// out of it. Outside of this Index impl, you already have the option of using
+// value.as_array() and working with the Vec directly, or matching on
+// Value::Array and getting the Vec directly. The Index impl means you can skip
+// that and index directly into the thing using a concise syntax. You don't have
+// to check the type, you don't have to check the len, it is all about what you
+// expect the Value to look like.
+//
+// Basically the use cases that would be well served by panicking here are
+// better served by using one of the other approaches: get and get_mut,
+// as_array, or match. The value of this impl is that it adds a way of working
+// with Value that is not well served by the existing approaches: concise and
+// careless and sometimes that is exactly what you want.
+impl<I> ops::Index<I> for Value
+where
+ I: Index,
+{
+ type Output = Value;
+
+ /// Index into a `serde_json::Value` using the syntax `value[0]` or
+ /// `value["k"]`.
+ ///
+ /// Returns `Value::Null` if the type of `self` does not match the type of
+ /// the index, for example if the index is a string and `self` is an array
+ /// or a number. Also returns `Value::Null` if the given key does not exist
+ /// in the map or the given index is not within the bounds of the array.
+ ///
+ /// For retrieving deeply nested values, you should have a look at the
+ /// `Value::pointer` method.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let data = json!({
+ /// "x": {
+ /// "y": ["z", "zz"]
+ /// }
+ /// });
+ ///
+ /// assert_eq!(data["x"]["y"], json!(["z", "zz"]));
+ /// assert_eq!(data["x"]["y"][0], json!("z"));
+ ///
+ /// assert_eq!(data["a"], json!(null)); // returns null for undefined values
+ /// assert_eq!(data["a"]["b"], json!(null)); // does not panic
+ /// ```
+ fn index(&self, index: I) -> &Value {
+ static NULL: Value = Value::Null;
+ index.index_into(self).unwrap_or(&NULL)
+ }
+}
+
+impl<I> ops::IndexMut<I> for Value
+where
+ I: Index,
+{
+ /// Write into a `serde_json::Value` using the syntax `value[0] = ...` or
+ /// `value["k"] = ...`.
+ ///
+ /// If the index is a number, the value must be an array of length bigger
+ /// than the index. Indexing into a value that is not an array or an array
+ /// that is too small will panic.
+ ///
+ /// If the index is a string, the value must be an object or null which is
+ /// treated like an empty object. If the key is not already present in the
+ /// object, it will be inserted with a value of null. Indexing into a value
+ /// that is neither an object nor null will panic.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut data = json!({ "x": 0 });
+ ///
+ /// // replace an existing key
+ /// data["x"] = json!(1);
+ ///
+ /// // insert a new key
+ /// data["y"] = json!([false, false, false]);
+ ///
+ /// // replace an array value
+ /// data["y"][0] = json!(true);
+ ///
+ /// // inserted a deeply nested key
+ /// data["a"]["b"]["c"]["d"] = json!(true);
+ ///
+ /// println!("{}", data);
+ /// ```
+ fn index_mut(&mut self, index: I) -> &mut Value {
+ index.index_or_insert(self)
+ }
+}
diff --git a/third_party/rust/serde_json/src/value/mod.rs b/third_party/rust/serde_json/src/value/mod.rs
new file mode 100644
index 0000000000..c467df6ccc
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/mod.rs
@@ -0,0 +1,987 @@
+//! The Value enum, a loosely typed way of representing any valid JSON value.
+//!
+//! # Constructing JSON
+//!
+//! Serde JSON provides a [`json!` macro][macro] to build `serde_json::Value`
+//! objects with very natural JSON syntax.
+//!
+//! ```
+//! use serde_json::json;
+//!
+//! fn main() {
+//! // The type of `john` is `serde_json::Value`
+//! let john = json!({
+//! "name": "John Doe",
+//! "age": 43,
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! });
+//!
+//! println!("first phone number: {}", john["phones"][0]);
+//!
+//! // Convert to a string of JSON and print it out
+//! println!("{}", john.to_string());
+//! }
+//! ```
+//!
+//! The `Value::to_string()` function converts a `serde_json::Value` into a
+//! `String` of JSON text.
+//!
+//! One neat thing about the `json!` macro is that variables and expressions can
+//! be interpolated directly into the JSON value as you are building it. Serde
+//! will check at compile time that the value you are interpolating is able to
+//! be represented as JSON.
+//!
+//! ```
+//! # use serde_json::json;
+//! #
+//! # fn random_phone() -> u16 { 0 }
+//! #
+//! let full_name = "John Doe";
+//! let age_last_year = 42;
+//!
+//! // The type of `john` is `serde_json::Value`
+//! let john = json!({
+//! "name": full_name,
+//! "age": age_last_year + 1,
+//! "phones": [
+//! format!("+44 {}", random_phone())
+//! ]
+//! });
+//! ```
+//!
+//! A string of JSON data can be parsed into a `serde_json::Value` by the
+//! [`serde_json::from_str`][from_str] function. There is also
+//! [`from_slice`][from_slice] for parsing from a byte slice `&[u8]` and
+//! [`from_reader`][from_reader] for parsing from any `io::Read` like a File or
+//! a TCP stream.
+//!
+//! ```
+//! use serde_json::{json, Value, Error};
+//!
+//! fn untyped_example() -> Result<(), Error> {
+//! // Some JSON input data as a &str. Maybe this comes from the user.
+//! let data = r#"
+//! {
+//! "name": "John Doe",
+//! "age": 43,
+//! "phones": [
+//! "+44 1234567",
+//! "+44 2345678"
+//! ]
+//! }"#;
+//!
+//! // Parse the string of data into serde_json::Value.
+//! let v: Value = serde_json::from_str(data)?;
+//!
+//! // Access parts of the data by indexing with square brackets.
+//! println!("Please call {} at the number {}", v["name"], v["phones"][0]);
+//!
+//! Ok(())
+//! }
+//! #
+//! # untyped_example().unwrap();
+//! ```
+//!
+//! [macro]: https://docs.serde.rs/serde_json/macro.json.html
+//! [from_str]: https://docs.serde.rs/serde_json/de/fn.from_str.html
+//! [from_slice]: https://docs.serde.rs/serde_json/de/fn.from_slice.html
+//! [from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html
+
+use crate::error::Error;
+use crate::io;
+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;
+
+pub use self::index::Index;
+pub use self::ser::Serializer;
+pub use crate::map::Map;
+pub use crate::number::Number;
+
+#[cfg(feature = "raw_value")]
+pub use crate::raw::{to_raw_value, RawValue};
+
+/// Represents any valid JSON value.
+///
+/// See the [`serde_json::value` module documentation](self) for usage examples.
+#[derive(Clone, Eq, PartialEq)]
+pub enum Value {
+ /// Represents a JSON null value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!(null);
+ /// ```
+ Null,
+
+ /// Represents a JSON boolean.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!(true);
+ /// ```
+ Bool(bool),
+
+ /// Represents a JSON number, whether integer or floating point.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!(12.5);
+ /// ```
+ Number(Number),
+
+ /// Represents a JSON string.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!("a string");
+ /// ```
+ String(String),
+
+ /// Represents a JSON array.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!(["an", "array"]);
+ /// ```
+ Array(Vec<Value>),
+
+ /// Represents a JSON object.
+ ///
+ /// By default the map is backed by a BTreeMap. Enable the `preserve_order`
+ /// feature of serde_json to use IndexMap instead, which preserves
+ /// entries in the order they are inserted into the map. In particular, this
+ /// allows JSON data to be deserialized into a Value and serialized to a
+ /// string while retaining the order of map keys in the input.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "an": "object" });
+ /// ```
+ Object(Map<String, Value>),
+}
+
+impl Debug for Value {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Value::Null => formatter.write_str("Null"),
+ Value::Bool(boolean) => write!(formatter, "Bool({})", boolean),
+ Value::Number(number) => Debug::fmt(number, formatter),
+ Value::String(string) => write!(formatter, "String({:?})", string),
+ Value::Array(vec) => {
+ formatter.write_str("Array ")?;
+ Debug::fmt(vec, formatter)
+ }
+ Value::Object(map) => {
+ formatter.write_str("Object ")?;
+ Debug::fmt(map, formatter)
+ }
+ }
+ }
+}
+
+impl Display for Value {
+ /// Display a JSON value as a string.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let json = json!({ "city": "London", "street": "10 Downing Street" });
+ ///
+ /// // Compact format:
+ /// //
+ /// // {"city":"London","street":"10 Downing Street"}
+ /// let compact = format!("{}", json);
+ /// assert_eq!(compact,
+ /// "{\"city\":\"London\",\"street\":\"10 Downing Street\"}");
+ ///
+ /// // Pretty format:
+ /// //
+ /// // {
+ /// // "city": "London",
+ /// // "street": "10 Downing Street"
+ /// // }
+ /// let pretty = format!("{:#}", json);
+ /// assert_eq!(pretty,
+ /// "{\n \"city\": \"London\",\n \"street\": \"10 Downing Street\"\n}");
+ /// ```
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ struct WriterFormatter<'a, 'b: 'a> {
+ inner: &'a mut fmt::Formatter<'b>,
+ }
+
+ impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ // Safety: the serializer below only emits valid utf8 when using
+ // the default formatter.
+ let s = unsafe { str::from_utf8_unchecked(buf) };
+ tri!(self.inner.write_str(s).map_err(io_error));
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ Ok(())
+ }
+ }
+
+ fn io_error(_: fmt::Error) -> io::Error {
+ // Error value does not matter because Display impl just maps it
+ // back to fmt::Error.
+ io::Error::new(io::ErrorKind::Other, "fmt error")
+ }
+
+ let alternate = f.alternate();
+ let mut wr = WriterFormatter { inner: f };
+ if alternate {
+ // {:#}
+ super::ser::to_writer_pretty(&mut wr, self).map_err(|_| fmt::Error)
+ } else {
+ // {}
+ super::ser::to_writer(&mut wr, self).map_err(|_| fmt::Error)
+ }
+ }
+}
+
+fn parse_index(s: &str) -> Option<usize> {
+ if s.starts_with('+') || (s.starts_with('0') && s.len() != 1) {
+ return None;
+ }
+ s.parse().ok()
+}
+
+impl Value {
+ /// Index into a JSON array or map. A string index can be used to access a
+ /// value in a map, and a usize index can be used to access an element of an
+ /// array.
+ ///
+ /// Returns `None` if the type of `self` does not match the type of the
+ /// index, for example if the index is a string and `self` is an array or a
+ /// number. Also returns `None` if the given key does not exist in the map
+ /// or the given index is not within the bounds of the array.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let object = json!({ "A": 65, "B": 66, "C": 67 });
+ /// assert_eq!(*object.get("A").unwrap(), json!(65));
+ ///
+ /// let array = json!([ "A", "B", "C" ]);
+ /// assert_eq!(*array.get(2).unwrap(), json!("C"));
+ ///
+ /// assert_eq!(array.get("A"), None);
+ /// ```
+ ///
+ /// Square brackets can also be used to index into a value in a more concise
+ /// way. This returns `Value::Null` in cases where `get` would have returned
+ /// `None`.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let object = json!({
+ /// "A": ["a", "á", "à"],
+ /// "B": ["b", "b́"],
+ /// "C": ["c", "ć", "ć̣", "ḉ"],
+ /// });
+ /// assert_eq!(object["B"][0], json!("b"));
+ ///
+ /// assert_eq!(object["D"], json!(null));
+ /// assert_eq!(object[0]["x"]["y"]["z"], json!(null));
+ /// ```
+ pub fn get<I: Index>(&self, index: I) -> Option<&Value> {
+ index.index_into(self)
+ }
+
+ /// Mutably index into a JSON array or map. A string index can be used to
+ /// access a value in a map, and a usize index can be used to access an
+ /// element of an array.
+ ///
+ /// Returns `None` if the type of `self` does not match the type of the
+ /// index, for example if the index is a string and `self` is an array or a
+ /// number. Also returns `None` if the given key does not exist in the map
+ /// or the given index is not within the bounds of the array.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut object = json!({ "A": 65, "B": 66, "C": 67 });
+ /// *object.get_mut("A").unwrap() = json!(69);
+ ///
+ /// let mut array = json!([ "A", "B", "C" ]);
+ /// *array.get_mut(2).unwrap() = json!("D");
+ /// ```
+ pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Value> {
+ index.index_into_mut(self)
+ }
+
+ /// Returns true if the `Value` is an Object. Returns false otherwise.
+ ///
+ /// For any Value on which `is_object` returns true, `as_object` and
+ /// `as_object_mut` are guaranteed to return the map representation of the
+ /// object.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let obj = json!({ "a": { "nested": true }, "b": ["an", "array"] });
+ ///
+ /// assert!(obj.is_object());
+ /// assert!(obj["a"].is_object());
+ ///
+ /// // array, not an object
+ /// assert!(!obj["b"].is_object());
+ /// ```
+ pub fn is_object(&self) -> bool {
+ self.as_object().is_some()
+ }
+
+ /// If the `Value` is an Object, returns the associated Map. Returns None
+ /// otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": { "nested": true }, "b": ["an", "array"] });
+ ///
+ /// // The length of `{"nested": true}` is 1 entry.
+ /// assert_eq!(v["a"].as_object().unwrap().len(), 1);
+ ///
+ /// // The array `["an", "array"]` is not an object.
+ /// assert_eq!(v["b"].as_object(), None);
+ /// ```
+ pub fn as_object(&self) -> Option<&Map<String, Value>> {
+ match self {
+ Value::Object(map) => Some(map),
+ _ => None,
+ }
+ }
+
+ /// If the `Value` is an Object, returns the associated mutable Map.
+ /// Returns None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut v = json!({ "a": { "nested": true } });
+ ///
+ /// v["a"].as_object_mut().unwrap().clear();
+ /// assert_eq!(v, json!({ "a": {} }));
+ /// ```
+ pub fn as_object_mut(&mut self) -> Option<&mut Map<String, Value>> {
+ match self {
+ Value::Object(map) => Some(map),
+ _ => None,
+ }
+ }
+
+ /// Returns true if the `Value` is an Array. Returns false otherwise.
+ ///
+ /// For any Value on which `is_array` returns true, `as_array` and
+ /// `as_array_mut` are guaranteed to return the vector representing the
+ /// array.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let obj = json!({ "a": ["an", "array"], "b": { "an": "object" } });
+ ///
+ /// assert!(obj["a"].is_array());
+ ///
+ /// // an object, not an array
+ /// assert!(!obj["b"].is_array());
+ /// ```
+ pub fn is_array(&self) -> bool {
+ self.as_array().is_some()
+ }
+
+ /// If the `Value` is an Array, returns the associated vector. Returns None
+ /// otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": ["an", "array"], "b": { "an": "object" } });
+ ///
+ /// // The length of `["an", "array"]` is 2 elements.
+ /// assert_eq!(v["a"].as_array().unwrap().len(), 2);
+ ///
+ /// // The object `{"an": "object"}` is not an array.
+ /// assert_eq!(v["b"].as_array(), None);
+ /// ```
+ pub fn as_array(&self) -> Option<&Vec<Value>> {
+ match self {
+ Value::Array(array) => Some(array),
+ _ => None,
+ }
+ }
+
+ /// If the `Value` is an Array, returns the associated mutable vector.
+ /// Returns None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut v = json!({ "a": ["an", "array"] });
+ ///
+ /// v["a"].as_array_mut().unwrap().clear();
+ /// assert_eq!(v, json!({ "a": [] }));
+ /// ```
+ pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>> {
+ match self {
+ Value::Array(list) => Some(list),
+ _ => None,
+ }
+ }
+
+ /// Returns true if the `Value` is a String. Returns false otherwise.
+ ///
+ /// For any Value on which `is_string` returns true, `as_str` is guaranteed
+ /// to return the string slice.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": "some string", "b": false });
+ ///
+ /// assert!(v["a"].is_string());
+ ///
+ /// // The boolean `false` is not a string.
+ /// assert!(!v["b"].is_string());
+ /// ```
+ pub fn is_string(&self) -> bool {
+ self.as_str().is_some()
+ }
+
+ /// If the `Value` is a String, returns the associated str. Returns None
+ /// otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": "some string", "b": false });
+ ///
+ /// assert_eq!(v["a"].as_str(), Some("some string"));
+ ///
+ /// // The boolean `false` is not a string.
+ /// assert_eq!(v["b"].as_str(), None);
+ ///
+ /// // JSON values are printed in JSON representation, so strings are in quotes.
+ /// //
+ /// // The value is: "some string"
+ /// println!("The value is: {}", v["a"]);
+ ///
+ /// // Rust strings are printed without quotes.
+ /// //
+ /// // The value is: some string
+ /// println!("The value is: {}", v["a"].as_str().unwrap());
+ /// ```
+ pub fn as_str(&self) -> Option<&str> {
+ match self {
+ Value::String(s) => Some(s),
+ _ => None,
+ }
+ }
+
+ /// Returns true if the `Value` is a Number. Returns false otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 1, "b": "2" });
+ ///
+ /// assert!(v["a"].is_number());
+ ///
+ /// // The string `"2"` is a string, not a number.
+ /// assert!(!v["b"].is_number());
+ /// ```
+ pub fn is_number(&self) -> bool {
+ match *self {
+ Value::Number(_) => true,
+ _ => false,
+ }
+ }
+
+ /// Returns true if the `Value` is an integer between `i64::MIN` and
+ /// `i64::MAX`.
+ ///
+ /// For any Value on which `is_i64` returns true, `as_i64` is guaranteed to
+ /// return the integer value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let big = i64::max_value() as u64 + 10;
+ /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
+ ///
+ /// assert!(v["a"].is_i64());
+ ///
+ /// // Greater than i64::MAX.
+ /// assert!(!v["b"].is_i64());
+ ///
+ /// // Numbers with a decimal point are not considered integers.
+ /// assert!(!v["c"].is_i64());
+ /// ```
+ pub fn is_i64(&self) -> bool {
+ match self {
+ Value::Number(n) => n.is_i64(),
+ _ => false,
+ }
+ }
+
+ /// Returns true if the `Value` is an integer between zero and `u64::MAX`.
+ ///
+ /// For any Value on which `is_u64` returns true, `as_u64` is guaranteed to
+ /// return the integer value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
+ ///
+ /// assert!(v["a"].is_u64());
+ ///
+ /// // Negative integer.
+ /// assert!(!v["b"].is_u64());
+ ///
+ /// // Numbers with a decimal point are not considered integers.
+ /// assert!(!v["c"].is_u64());
+ /// ```
+ pub fn is_u64(&self) -> bool {
+ match self {
+ Value::Number(n) => n.is_u64(),
+ _ => false,
+ }
+ }
+
+ /// Returns true if the `Value` is a number that can be represented by f64.
+ ///
+ /// For any Value on which `is_f64` returns true, `as_f64` is guaranteed to
+ /// return the floating point value.
+ ///
+ /// Currently this function returns true if and only if both `is_i64` and
+ /// `is_u64` return false but this is not a guarantee in the future.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
+ ///
+ /// assert!(v["a"].is_f64());
+ ///
+ /// // Integers.
+ /// assert!(!v["b"].is_f64());
+ /// assert!(!v["c"].is_f64());
+ /// ```
+ pub fn is_f64(&self) -> bool {
+ match self {
+ Value::Number(n) => n.is_f64(),
+ _ => false,
+ }
+ }
+
+ /// If the `Value` is an integer, represent it as i64 if possible. Returns
+ /// None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let big = i64::max_value() as u64 + 10;
+ /// let v = json!({ "a": 64, "b": big, "c": 256.0 });
+ ///
+ /// assert_eq!(v["a"].as_i64(), Some(64));
+ /// assert_eq!(v["b"].as_i64(), None);
+ /// assert_eq!(v["c"].as_i64(), None);
+ /// ```
+ pub fn as_i64(&self) -> Option<i64> {
+ match self {
+ Value::Number(n) => n.as_i64(),
+ _ => None,
+ }
+ }
+
+ /// If the `Value` is an integer, represent it as u64 if possible. Returns
+ /// None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
+ ///
+ /// assert_eq!(v["a"].as_u64(), Some(64));
+ /// assert_eq!(v["b"].as_u64(), None);
+ /// assert_eq!(v["c"].as_u64(), None);
+ /// ```
+ pub fn as_u64(&self) -> Option<u64> {
+ match self {
+ Value::Number(n) => n.as_u64(),
+ _ => None,
+ }
+ }
+
+ /// If the `Value` is a number, represent it as f64 if possible. Returns
+ /// None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
+ ///
+ /// assert_eq!(v["a"].as_f64(), Some(256.0));
+ /// assert_eq!(v["b"].as_f64(), Some(64.0));
+ /// assert_eq!(v["c"].as_f64(), Some(-64.0));
+ /// ```
+ pub fn as_f64(&self) -> Option<f64> {
+ match self {
+ Value::Number(n) => n.as_f64(),
+ _ => None,
+ }
+ }
+
+ /// Returns true if the `Value` is a Boolean. Returns false otherwise.
+ ///
+ /// For any Value on which `is_boolean` returns true, `as_bool` is
+ /// guaranteed to return the boolean value.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": false, "b": "false" });
+ ///
+ /// assert!(v["a"].is_boolean());
+ ///
+ /// // The string `"false"` is a string, not a boolean.
+ /// assert!(!v["b"].is_boolean());
+ /// ```
+ pub fn is_boolean(&self) -> bool {
+ self.as_bool().is_some()
+ }
+
+ /// If the `Value` is a Boolean, returns the associated bool. Returns None
+ /// otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": false, "b": "false" });
+ ///
+ /// assert_eq!(v["a"].as_bool(), Some(false));
+ ///
+ /// // The string `"false"` is a string, not a boolean.
+ /// assert_eq!(v["b"].as_bool(), None);
+ /// ```
+ pub fn as_bool(&self) -> Option<bool> {
+ match *self {
+ Value::Bool(b) => Some(b),
+ _ => None,
+ }
+ }
+
+ /// Returns true if the `Value` is a Null. Returns false otherwise.
+ ///
+ /// For any Value on which `is_null` returns true, `as_null` is guaranteed
+ /// to return `Some(())`.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": null, "b": false });
+ ///
+ /// assert!(v["a"].is_null());
+ ///
+ /// // The boolean `false` is not null.
+ /// assert!(!v["b"].is_null());
+ /// ```
+ pub fn is_null(&self) -> bool {
+ self.as_null().is_some()
+ }
+
+ /// If the `Value` is a Null, returns (). Returns None otherwise.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let v = json!({ "a": null, "b": false });
+ ///
+ /// assert_eq!(v["a"].as_null(), Some(()));
+ ///
+ /// // The boolean `false` is not null.
+ /// assert_eq!(v["b"].as_null(), None);
+ /// ```
+ pub fn as_null(&self) -> Option<()> {
+ match *self {
+ Value::Null => Some(()),
+ _ => None,
+ }
+ }
+
+ /// Looks up a value by a JSON Pointer.
+ ///
+ /// JSON Pointer defines a string syntax for identifying a specific value
+ /// within a JavaScript Object Notation (JSON) document.
+ ///
+ /// A Pointer is a Unicode string with the reference tokens separated by `/`.
+ /// Inside tokens `/` is replaced by `~1` and `~` is replaced by `~0`. The
+ /// addressed value is returned and if there is no such value `None` is
+ /// returned.
+ ///
+ /// For more information read [RFC6901](https://tools.ietf.org/html/rfc6901).
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let data = json!({
+ /// "x": {
+ /// "y": ["z", "zz"]
+ /// }
+ /// });
+ ///
+ /// assert_eq!(data.pointer("/x/y/1").unwrap(), &json!("zz"));
+ /// assert_eq!(data.pointer("/a/b/c"), None);
+ /// ```
+ pub fn pointer(&self, pointer: &str) -> Option<&Value> {
+ if pointer.is_empty() {
+ return Some(self);
+ }
+ if !pointer.starts_with('/') {
+ return None;
+ }
+ pointer
+ .split('/')
+ .skip(1)
+ .map(|x| x.replace("~1", "/").replace("~0", "~"))
+ .try_fold(self, |target, token| match target {
+ Value::Object(map) => map.get(&token),
+ Value::Array(list) => parse_index(&token).and_then(|x| list.get(x)),
+ _ => None,
+ })
+ }
+
+ /// Looks up a value by a JSON Pointer and returns a mutable reference to
+ /// that value.
+ ///
+ /// JSON Pointer defines a string syntax for identifying a specific value
+ /// within a JavaScript Object Notation (JSON) document.
+ ///
+ /// A Pointer is a Unicode string with the reference tokens separated by `/`.
+ /// Inside tokens `/` is replaced by `~1` and `~` is replaced by `~0`. The
+ /// addressed value is returned and if there is no such value `None` is
+ /// returned.
+ ///
+ /// For more information read [RFC6901](https://tools.ietf.org/html/rfc6901).
+ ///
+ /// # Example of Use
+ ///
+ /// ```
+ /// use serde_json::Value;
+ ///
+ /// fn main() {
+ /// let s = r#"{"x": 1.0, "y": 2.0}"#;
+ /// let mut value: Value = serde_json::from_str(s).unwrap();
+ ///
+ /// // Check value using read-only pointer
+ /// assert_eq!(value.pointer("/x"), Some(&1.0.into()));
+ /// // Change value with direct assignment
+ /// *value.pointer_mut("/x").unwrap() = 1.5.into();
+ /// // Check that new value was written
+ /// assert_eq!(value.pointer("/x"), Some(&1.5.into()));
+ /// // Or change the value only if it exists
+ /// value.pointer_mut("/x").map(|v| *v = 1.5.into());
+ ///
+ /// // "Steal" ownership of a value. Can replace with any valid Value.
+ /// let old_x = value.pointer_mut("/x").map(Value::take).unwrap();
+ /// assert_eq!(old_x, 1.5);
+ /// assert_eq!(value.pointer("/x").unwrap(), &Value::Null);
+ /// }
+ /// ```
+ pub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value> {
+ if pointer.is_empty() {
+ return Some(self);
+ }
+ if !pointer.starts_with('/') {
+ return None;
+ }
+ pointer
+ .split('/')
+ .skip(1)
+ .map(|x| x.replace("~1", "/").replace("~0", "~"))
+ .try_fold(self, |target, token| match target {
+ Value::Object(map) => map.get_mut(&token),
+ Value::Array(list) => parse_index(&token).and_then(move |x| list.get_mut(x)),
+ _ => None,
+ })
+ }
+
+ /// Takes the value out of the `Value`, leaving a `Null` in its place.
+ ///
+ /// ```
+ /// # use serde_json::json;
+ /// #
+ /// let mut v = json!({ "x": "y" });
+ /// assert_eq!(v["x"].take(), json!("y"));
+ /// assert_eq!(v, json!({ "x": null }));
+ /// ```
+ pub fn take(&mut self) -> Value {
+ mem::replace(self, Value::Null)
+ }
+}
+
+/// The default value is `Value::Null`.
+///
+/// This is useful for handling omitted `Value` fields when deserializing.
+///
+/// # Examples
+///
+/// ```
+/// # use serde::Deserialize;
+/// use serde_json::Value;
+///
+/// #[derive(Deserialize)]
+/// struct Settings {
+/// level: i32,
+/// #[serde(default)]
+/// extras: Value,
+/// }
+///
+/// # fn try_main() -> Result<(), serde_json::Error> {
+/// let data = r#" { "level": 42 } "#;
+/// let s: Settings = serde_json::from_str(data)?;
+///
+/// assert_eq!(s.level, 42);
+/// assert_eq!(s.extras, Value::Null);
+/// #
+/// # Ok(())
+/// # }
+/// #
+/// # try_main().unwrap()
+/// ```
+impl Default for Value {
+ fn default() -> Value {
+ Value::Null
+ }
+}
+
+mod de;
+mod from;
+mod index;
+mod partial_eq;
+mod ser;
+
+/// Convert a `T` into `serde_json::Value` which is an enum that can represent
+/// any valid JSON data.
+///
+/// # Example
+///
+/// ```
+/// use serde::Serialize;
+/// use serde_json::json;
+///
+/// use std::error::Error;
+///
+/// #[derive(Serialize)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn compare_json_values() -> Result<(), Box<Error>> {
+/// let u = User {
+/// fingerprint: "0xF9BA143B95FF6D82".to_owned(),
+/// location: "Menlo Park, CA".to_owned(),
+/// };
+///
+/// // The type of `expected` is `serde_json::Value`
+/// let expected = json!({
+/// "fingerprint": "0xF9BA143B95FF6D82",
+/// "location": "Menlo Park, CA",
+/// });
+///
+/// let v = serde_json::to_value(u).unwrap();
+/// assert_eq!(v, expected);
+///
+/// Ok(())
+/// }
+/// #
+/// # compare_json_values().unwrap();
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if `T`'s implementation of `Serialize` decides to
+/// fail, or if `T` contains a map with non-string keys.
+///
+/// ```
+/// use std::collections::BTreeMap;
+///
+/// fn main() {
+/// // The keys in this map are vectors, not strings.
+/// let mut map = BTreeMap::new();
+/// map.insert(vec![32, 64], "x86");
+///
+/// println!("{}", serde_json::to_value(map).unwrap_err());
+/// }
+/// ```
+// Taking by value is more friendly to iterator adapters, option and result
+// consumers, etc. See https://github.com/serde-rs/json/pull/149.
+pub fn to_value<T>(value: T) -> Result<Value, Error>
+where
+ T: Serialize,
+{
+ value.serialize(Serializer)
+}
+
+/// Interpret a `serde_json::Value` as an instance of type `T`.
+///
+/// # Example
+///
+/// ```
+/// use serde::Deserialize;
+/// use serde_json::json;
+///
+/// #[derive(Deserialize, Debug)]
+/// struct User {
+/// fingerprint: String,
+/// location: String,
+/// }
+///
+/// fn main() {
+/// // The type of `j` is `serde_json::Value`
+/// let j = json!({
+/// "fingerprint": "0xF9BA143B95FF6D82",
+/// "location": "Menlo Park, CA"
+/// });
+///
+/// let u: User = serde_json::from_value(j).unwrap();
+/// println!("{:#?}", u);
+/// }
+/// ```
+///
+/// # Errors
+///
+/// This conversion can fail if the structure of the Value does not match the
+/// structure expected by `T`, for example if `T` is a struct type but the Value
+/// contains something other than a JSON map. It can also fail if the structure
+/// is correct but `T`'s implementation of `Deserialize` decides that something
+/// is wrong with the data, for example required struct fields are missing from
+/// the JSON map or some number is too big to fit in the expected primitive
+/// type.
+pub fn from_value<T>(value: Value) -> Result<T, Error>
+where
+ T: DeserializeOwned,
+{
+ T::deserialize(value)
+}
diff --git a/third_party/rust/serde_json/src/value/partial_eq.rs b/third_party/rust/serde_json/src/value/partial_eq.rs
new file mode 100644
index 0000000000..b4ef84c4f3
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/partial_eq.rs
@@ -0,0 +1,95 @@
+use super::Value;
+use alloc::string::String;
+
+fn eq_i64(value: &Value, other: i64) -> bool {
+ value.as_i64().map_or(false, |i| i == other)
+}
+
+fn eq_u64(value: &Value, other: u64) -> bool {
+ value.as_u64().map_or(false, |i| i == other)
+}
+
+fn eq_f64(value: &Value, other: f64) -> bool {
+ value.as_f64().map_or(false, |i| i == other)
+}
+
+fn eq_bool(value: &Value, other: bool) -> bool {
+ value.as_bool().map_or(false, |i| i == other)
+}
+
+fn eq_str(value: &Value, other: &str) -> bool {
+ value.as_str().map_or(false, |i| i == other)
+}
+
+impl PartialEq<str> for Value {
+ fn eq(&self, other: &str) -> bool {
+ eq_str(self, other)
+ }
+}
+
+impl<'a> PartialEq<&'a str> for Value {
+ fn eq(&self, other: &&str) -> bool {
+ eq_str(self, *other)
+ }
+}
+
+impl PartialEq<Value> for str {
+ fn eq(&self, other: &Value) -> bool {
+ eq_str(other, self)
+ }
+}
+
+impl<'a> PartialEq<Value> for &'a str {
+ fn eq(&self, other: &Value) -> bool {
+ eq_str(other, *self)
+ }
+}
+
+impl PartialEq<String> for Value {
+ fn eq(&self, other: &String) -> bool {
+ eq_str(self, other.as_str())
+ }
+}
+
+impl PartialEq<Value> for String {
+ fn eq(&self, other: &Value) -> bool {
+ eq_str(other, self.as_str())
+ }
+}
+
+macro_rules! partialeq_numeric {
+ ($($eq:ident [$($ty:ty)*])*) => {
+ $($(
+ impl PartialEq<$ty> for Value {
+ fn eq(&self, other: &$ty) -> bool {
+ $eq(self, *other as _)
+ }
+ }
+
+ impl PartialEq<Value> for $ty {
+ fn eq(&self, other: &Value) -> bool {
+ $eq(other, *self as _)
+ }
+ }
+
+ impl<'a> PartialEq<$ty> for &'a Value {
+ fn eq(&self, other: &$ty) -> bool {
+ $eq(*self, *other as _)
+ }
+ }
+
+ impl<'a> PartialEq<$ty> for &'a mut Value {
+ fn eq(&self, other: &$ty) -> bool {
+ $eq(*self, *other as _)
+ }
+ }
+ )*)*
+ }
+}
+
+partialeq_numeric! {
+ eq_i64[i8 i16 i32 i64 isize]
+ eq_u64[u8 u16 u32 u64 usize]
+ eq_f64[f32 f64]
+ eq_bool[bool]
+}
diff --git a/third_party/rust/serde_json/src/value/ser.rs b/third_party/rust/serde_json/src/value/ser.rs
new file mode 100644
index 0000000000..892a63d5fc
--- /dev/null
+++ b/third_party/rust/serde_json/src/value/ser.rs
@@ -0,0 +1,1018 @@
+use crate::error::{Error, ErrorCode, Result};
+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};
+
+impl Serialize for Value {
+ #[inline]
+ fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
+ where
+ S: ::serde::Serializer,
+ {
+ match self {
+ Value::Null => serializer.serialize_unit(),
+ Value::Bool(b) => serializer.serialize_bool(*b),
+ Value::Number(n) => n.serialize(serializer),
+ Value::String(s) => serializer.serialize_str(s),
+ Value::Array(v) => v.serialize(serializer),
+ #[cfg(any(feature = "std", feature = "alloc"))]
+ Value::Object(m) => {
+ use serde::ser::SerializeMap;
+ let mut map = tri!(serializer.serialize_map(Some(m.len())));
+ for (k, v) in m {
+ tri!(map.serialize_entry(k, v));
+ }
+ map.end()
+ }
+ }
+ }
+}
+
+/// Serializer whose output is a `Value`.
+///
+/// This is the serializer that backs [`serde_json::to_value`][crate::to_value].
+/// Unlike the main serde_json serializer which goes from some serializable
+/// value of type `T` to JSON text, this one goes from `T` to
+/// `serde_json::Value`.
+///
+/// The `to_value` function is implementable as:
+///
+/// ```
+/// use serde::Serialize;
+/// use serde_json::{Error, Value};
+///
+/// pub fn to_value<T>(input: T) -> Result<Value, Error>
+/// where
+/// T: Serialize,
+/// {
+/// input.serialize(serde_json::value::Serializer)
+/// }
+/// ```
+pub struct Serializer;
+
+impl serde::Serializer for Serializer {
+ type Ok = Value;
+ type Error = Error;
+
+ type SerializeSeq = SerializeVec;
+ type SerializeTuple = SerializeVec;
+ type SerializeTupleStruct = SerializeVec;
+ type SerializeTupleVariant = SerializeTupleVariant;
+ type SerializeMap = SerializeMap;
+ type SerializeStruct = SerializeMap;
+ type SerializeStructVariant = SerializeStructVariant;
+
+ #[inline]
+ fn serialize_bool(self, value: bool) -> Result<Value> {
+ Ok(Value::Bool(value))
+ }
+
+ #[inline]
+ fn serialize_i8(self, value: i8) -> Result<Value> {
+ self.serialize_i64(value as i64)
+ }
+
+ #[inline]
+ fn serialize_i16(self, value: i16) -> Result<Value> {
+ self.serialize_i64(value as i64)
+ }
+
+ #[inline]
+ fn serialize_i32(self, value: i32) -> Result<Value> {
+ self.serialize_i64(value as i64)
+ }
+
+ fn serialize_i64(self, value: i64) -> Result<Value> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn serialize_i128(self, value: i128) -> Result<Value> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[inline]
+ fn serialize_u8(self, value: u8) -> Result<Value> {
+ self.serialize_u64(value as u64)
+ }
+
+ #[inline]
+ fn serialize_u16(self, value: u16) -> Result<Value> {
+ self.serialize_u64(value as u64)
+ }
+
+ #[inline]
+ fn serialize_u32(self, value: u32) -> Result<Value> {
+ self.serialize_u64(value as u64)
+ }
+
+ #[inline]
+ fn serialize_u64(self, value: u64) -> Result<Value> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[cfg(feature = "arbitrary_precision")]
+ fn serialize_u128(self, value: u128) -> Result<Value> {
+ Ok(Value::Number(value.into()))
+ }
+
+ #[inline]
+ fn serialize_f32(self, value: f32) -> Result<Value> {
+ self.serialize_f64(value as f64)
+ }
+
+ #[inline]
+ fn serialize_f64(self, value: f64) -> Result<Value> {
+ Ok(Number::from_f64(value).map_or(Value::Null, Value::Number))
+ }
+
+ #[inline]
+ fn serialize_char(self, value: char) -> Result<Value> {
+ let mut s = String::new();
+ s.push(value);
+ Ok(Value::String(s))
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<Value> {
+ Ok(Value::String(value.to_owned()))
+ }
+
+ fn serialize_bytes(self, value: &[u8]) -> Result<Value> {
+ let vec = value.iter().map(|&b| Value::Number(b.into())).collect();
+ Ok(Value::Array(vec))
+ }
+
+ #[inline]
+ fn serialize_unit(self) -> Result<Value> {
+ Ok(Value::Null)
+ }
+
+ #[inline]
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<Value> {
+ self.serialize_str(variant)
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ value: &T,
+ ) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ let mut values = Map::new();
+ values.insert(String::from(variant), tri!(to_value(value)));
+ Ok(Value::Object(values))
+ }
+
+ #[inline]
+ fn serialize_none(self) -> Result<Value> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_some<T>(self, value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Ok(SerializeVec {
+ vec: Vec::with_capacity(len.unwrap_or(0)),
+ })
+ }
+
+ fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
+ self.serialize_seq(Some(len))
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ self.serialize_seq(Some(len))
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Ok(SerializeTupleVariant {
+ name: String::from(variant),
+ vec: Vec::with_capacity(len),
+ })
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Ok(SerializeMap::Map {
+ map: Map::new(),
+ next_key: None,
+ })
+ }
+
+ fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
+ match name {
+ #[cfg(feature = "arbitrary_precision")]
+ crate::number::TOKEN => Ok(SerializeMap::Number { out_value: None }),
+ #[cfg(feature = "raw_value")]
+ crate::raw::TOKEN => Ok(SerializeMap::RawValue { out_value: None }),
+ _ => self.serialize_map(Some(len)),
+ }
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Ok(SerializeStructVariant {
+ name: String::from(variant),
+ map: Map::new(),
+ })
+ }
+
+ fn collect_str<T>(self, value: &T) -> Result<Value>
+ where
+ T: ?Sized + Display,
+ {
+ Ok(Value::String(value.to_string()))
+ }
+}
+
+pub struct SerializeVec {
+ vec: Vec<Value>,
+}
+
+pub struct SerializeTupleVariant {
+ name: String,
+ vec: Vec<Value>,
+}
+
+pub enum SerializeMap {
+ Map {
+ map: Map<String, Value>,
+ next_key: Option<String>,
+ },
+ #[cfg(feature = "arbitrary_precision")]
+ Number { out_value: Option<Value> },
+ #[cfg(feature = "raw_value")]
+ RawValue { out_value: Option<Value> },
+}
+
+pub struct SerializeStructVariant {
+ name: String,
+ map: Map<String, Value>,
+}
+
+impl serde::ser::SerializeSeq for SerializeVec {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.vec.push(tri!(to_value(value)));
+ Ok(())
+ }
+
+ fn end(self) -> Result<Value> {
+ Ok(Value::Array(self.vec))
+ }
+}
+
+impl serde::ser::SerializeTuple for SerializeVec {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ serde::ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ fn end(self) -> Result<Value> {
+ serde::ser::SerializeSeq::end(self)
+ }
+}
+
+impl serde::ser::SerializeTupleStruct for SerializeVec {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ serde::ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ fn end(self) -> Result<Value> {
+ serde::ser::SerializeSeq::end(self)
+ }
+}
+
+impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.vec.push(tri!(to_value(value)));
+ Ok(())
+ }
+
+ fn end(self) -> Result<Value> {
+ let mut object = Map::new();
+
+ object.insert(self.name, Value::Array(self.vec));
+
+ Ok(Value::Object(object))
+ }
+}
+
+impl serde::ser::SerializeMap for SerializeMap {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_key<T>(&mut self, key: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ SerializeMap::Map { next_key, .. } => {
+ *next_key = Some(tri!(key.serialize(MapKeySerializer)));
+ Ok(())
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ SerializeMap::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ SerializeMap::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ fn serialize_value<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ SerializeMap::Map { map, next_key } => {
+ let key = next_key.take();
+ // Panic because this indicates a bug in the program rather than an
+ // expected failure.
+ let key = key.expect("serialize_value called before serialize_key");
+ map.insert(key, tri!(to_value(value)));
+ Ok(())
+ }
+ #[cfg(feature = "arbitrary_precision")]
+ SerializeMap::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ SerializeMap::RawValue { .. } => unreachable!(),
+ }
+ }
+
+ fn end(self) -> Result<Value> {
+ match self {
+ SerializeMap::Map { map, .. } => Ok(Value::Object(map)),
+ #[cfg(feature = "arbitrary_precision")]
+ SerializeMap::Number { .. } => unreachable!(),
+ #[cfg(feature = "raw_value")]
+ SerializeMap::RawValue { .. } => unreachable!(),
+ }
+ }
+}
+
+struct MapKeySerializer;
+
+fn key_must_be_a_string() -> Error {
+ Error::syntax(ErrorCode::KeyMustBeAString, 0, 0)
+}
+
+impl serde::Serializer for MapKeySerializer {
+ type Ok = String;
+ type Error = Error;
+
+ type SerializeSeq = Impossible<String, Error>;
+ type SerializeTuple = Impossible<String, Error>;
+ type SerializeTupleStruct = Impossible<String, Error>;
+ type SerializeTupleVariant = Impossible<String, Error>;
+ type SerializeMap = Impossible<String, Error>;
+ type SerializeStruct = Impossible<String, Error>;
+ type SerializeStructVariant = Impossible<String, Error>;
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<String> {
+ Ok(variant.to_owned())
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_bool(self, _value: bool) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_i8(self, value: i8) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i16(self, value: i16) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i32(self, value: i32) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_i64(self, value: i64) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u8(self, value: u8) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u16(self, value: u16) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u32(self, value: u32) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_u64(self, value: u64) -> Result<String> {
+ Ok(value.to_string())
+ }
+
+ fn serialize_f32(self, _value: f32) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_f64(self, _value: f64) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ #[inline]
+ fn serialize_char(self, value: char) -> Result<String> {
+ Ok({
+ let mut s = String::new();
+ s.push(value);
+ s
+ })
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<String> {
+ Ok(value.to_owned())
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_unit(self) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<String>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_none(self) -> Result<String> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<String>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(key_must_be_a_string())
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Err(key_must_be_a_string())
+ }
+
+ fn collect_str<T>(self, value: &T) -> Result<String>
+ where
+ T: ?Sized + Display,
+ {
+ Ok(value.to_string())
+ }
+}
+
+impl serde::ser::SerializeStruct for SerializeMap {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ match self {
+ SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value),
+ #[cfg(feature = "arbitrary_precision")]
+ SerializeMap::Number { out_value } => {
+ if key == crate::number::TOKEN {
+ *out_value = Some(value.serialize(NumberValueEmitter)?);
+ Ok(())
+ } else {
+ Err(invalid_number())
+ }
+ }
+ #[cfg(feature = "raw_value")]
+ SerializeMap::RawValue { out_value } => {
+ if key == crate::raw::TOKEN {
+ *out_value = Some(value.serialize(RawValueEmitter)?);
+ Ok(())
+ } else {
+ Err(invalid_raw_value())
+ }
+ }
+ }
+ }
+
+ fn end(self) -> Result<Value> {
+ match self {
+ SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self),
+ #[cfg(feature = "arbitrary_precision")]
+ SerializeMap::Number { out_value, .. } => {
+ Ok(out_value.expect("number value was not emitted"))
+ }
+ #[cfg(feature = "raw_value")]
+ SerializeMap::RawValue { out_value, .. } => {
+ Ok(out_value.expect("raw value was not emitted"))
+ }
+ }
+ }
+}
+
+impl serde::ser::SerializeStructVariant for SerializeStructVariant {
+ type Ok = Value;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.map.insert(String::from(key), tri!(to_value(value)));
+ Ok(())
+ }
+
+ fn end(self) -> Result<Value> {
+ let mut object = Map::new();
+
+ object.insert(self.name, Value::Object(self.map));
+
+ Ok(Value::Object(object))
+ }
+}
+
+#[cfg(feature = "arbitrary_precision")]
+struct NumberValueEmitter;
+
+#[cfg(feature = "arbitrary_precision")]
+fn invalid_number() -> Error {
+ Error::syntax(ErrorCode::InvalidNumber, 0, 0)
+}
+
+#[cfg(feature = "arbitrary_precision")]
+impl serde::ser::Serializer for NumberValueEmitter {
+ type Ok = Value;
+ type Error = Error;
+
+ type SerializeSeq = Impossible<Value, Error>;
+ type SerializeTuple = Impossible<Value, Error>;
+ type SerializeTupleStruct = Impossible<Value, Error>;
+ type SerializeTupleVariant = Impossible<Value, Error>;
+ type SerializeMap = Impossible<Value, Error>;
+ type SerializeStruct = Impossible<Value, Error>;
+ type SerializeStructVariant = Impossible<Value, Error>;
+
+ fn serialize_bool(self, _v: bool) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i8(self, _v: i8) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i16(self, _v: i16) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i32(self, _v: i32) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_i64(self, _v: i64) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u8(self, _v: u8) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u16(self, _v: u16) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u32(self, _v: u32) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_u64(self, _v: u64) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_f32(self, _v: f32) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_f64(self, _v: f64) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_char(self, _v: char) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_str(self, value: &str) -> Result<Value> {
+ let n = tri!(value.to_owned().parse());
+ Ok(Value::Number(n))
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_none(self) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit(self) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ ) -> Result<Value> {
+ Err(invalid_number())
+ }
+
+ fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_number())
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(invalid_number())
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(invalid_number())
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(invalid_number())
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(invalid_number())
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Err(invalid_number())
+ }
+}
+
+#[cfg(feature = "raw_value")]
+struct RawValueEmitter;
+
+#[cfg(feature = "raw_value")]
+fn invalid_raw_value() -> Error {
+ Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0)
+}
+
+#[cfg(feature = "raw_value")]
+impl serde::ser::Serializer for RawValueEmitter {
+ type Ok = Value;
+ type Error = Error;
+
+ type SerializeSeq = Impossible<Value, Error>;
+ type SerializeTuple = Impossible<Value, Error>;
+ type SerializeTupleStruct = Impossible<Value, Error>;
+ type SerializeTupleVariant = Impossible<Value, Error>;
+ type SerializeMap = Impossible<Value, Error>;
+ type SerializeStruct = Impossible<Value, Error>;
+ type SerializeStructVariant = Impossible<Value, Error>;
+
+ fn serialize_bool(self, _v: bool) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_i8(self, _v: i8) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_i16(self, _v: i16) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_i32(self, _v: i32) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_i64(self, _v: i64) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_u8(self, _v: u8) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_u16(self, _v: u16) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_u32(self, _v: u32) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_u64(self, _v: u64) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_f32(self, _v: f32) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_f64(self, _v: f64) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_char(self, _v: char) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_str(self, value: &str) -> Result<Value> {
+ crate::from_str(value)
+ }
+
+ fn serialize_bytes(self, _value: &[u8]) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_none(self) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_unit(self) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ ) -> Result<Value> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<Value>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(invalid_raw_value())
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> 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/third_party/rust/serde_json/tests/compiletest.rs b/third_party/rust/serde_json/tests/compiletest.rs
new file mode 100644
index 0000000000..7974a6249e
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/debug.rs b/third_party/rust/serde_json/tests/debug.rs
new file mode 100644
index 0000000000..8ddcf5a381
--- /dev/null
+++ b/third_party/rust/serde_json/tests/debug.rs
@@ -0,0 +1,81 @@
+use indoc::indoc;
+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)");
+ assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1"
+ assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41");
+}
+
+#[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);
+}
+
+#[test]
+fn indented() {
+ let j = json!({
+ "Array": [true],
+ "Bool": true,
+ "EmptyArray": [],
+ "EmptyObject": {},
+ "Null": null,
+ "Number": 1,
+ "String": "...",
+ });
+ let expected = indoc! {r#"
+ Object {
+ "Array": Array [
+ Bool(true),
+ ],
+ "Bool": Bool(true),
+ "EmptyArray": Array [],
+ "EmptyObject": Object {},
+ "Null": Null,
+ "Number": Number(1),
+ "String": String("..."),
+ }"#
+ };
+ assert_eq!(format!("{:#?}", j), expected);
+}
diff --git a/third_party/rust/serde_json/tests/lexical.rs b/third_party/rust/serde_json/tests/lexical.rs
new file mode 100644
index 0000000000..6e0f07b8c3
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/algorithm.rs b/third_party/rust/serde_json/tests/lexical/algorithm.rs
new file mode 100644
index 0000000000..7f3a2c6241
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/exponent.rs b/third_party/rust/serde_json/tests/lexical/exponent.rs
new file mode 100644
index 0000000000..f7a847be38
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/float.rs b/third_party/rust/serde_json/tests/lexical/float.rs
new file mode 100644
index 0000000000..c87e7e1ed9
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/math.rs b/third_party/rust/serde_json/tests/lexical/math.rs
new file mode 100644
index 0000000000..79d3ef3ee5
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/num.rs b/third_party/rust/serde_json/tests/lexical/num.rs
new file mode 100644
index 0000000000..1a94be0130
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/parse.rs b/third_party/rust/serde_json/tests/lexical/parse.rs
new file mode 100644
index 0000000000..80ca25e772
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/lexical/rounding.rs b/third_party/rust/serde_json/tests/lexical/rounding.rs
new file mode 100644
index 0000000000..7ea17716c3
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/macros/mod.rs b/third_party/rust/serde_json/tests/macros/mod.rs
new file mode 100644
index 0000000000..aaf820f074
--- /dev/null
+++ b/third_party/rust/serde_json/tests/macros/mod.rs
@@ -0,0 +1,61 @@
+#![allow(unused_macro_rules)]
+
+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/third_party/rust/serde_json/tests/map.rs b/third_party/rust/serde_json/tests/map.rs
new file mode 100644
index 0000000000..ae01969561
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/regression.rs b/third_party/rust/serde_json/tests/regression.rs
new file mode 100644
index 0000000000..fb2b25c89b
--- /dev/null
+++ b/third_party/rust/serde_json/tests/regression.rs
@@ -0,0 +1,3 @@
+mod regression {
+ automod::dir!("tests/regression");
+}
diff --git a/third_party/rust/serde_json/tests/regression/issue520.rs b/third_party/rust/serde_json/tests/regression/issue520.rs
new file mode 100644
index 0000000000..9ed367731b
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/regression/issue795.rs b/third_party/rust/serde_json/tests/regression/issue795.rs
new file mode 100644
index 0000000000..bb82852c54
--- /dev/null
+++ b/third_party/rust/serde_json/tests/regression/issue795.rs
@@ -0,0 +1,59 @@
+#![allow(clippy::assertions_on_result_states)]
+
+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/third_party/rust/serde_json/tests/regression/issue845.rs b/third_party/rust/serde_json/tests/regression/issue845.rs
new file mode 100644
index 0000000000..56037ae669
--- /dev/null
+++ b/third_party/rust/serde_json/tests/regression/issue845.rs
@@ -0,0 +1,74 @@
+#![allow(clippy::trait_duplication_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/8757
+
+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/third_party/rust/serde_json/tests/regression/issue953.rs b/third_party/rust/serde_json/tests/regression/issue953.rs
new file mode 100644
index 0000000000..771aa5287c
--- /dev/null
+++ b/third_party/rust/serde_json/tests/regression/issue953.rs
@@ -0,0 +1,9 @@
+use serde_json::Value;
+
+#[test]
+fn test() {
+ let x1 = serde_json::from_str::<Value>("18446744073709551615.");
+ assert!(x1.is_err());
+ let x2 = serde_json::from_str::<Value>("18446744073709551616.");
+ assert!(x2.is_err());
+}
diff --git a/third_party/rust/serde_json/tests/stream.rs b/third_party/rust/serde_json/tests/stream.rs
new file mode 100644
index 0000000000..ec6b9e3d02
--- /dev/null
+++ b/third_party/rust/serde_json/tests/stream.rs
@@ -0,0 +1,183 @@
+#![cfg(not(feature = "preserve_order"))]
+#![allow(clippy::assertions_on_result_states)]
+
+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/third_party/rust/serde_json/tests/test.rs b/third_party/rust/serde_json/tests/test.rs
new file mode 100644
index 0000000000..c2050724bc
--- /dev/null
+++ b/third_party/rust/serde_json/tests/test.rs
@@ -0,0 +1,2387 @@
+#![cfg(not(feature = "preserve_order"))]
+#![allow(
+ clippy::assertions_on_result_states,
+ clippy::cast_precision_loss,
+ clippy::derive_partial_eq_without_eq,
+ clippy::excessive_precision,
+ clippy::float_cmp,
+ clippy::items_after_statements,
+ 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![
+ ("\"\"", String::new()),
+ ("\"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/third_party/rust/serde_json/tests/ui/missing_colon.rs b/third_party/rust/serde_json/tests/ui/missing_colon.rs
new file mode 100644
index 0000000000..d93b7b907d
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_colon.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" });
+}
diff --git a/third_party/rust/serde_json/tests/ui/missing_colon.stderr b/third_party/rust/serde_json/tests/ui/missing_colon.stderr
new file mode 100644
index 0000000000..1515211adf
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_colon.stderr
@@ -0,0 +1,12 @@
+error: unexpected end of macro invocation
+ --> tests/ui/missing_colon.rs:4:5
+ |
+4 | json!({ "a" });
+ | ^^^^^^^^^^^^^^ missing tokens in macro arguments
+ |
+note: while trying to match `@`
+ --> src/macros.rs
+ |
+ | (@array [$($elems:expr,)*]) => {
+ | ^
+ = note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git a/third_party/rust/serde_json/tests/ui/missing_comma.rs b/third_party/rust/serde_json/tests/ui/missing_comma.rs
new file mode 100644
index 0000000000..8818c3e60c
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_comma.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "1": "" "2": "" });
+}
diff --git a/third_party/rust/serde_json/tests/ui/missing_comma.stderr b/third_party/rust/serde_json/tests/ui/missing_comma.stderr
new file mode 100644
index 0000000000..bafa0f8915
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_comma.stderr
@@ -0,0 +1,13 @@
+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
+ |
+note: while trying to match `,`
+ --> src/macros.rs
+ |
+ | ($e:expr , $($tt:tt)*) => {};
+ | ^
diff --git a/third_party/rust/serde_json/tests/ui/missing_value.rs b/third_party/rust/serde_json/tests/ui/missing_value.rs
new file mode 100644
index 0000000000..0ba14e22c2
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_value.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : });
+}
diff --git a/third_party/rust/serde_json/tests/ui/missing_value.stderr b/third_party/rust/serde_json/tests/ui/missing_value.stderr
new file mode 100644
index 0000000000..9c9de99ca9
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/missing_value.stderr
@@ -0,0 +1,12 @@
+error: unexpected end of macro invocation
+ --> tests/ui/missing_value.rs:4:5
+ |
+4 | json!({ "a" : });
+ | ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
+ |
+note: while trying to match `@`
+ --> src/macros.rs
+ |
+ | (@array [$($elems:expr,)*]) => {
+ | ^
+ = note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git a/third_party/rust/serde_json/tests/ui/not_found.rs b/third_party/rust/serde_json/tests/ui/not_found.rs
new file mode 100644
index 0000000000..2df6870dc3
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/not_found.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : x });
+}
diff --git a/third_party/rust/serde_json/tests/ui/not_found.stderr b/third_party/rust/serde_json/tests/ui/not_found.stderr
new file mode 100644
index 0000000000..6fec180409
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/ui/parse_expr.rs b/third_party/rust/serde_json/tests/ui/parse_expr.rs
new file mode 100644
index 0000000000..e7f1805b1e
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/parse_expr.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" : ~ });
+}
diff --git a/third_party/rust/serde_json/tests/ui/parse_expr.stderr b/third_party/rust/serde_json/tests/ui/parse_expr.stderr
new file mode 100644
index 0000000000..cd3e1c94d1
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/parse_expr.stderr
@@ -0,0 +1,11 @@
+error: no rules expected the token `~`
+ --> tests/ui/parse_expr.rs:4:19
+ |
+4 | json!({ "a" : ~ });
+ | ^ no rules expected this token in macro call
+ |
+note: while trying to match meta-variable `$e:expr`
+ --> src/macros.rs
+ |
+ | ($e:expr , $($tt:tt)*) => {};
+ | ^^^^^^^
diff --git a/third_party/rust/serde_json/tests/ui/parse_key.rs b/third_party/rust/serde_json/tests/ui/parse_key.rs
new file mode 100644
index 0000000000..858bd71692
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/parse_key.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "".s : true });
+}
diff --git a/third_party/rust/serde_json/tests/ui/parse_key.stderr b/third_party/rust/serde_json/tests/ui/parse_key.stderr
new file mode 100644
index 0000000000..f10c21800e
--- /dev/null
+++ b/third_party/rust/serde_json/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/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs
new file mode 100644
index 0000000000..226c58cf17
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!([ true => ]);
+}
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr
new file mode 100644
index 0000000000..ef449f764d
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr
@@ -0,0 +1,7 @@
+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
+ |
+ = note: while trying to match end of macro
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs
new file mode 100644
index 0000000000..0dfb731559
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "k": true => });
+}
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr
new file mode 100644
index 0000000000..c62d90ba0d
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr
@@ -0,0 +1,7 @@
+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
+ |
+ = note: while trying to match end of macro
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_colon.rs b/third_party/rust/serde_json/tests/ui/unexpected_colon.rs
new file mode 100644
index 0000000000..e767ea6f7d
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_colon.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ : true });
+}
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr b/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr
new file mode 100644
index 0000000000..7e47726bc6
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr
@@ -0,0 +1,7 @@
+error: no rules expected the token `:`
+ --> tests/ui/unexpected_colon.rs:4:13
+ |
+4 | json!({ : true });
+ | ^ no rules expected this token in macro call
+ |
+ = note: while trying to match end of macro
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_comma.rs b/third_party/rust/serde_json/tests/ui/unexpected_comma.rs
new file mode 100644
index 0000000000..338874ed8f
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_comma.rs
@@ -0,0 +1,5 @@
+use serde_json::json;
+
+fn main() {
+ json!({ "a" , "b": true });
+}
diff --git a/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr b/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr
new file mode 100644
index 0000000000..552f399a50
--- /dev/null
+++ b/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr
@@ -0,0 +1,7 @@
+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
+ |
+ = note: while trying to match end of macro