A small JSON parser written in Rust with a hand-built lexer and recursive-descent parser.
It can tokenise JSON text, parse it into a JsonValue tree, and print it back in normalized JSON form.
- Tokenises JSON input into typed tokens (
{},[], strings, numbers, booleans,null) - Parses nested objects and arrays
- Supports string escapes including Unicode (
\uXXXX) - Exposes both library functions and a binary example
- Includes tests for valid and invalid JSON scenarios
src/tokeniser.rs- Lexer/tokeniser (tokenise)src/parser.rs- Recursive-descent parser (parse)src/error.rs- Shared parser/tokeniser error typesrc/value.rs-JsonValueenum and display formattingsrc/lib.rs- Public library exportssrc/bin/main.rs- Example binary entrypointtests/mod.rs- Integration tests
- Rust (stable) and Cargo
Build the project:
cargo buildRun the parser with JSON from a file:
cargo run --bin jsonparser -- input.jsonOr pass JSON through standard input:
echo'{"name":"Soraya","age":30}'| cargo run --bin jsonparser --Use - to explicitly read from standard input:
echo'{"active":true}'| cargo run --bin jsonparser -- -Run tests:
cargo testuse jsonparser::{parse, tokenise,Result};fnmain() -> Result<()>{let input = r#"{"name":"Soraya","age":30,"active":true}"#;let tokens = tokenise(input)?;let value = parse(tokens)?;println!("{value}");Ok(())}- Objects
- Arrays
- Strings (with common escapes and
\uXXXX) - Numbers (parsed as
f64) - Booleans
null
- Numbers are parsed as
f64 - Unicode escape parsing does not combine UTF-16 surrogate pairs