#json #serde #deserialization #ref #borrowed


Provides JSON deserialization into a borrowed DOM

7 releases

0.3.0 Sep 19, 2023
0.2.0 Jul 9, 2023
0.1.4 Jan 14, 2023

#727 in Encoding

Download history 766/week @ 2023-06-06 602/week @ 2023-06-13 708/week @ 2023-06-20 899/week @ 2023-06-27 900/week @ 2023-07-04 998/week @ 2023-07-11 172/week @ 2023-07-18 551/week @ 2023-07-25 64/week @ 2023-08-01 14/week @ 2023-08-08 24/week @ 2023-08-15 24/week @ 2023-08-22 17/week @ 2023-08-29 30/week @ 2023-09-05 25/week @ 2023-09-12 72/week @ 2023-09-19

153 downloads per month
Used in 2 crates (via reductionml-core)

MIT license

515 lines

Crates.io Docs

Serde JSON Borrow

Up to 2x faster JSON parsing for ndjson type use cases.

serde_json_borrow deserializes JSON from &'ctx str into serde_json_borrow::Value<'ctx> DOM, by trying to reference the original bytes, instead of copying them into Strings.

In contrast the default serde_json parses into an owned serde_json::Value. Every String encountered is getting copied and therefore allocated. That's great for ergnomonics, but not great for performance. Especially in cases where the DOM representation is just an intermediate struct.

To get a little bit more performance, serde_json_borrow pushes the (key,values) for JSON objects into a Vec instead of using a BTreeMap. Access works via an iterator, which has the same API when iterating the BTreeMap.


You can take advantage of OwnedValue to parse a String containing unparsed JSON into a Value without having to worry about lifetimes, as OwnedValue will take ownership of the String and reference slices of it, rather than making copies.


Keys in objects are not allowed to have any JSON escaping characters. So if your keys contain any control characters (https://www.json.org/json-en.html), this crate will not work for you. List of unsupported characters in keys.

\" represents the quotation mark character (U+0022).
\\ represents the reverse solidus character (U+005C).
\/ represents the solidus character (U+002F).
\b represents the backspace character (U+0008).
\f represents the form feed character (U+000C).
\n represents the line feed character (U+000A).
\r represents the carriage return character (U+000D).
\t represents the character tabulation character (U+0009).


Running benches/crit_bench.rs (/home/pascal/cargo_target_dir/release/deps/crit_bench-fd2d661e0b4255c5)
                        time:   [352.60 µs 353.40 µs 354.26 µs]
                        thrpt:  [236.01 MiB/s 236.59 MiB/s 237.12 MiB/s]
                        time:   [175.53 µs 175.72 µs 175.93 µs]
                        thrpt:  [475.23 MiB/s 475.81 MiB/s 476.34 MiB/s]


Instead of parsing a JSON object into a Vec, a BTreeMap could be enabled via a feature flag.


~17K SLoC