fixed-json
fixed-json is a no-allocation JSON parser for unpacking known JSON shapes
into caller-owned fixed storage. It is inspired by Eric S. Raymond's
microjson C library, but exposes a Rust
API built around enums, builders, slices, Result, and ordinary error traits.
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 for known input shapes
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
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.
Errors are returned as fixed_json::Error, which implements Display.
The std::error::Error implementation is available when the std feature is
enabled. For compatibility with the C API style, error_string(error) is also
provided.
Basic Usage
use ObjectBuilder;
let mut count = 0;
let mut flag1 = false;
let mut flag2 = false;
let end = new
.integer
.boolean
.boolean
.read?;
assert_eq!;
assert_eq!;
assert!;
assert!;
# Ok::
The const generic parameter is the maximum number of attributes the builder can
hold. read_object with explicit descriptors remains available when you need to
reuse descriptor arrays or build nested descriptors separately.
Fixed String Buffers
Strings are written into caller-provided byte buffers and nul-terminated when space allows:
use ;
let mut name = ;
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;
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.
Defaults, Checks, and Enum Maps
Attributes can be configured fluently:
use ;
const MODES: & = &;
let mut class_ok = 0;
let mut mode = -1;
assert_eq!;
assert_eq!;
# Ok::
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
validate_json accepts arbitrary JSON syntax. The descriptor parser remains
stricter by design: application parsing requires known shapes and fixed storage.
Benchmarks
Criterion benchmarks are available for common parser paths:
To compile the benchmark without running it:
no_std
The crate has no default features and builds as no_std by default:
Enable the optional std feature only if you want Error to implement
std::error::Error:
= { = "0.1", = ["std"] }
Limitations
This crate follows the spirit of the original C microjson library while using a Rust API:
- 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
- strings are written into byte buffers and can be read with
cstr
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.