Expand description
Strict, bounded, and deterministic JSON for reliability-sensitive Rust.
reliakit-json is built for systems that process untrusted JSON or need
predictable output: it parses a strict subset of RFC 8259, rejects
duplicate object keys, enforces explicit resource limits,
preserves number precision, reports errors with location and path, and
serializes deterministically. It has no external dependencies, forbids
unsafe code, and supports no_std (with alloc).
It maps to and from your own types through the JsonEncode / JsonDecode
traits — the optional reliakit-derive crate provides
#[derive(JsonEncode, JsonDecode)]. It deliberately does not include
schema validation, JSON5, comments, trailing commas, lenient parsing, or
SIMD throughput.
§Example
use reliakit_json::{parse_str, to_compact_string};
let value = parse_str(r#"{"name":"reliakit","ok":true}"#).unwrap();
assert_eq!(value.as_object().unwrap().get("name").unwrap().as_str(), Some("reliakit"));
// Serialization is deterministic and preserves member order.
assert_eq!(to_compact_string(&value), r#"{"name":"reliakit","ok":true}"#);
// Strict by default: duplicate keys are rejected, not silently resolved.
assert!(parse_str(r#"{"a":1,"a":2}"#).is_err());§Typed encoding
JsonEncode turns a value into deterministic JSON text and JsonDecode
reads it back strictly. to_json_string and from_json_str do both ends
in one call.
use reliakit_json::{from_json_str, to_json_string};
assert_eq!(to_json_string(&vec![1u8, 2, 3]), "[1,2,3]");
assert_eq!(from_json_str::<Vec<u8>>("[1,2,3]").unwrap(), vec![1u8, 2, 3]);
// Strict: a number with a fraction is rejected for an integer target.
assert!(from_json_str::<u8>("25.0").is_err());§Limits
parse applies conservative JsonLimits by default. Use
parse_with_limits to choose a profile or tune individual limits:
use reliakit_json::{parse_with_limits, JsonLimits};
let limits = JsonLimits::conservative().with_max_depth(8);
assert!(parse_with_limits(b"[[[[[[[[[[1]]]]]]]]]]", limits).is_err());§Feature flags
std(default) enablesstd::error::Errorfor the error types. The crate is otherwiseno_stdand always usesalloc.canonicalenables RFC 8785 (JCS) canonical serialization.primitivesadds typed extraction intoreliakit-primitivesconstrained types (JsonObject::get_str_as,JsonValue::str_as); it pulls inreliakit-primitives(no_std+alloc, zero third-party dependencies).
Structs§
- Json
Decode Error - An error from decoding a
JsonValueinto a typed value. - Json
Error - An error produced while parsing or serializing JSON.
- Json
Extract Error - An error from extracting a typed value out of JSON, carrying the
JsonPathof the offending location. - Json
Form - Accumulates field-extraction failures from a
JsonObjectinto a singleValidationError. - Json
Limits - Resource limits enforced while parsing.
- Json
Member - A single object member (key/value pair).
- Json
Number - A JSON number that preserves its exact, validated source representation.
- Json
Object - A JSON object: members in insertion order with unique keys.
- Json
Path - The location of an error within the JSON document, as a path of object keys
and array indices from the document root (
$).
Enums§
- Json
Decode Error Kind - The kind of a typed-JSON decoding error.
- Json
Error Kind - The category of a parse or serialization failure.
- Json
Extract Error Kind - Why extracting a typed
reliakit-primitivesvalue from JSON failed. - Json
From StrError - The error type of
from_json_str: either the input was not valid JSON, or the parsed value did not match the target type. - Json
Limit Kind - A resource limit that was exceeded while parsing.
- Json
Number Error - An error from a
JsonNumberconversion. - Json
Path Segment - One segment of a
JsonPath. - Json
Value - An owned JSON value.
Traits§
- Json
Decode - A type that can be decoded from a
JsonValue. - Json
Encode - A type that can be encoded into a
JsonValue.
Functions§
- from_
json_ str - Parses JSON text and decodes it into
Tin one step. - parse
- Parses a JSON value from UTF-8 bytes using the default
JsonLimits. - parse_
str - Parses a JSON value from a
&strusing the defaultJsonLimits. - parse_
with_ limits - Parses a JSON value from UTF-8 bytes with explicit
JsonLimits. - to_
canonical_ string - Serializes a value to its RFC 8785 (JCS) canonical form as a
String. - to_
canonical_ vec - Serializes a value to its RFC 8785 (JCS) canonical form as UTF-8 bytes.
- to_
compact_ string - Serializes a value to a compact JSON string (no insignificant whitespace).
- to_
compact_ vec - Serializes a value to compact JSON bytes. See
to_compact_string. - to_
json_ string - Encodes a value to compact, deterministic JSON text.
- to_
json_ vec - Encodes a value to compact, deterministic JSON bytes.