vr-jcs 0.3.2

RFC 8785 JSON Canonicalization Scheme (JCS) for deterministic serialization in Rust
Documentation

vr-jcs

RFC 8785 JSON Canonicalization Scheme (JCS) for deterministic serialization.

crates.io docs.rs License: Apache-2.0

vr-jcs produces RFC 8785 canonical JSON suitable for deterministic digest computation, content hashing, and stable serialization boundaries. Originally developed for the VertRule receipt infrastructure, it is suitable for any context requiring reproducible JSON output.

What it does

  • Sorts object property names by UTF-16 code units
  • Renders numbers using ECMAScript-compatible formatting (via zmij)
  • Emits canonical JSON with no insignificant whitespace
  • Validates I-JSON string constraints (noncharacter rejection)
  • Rejects duplicate property names on raw JSON parse paths

What it does not do

  • It is not a general-purpose JSON transformation toolkit
  • It does not invent semantics beyond canonicalization
  • Duplicate-key rejection applies to raw JSON parse helpers; typed Serialize inputs do not carry duplicate keys

Usage

use vr_jcs::to_canon_string_from_str;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let canon = to_canon_string_from_str(r#"{"z":1,"a":2}"#)?;
    assert_eq!(canon, r#"{"a":2,"z":1}"#);

    // Duplicates are rejected:
    assert!(to_canon_string_from_str(r#"{"a":1,"a":2}"#).is_err());
    Ok(())
}

API

Function Description
to_canon_bytes_from_slice Parse raw JSON bytes, reject duplicates, return canonical bytes
to_canon_string_from_str Parse raw JSON string, reject duplicates, return canonical string
canonicalize Sort object keys in-place by UTF-16 code units
to_canon_bytes (deprecated) Serialize a Serialize type to canonical JSON bytes
to_canon_string (deprecated) Serialize a Serialize type to a canonical JSON string

All functions return Result<_, JcsError>.

License

Licensed under Apache-2.0. See LICENSE for details.