fixed-json
This directory contains fixed-json, a Rust implementation of the (microjson) library, a
fixed-storage JSON parser, made by Eric S. Raymond in C.
The library is designed for constrained environments:
#![no_std]- no heap allocation in the library
- caller-owned output storage
- fixed string buffers and fixed arrays
- descriptor-based parsing, matching the C library model
The examples and benchmarks use std for command-line I/O and Criterion, but
the library itself does not require std.
Supported API
The primary entry points are:
read_object
read_array
validate_json
error_string
read_object and read_array parse into caller-provided descriptors and
storage. validate_json is a no-allocation JSON syntax validator used by the
JSONTestSuite integration tests.
Basic Usage
use ;
let mut count = 0;
let mut flag1 = false;
let mut flag2 = false;
let mut attrs = ;
let end = read_object?;
drop;
assert_eq!;
assert_eq!;
assert!;
assert!;
# Ok::
Descriptors hold mutable references into the destination storage. Drop the descriptor array before reading the parsed values if the borrow checker requires it.
Fixed String Buffers
Strings are written into caller-provided byte buffers and nul-terminated when space allows:
use ;
let mut name = ;
let mut attrs = ;
read_object?;
drop;
assert_eq!;
# Ok::
If the JSON string does not fit, parsing returns Error::StrLong.
Arrays
Arrays are homogeneous and use caller-provided fixed slices:
use ;
let mut values = ;
let mut count = 0usize;
let mut array = Integers ;
read_array?;
drop;
assert_eq!;
assert_eq!;
# Ok::
Supported array element types include integers, unsigned integers, shorts, unsigned shorts, reals, booleans, strings, object arrays, and struct-object arrays through a callback.
Examples
The C examples have Rust equivalents:
Tests
Run all tests:
Run only the JSONTestSuite integration tests:
The JSONTestSuite tests are generated at build time from
JSONTestSuite/test_parsing/*.json, with one Rust test case per JSON file.
Expectation mapping:
y_*.json: must be accepted byvalidate_jsonn_*.json: must be rejected byvalidate_jsoni_*.json: implementation-defined, exercised but not forced either way
The descriptor parser intentionally remains stricter than a general JSON DOM parser. For example, application parsing still requires known shapes and fixed storage.
Benchmarks
Criterion benchmarks are available for common parser paths:
To compile the benchmark without running it:
no_std Check
Verify the library without default features:
Limitations
This crate follows the spirit of the original C microjson library:
- parsing into application data is descriptor-based
- output storage must be supplied by the caller
- arrays are fixed-capacity
- application-level arrays are homogeneous
- no heap allocation is used by the library
Use validate_json when you need syntax validation of arbitrary JSON. Use
read_object and read_array when you need to unpack known JSON shapes into
fixed storage.