9 unstable releases (3 breaking)

0.4.4 Dec 4, 2022
0.4.3 Nov 30, 2022
0.4.2 Oct 4, 2022
0.4.1 Sep 25, 2022
0.1.0 May 9, 2022

#655 in Parser implementations

Download history 1701/week @ 2024-01-15 1801/week @ 2024-01-22 1876/week @ 2024-01-29 2167/week @ 2024-02-05 2539/week @ 2024-02-12 1931/week @ 2024-02-19 1832/week @ 2024-02-26 2212/week @ 2024-03-04 2165/week @ 2024-03-11 2492/week @ 2024-03-18 2798/week @ 2024-03-25 2745/week @ 2024-04-01 2181/week @ 2024-04-08 2425/week @ 2024-04-15 2782/week @ 2024-04-22 2715/week @ 2024-04-29

10,338 downloads per month
Used in 8 crates (4 directly)

Apache-2.0

22KB
482 lines

test codecov

Rust native JSON deserializer

This repository contains a performant Rust implementation to parse JSON by reference.

Why not serde-json?

serde-json is both a JSON parser and data model based on serde's model (Value). Value is an owning data structure.

In many use cases, JSON can be parsed into references. In particular, ownership of strings is only required when the JSON string contains non-ascii characters.

There is a performance oportunity if JSON is parsed by reference instead of by value when possible.

This crate fills this gap. When parsing e.g. a list of strings, this crate is ~2x faster than serde-json (see below).

Safety

This crate is #![forbid(unsafe_code)] and only panics on failed allocations.

Benches

Run

python3 write_bench_files.py && cargo bench --bench parse

for a comparison with serde_json. Broadly speaking, this crate is either faster or equally fast. Some examples:

Array of bools

bool json_deserializer 2^20   time:   [26.022 ms 26.056 ms 26.090 ms]
bool serde_json 2^20          time:   [30.419 ms 30.468 ms 30.516 ms]
bool simd_json 2^20           time:   [31.440 ms 31.486 ms 31.531 ms] 

Array of strings

string json_deserializer 2^18 time:   [10.106 ms 10.138 ms 10.173 ms]
string serde_json 2^18        time:   [23.177 ms 23.209 ms 23.243 ms]
string simd_json 2^18         time:   [10.924 ms 10.941 ms 10.959 ms]

# with `RUSTFLAGS='-C target-cpu=native'` (skilake in this case)
string simd_json 2^18         time:   [8.0735 ms 8.0887 ms 8.1046 ms]

Array of an object with a string

object_string json_deserializer 2^14
                        time:   [2.7631 ms 2.7681 ms 2.7736 ms]
object_string serde_json 2^14
                        time:   [4.3729 ms 4.3823 ms 4.3922 ms]
object_string simd_json 2^14
                        time:   [2.6313 ms 2.6357 ms 2.6401 ms]

Array of an object with a bool

object_bool json_deserializer 2^10
                        time:   [144.14 us 144.35 us 144.62 us]
object_bool serde_json 2^10
                        time:   [197.12 us 197.62 us 198.31 us]
object_bool simd_json 2^10
                        time:   [160.87 us 161.33 us 161.77 us]

Dependencies

~185KB