High-fidelity JSON lexer and parser

Build status Rust 1.56+

hifijson is a Rust crate that provides a high-fidelity JSON lexer and parser. In this context, high-fidelity means that unlike many other parsers, hifijson aims to preserve input data very faithfully, in particular numbers.

Comparison to serde_json

serde_json is currently the most popular JSON parser written in Rust. However, there are some deficiencies of serde_json:

You should probably use serde_json if you want to serialise / deserialise your existing Rust datatypes. However, if you want to process arbitrary JSON coming from the external world, require some control over what kind of input you read, or just care about fast build times and minimal dependencies, then hifijson might be for you.

There is also [serde-json-core] for embedded usage of JSON; however, this crate neither supports arbitrary-precision numbers, reading from byte iterators, nor escape sequences in strings.

Lexer

Writing a JSON parser is remarkably easy --- the hard part is actually lexing. This is why hifijson provides you first and foremost with a lexer, which you can then use to build a parser yourself. Yes, you. You can do it. hifijson tries to give you some basic abstractions to help you. For example, the default parser is implemented in less than 40 lines of code.

Default parser

Parsing JSON is a minefield, because the JSON standard is underspecified or downright contradictory in certain aspects. For this reason, a parser has to make certain decisions which inputs to accept and which to reject.

hifijson comes with a default parser that might be good enough for many use cases. This parser makes the following choices:

Furthermore, the parser passes all tests of the JSON parsing test suite.

Fuzzing

To run the fuzzer, install cargo-fuzz. Then, if you do not wish to use the nightly Rust compiler as default, run the fuzzer by cargo +nightly fuzz run all.