Crate rill_json

Crate rill_json 

Source
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 no unsafe keyword.
  • Streaming Parser: An Iterator that emits ParserEvents, ideal for parsing large files with minimal memory.
  • Optimized Performance: Uses a byte-slice-based tokenizer with a branchless Lookup Table (LUT) and memchr for 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 JsonValue enum for convenience, with a JsonValue::parse() function to build an in-memory tree.
  • Serializer Included: Comes with stringify() and stringify_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 ParseError type for the library. Contains the primary ParseError type for the library.
parser
Contains the streaming Parser and its ParserEvent enum. Contains the StreamingParser and its state machine.
token
Contains the Token and TokenType enums used internally. Defines the Token and TokenType enums.
value
Contains the JsonValue enum and the serialization (stringify) logic. Contains the JsonValue enum, a native Rust representation of any valid JSON value.

Functions§

parse_streaming
Parses a JSON string slice into a StreamingParser.