Crates.io | canonical_json |
lib.rs | canonical_json |
version | 0.5.0 |
source | src |
created_at | 2020-08-17 14:17:18.670664 |
updated_at | 2022-12-27 11:56:40.211468 |
description | A Canonical JSON serializer |
homepage | |
repository | |
max_upload_size | |
id | 277515 |
size | 25,198 |
Canonical JSON is a variant of JSON in which each value has a single, unambiguous serialized form. This provides meaningful and repeatable hashes of encoded data.
Canonical JSON can be parsed by regular JSON parsers. The most notable differences compared to usual JSON format (RFC 7159 or serde_json::to_string()
) are:
This library follows gibson's Canonical JSON spec.
Add this to your Cargo.toml
:
[dependencies]
canonical_json = "0.5.0"
use serde_json::json;
use canonical_json::ser::to_string;
fn main() {
to_string(&json!(null)); // returns "null"
to_string(&json!("we ❤ Rust")); // returns "we \u2764 Rust""
to_string(&json!(10.0_f64.powf(21.0))); // returns "1e+21"
to_string(&json!({
"a": "a",
"id": "1",
"b": "b"
})); // returns "{"a":"a","b":"b","id":"1"}"; (orders object keys)
to_string(&json!(vec!["one", "two", "three"])); // returns "["one","two","three"]"
}
Run the projet test suite:
$ cargo test
Run @gibson042's Canonical JSON test suite:
$ git clone git@github.com:gibson042/canonicaljson-spec.git
$ cd canonicaljson-spec/
$ ./test.sh ../canonicaljson-rs/demo/target/debug/demo
Some known errors:
lone leading surrogate in hex escape
number out of range
Non-token input after 896 characters: "\u6} surrogate pair\u2014U+1D306
\xDD
instead of \uDDDD
)Licensed under MIT