Skip to main content

stringify_deterministic

Function stringify_deterministic 

Source
pub fn stringify_deterministic(doc: &AppDataDoc) -> Result<String, CowError>
Expand description

Serialise doc to a deterministic JSON string with all object keys sorted alphabetically at every nesting level.

This is the core serialisation primitive that underpins all app-data hashing in this crate. It guarantees that two AppDataDoc values with identical logical content always produce byte-identical JSON, regardless of Rust struct field order or serde attribute ordering.

Matches the behaviour of json-stringify-deterministic used by the TypeScript SDK, ensuring cross-language hash compatibility.

§Parameters

§Returns

A compact JSON string with no whitespace between tokens and all object keys recursively sorted in lexicographic order.

§Errors

Returns CowError::AppData on serialisation failure.

§Example

use cow_rs::app_data::{AppDataDoc, stringify_deterministic};

let doc = AppDataDoc::new("Test");
let json = stringify_deterministic(&doc).unwrap();
// Deterministic: calling twice yields the exact same bytes.
assert_eq!(json, stringify_deterministic(&doc).unwrap());