Crates.io | olpc-cjson |
lib.rs | olpc-cjson |
version | 0.1.4 |
source | src |
created_at | 2019-11-09 01:26:25.101199 |
updated_at | 2024-10-10 20:45:19.950495 |
description | serde_json Formatter to serialize as OLPC-style canonical JSON |
homepage | |
repository | https://github.com/awslabs/tough |
max_upload_size | |
id | 179614 |
size | 40,645 |
olpc-cjson
provides a serde_json::Formatter
to serialize data as canonical JSON, as defined by OLPC and used in TUF. It is developed as part of tough, a Rust library for using TUF repositories.
OLPC's canonical JSON specification is subtly different from other "canonical JSON" specifications, and is also not a strict subset of JSON (specifically, ASCII control characters 0x00–0x1f are printed literally, which is not valid JSON). Therefore, serde_json
cannot necessarily deserialize JSON produced by this formatter.
This crate is not developed or endorsed by OLPC; use of the term is solely to distinguish this specification of canonical JSON from other specifications of canonical JSON.
use olpc_cjson::CanonicalFormatter;
use serde::Serialize;
use serde_json::json;
let value = json!({"b": 12, "a": "qwerty"});
let mut buf = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
value.serialize(&mut ser).unwrap();
assert_eq!(buf, br#"{"a":"qwerty","b":12}"#);