Skip to main content

Crate reliakit_json

Crate reliakit_json 

Source
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) enables std::error::Error for the error types. The crate is otherwise no_std and always uses alloc.
  • canonical enables RFC 8785 (JCS) canonical serialization.
  • primitives adds typed extraction into reliakit-primitives constrained types (JsonObject::get_str_as, JsonValue::str_as); it pulls in reliakit-primitives (no_std + alloc, zero third-party dependencies).

Structs§

JsonDecodeError
An error from decoding a JsonValue into a typed value.
JsonError
An error produced while parsing or serializing JSON.
JsonExtractError
An error from extracting a typed value out of JSON, carrying the JsonPath of the offending location.
JsonForm
Accumulates field-extraction failures from a JsonObject into a single ValidationError.
JsonLimits
Resource limits enforced while parsing.
JsonMember
A single object member (key/value pair).
JsonNumber
A JSON number that preserves its exact, validated source representation.
JsonObject
A JSON object: members in insertion order with unique keys.
JsonPath
The location of an error within the JSON document, as a path of object keys and array indices from the document root ($).

Enums§

JsonDecodeErrorKind
The kind of a typed-JSON decoding error.
JsonErrorKind
The category of a parse or serialization failure.
JsonExtractErrorKind
Why extracting a typed reliakit-primitives value from JSON failed.
JsonFromStrError
The error type of from_json_str: either the input was not valid JSON, or the parsed value did not match the target type.
JsonLimitKind
A resource limit that was exceeded while parsing.
JsonNumberError
An error from a JsonNumber conversion.
JsonPathSegment
One segment of a JsonPath.
JsonValue
An owned JSON value.

Traits§

JsonDecode
A type that can be decoded from a JsonValue.
JsonEncode
A type that can be encoded into a JsonValue.

Functions§

from_json_str
Parses JSON text and decodes it into T in one step.
parse
Parses a JSON value from UTF-8 bytes using the default JsonLimits.
parse_str
Parses a JSON value from a &str using the default JsonLimits.
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.