Expand description
§rill-json
rill-json is a fast, 100% safe, and RFC 8259-compliant streaming JSON parser
and serializer, built from scratch in pure Rust.
This library is designed for performance, safety, and correctness.
§Key Features
- 100% Safe Rust: Contains
#![forbid(unsafe_code)]to guarantee nounsafekeyword. - Streaming Parser: An
Iteratorthat emitsParserEvents, ideal for parsing large files with minimal memory. - Optimized Performance: Uses a byte-slice-based tokenizer with a
branchless Lookup Table (LUT) and
memchrfor high-performance, safe-SIMD-accelerated string parsing. - Zero-Allocation String Parsing: Returns borrowed string slices (
&str) when no JSON escapes are present, avoiding allocations. - In-Memory DOM: Provides a
JsonValueenum for convenience, with aJsonValue::parse()function to build an in-memory tree. - Serializer Included: Comes with
stringify()andstringify_pretty()to serialize your Rust data back to JSON. - RFC 8259 Compliant: Passes a full test suite for specification compliance.
§Quick Start: 3 Ways to Use rill-json
§1. Streaming Parser (Lowest Memory)
The parse_streaming function is the most efficient way to parse JSON,
especially large files.
use rill_json::{parse_streaming, ParserEvent};
let json_data = r#"{ "name": "Babbage", "id": 1815 }"#;
let mut parser = parse_streaming(json_data).unwrap();
let mut found_name_key = false;
while let Some(event) = parser.next() {
match event.unwrap() {
ParserEvent::Key(key) if key == "name" => found_name_key = true,
ParserEvent::String(value) if found_name_key => {
println!("Found name: {}", value);
break;
}
_ => found_name_key = false,
}
}§2. In-Memory Parsing (Most Convenient)
For convenience, you can also parse directly to an in-memory JsonValue.
use rill_json::{JsonValue, JsonNumber};
use std::collections::BTreeMap;
let json_data = r#"{ "id": 1815 }"#;
let parsed = JsonValue::parse(json_data).unwrap();
let mut expected_map = BTreeMap::new();
expected_map.insert("id".to_string(), JsonValue::Number(JsonNumber::I64(1815)));
let expected_val = JsonValue::Object(expected_map);
assert_eq!(parsed, expected_val);§3. Serializing (Writing JSON)
You can also create JSON strings from your own Rust data using the JsonValue enum.
use rill_json::{JsonValue, JsonNumber};
use std::collections::BTreeMap;
let mut user = BTreeMap::new();
user.insert("username".to_string(), JsonValue::String("ada_l".to_string()));
user.insert("id".to_string(), JsonValue::Number(JsonNumber::I64(1815)));
let json_object = JsonValue::Object(user);
// Get the compact string
let json_string = json_object.stringify().unwrap();
assert_eq!(json_string, r#"{"id":1815,"username":"ada_l"}"#);
// Or get the pretty-printed version
let pretty_string = json_object.stringify_pretty().unwrap();
println!("{}", pretty_string);Re-exports§
pub use error::ParseError;pub use parser::ParserEvent;pub use parser::StreamingParser;pub use value::JsonNumber;pub use value::JsonValue;
Modules§
- error
- Contains the primary
ParseErrortype for the library. Contains the primaryParseErrortype for the library. - parser
- Contains the streaming
Parserand itsParserEventenum. Contains theStreamingParserand its state machine. - token
- Contains the
TokenandTokenTypeenums used internally. Defines theTokenandTokenTypeenums. - value
- Contains the
JsonValueenum and the serialization (stringify) logic. Contains theJsonValueenum, a native Rust representation of any valid JSON value.
Functions§
- parse_
streaming - Parses a JSON string slice into a
StreamingParser.