https://github.com/ahdinosaur/json-canon
Serialize JSON into a canonical format.
https://github.com/ahdinosaur/json-canon
canonical jcs json json-serialization rfc8785 serialization
Last synced: 5 months ago
JSON representation
Serialize JSON into a canonical format.
- Host: GitHub
- URL: https://github.com/ahdinosaur/json-canon
- Owner: ahdinosaur
- Created: 2023-05-10T11:27:46.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-23T10:37:55.000Z (about 2 years ago)
- Last Synced: 2025-01-11T06:34:54.493Z (6 months ago)
- Topics: canonical, jcs, json, json-serialization, rfc8785, serialization
- Language: Rust
- Homepage:
- Size: 167 KB
- Stars: 16
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `json-canon`
Serialize JSON into a canonical format.
Safe for generating a consistent cryptographic hash or signature across platforms.
Follows [RFC8785: JSON Canonicalization Scheme (JCS)](https://tools.ietf.org/html/rfc8785)

## Features
The JSON Canonicalization Scheme concept in a nutshell:
- Serialization of primitive JSON data types using methods compatible with ECMAScript's `JSON.stringify()`
- Lexicographic sorting of JSON `Object` properties in a *recursive* process
- JSON `Array` data is also subject to canonicalization, *but element order remains untouched*## Serializers
### JavaScript: [`json-canon`](./js/json-canon)
[](https://www.npmjs.com/package/json-canon) [](https://www.npmjs.com/package/json-canon) [](https://github.com/ahdinosaur/json-canon/actions/workflows/js.yml)
```js
const serialize = require('json-canon')const json = {
from_account: "543 232 625-3",
to_account: "321 567 636-4",
amount: 500,
currency: "USD"
}console.log(serialize(json))
// {"amount":500,"currency":"USD","from_account":"543 232 625-3","to_account":"321 567 636-4"}
```### Rust: [`json-canon`](./rust/json-canon)
[](https://crates.io/crates/json-canon) [](https://crates.io/crates/json-canon) [](https://docs.rs/json-canon) [](https://github.com/ahdinosaur/json-canon/actions/workflows/rust.yml)
```rust
use json_canon::to_string;
use serde_json::json;let data = json!({
"from_account": "543 232 625-3",
"to_account": "321 567 636-4",
"amount": 500,
"currency": "USD"
});println!("{}", to_string(&data)?);
// {"amount":500,"currency":"USD","from_account":"543 232 625-3","to_account":"321 567 636-4"}
```## Fuzzers
- JavaScript: [`json-canon-fuzz`](./js/json-canon-fuzz)
## References
- [`cyberphone/ietf-json-canon`](https://github.com/cyberphone/ietf-json-canon)
- [`cyberphone/json-canonicalization`](https://github.com/cyberphone/json-canonicalization)