#json #serde-json #deserialize-json #canonical #specification #tuf #olpc

bin+lib olpc-cjson

serde_json Formatter to serialize as OLPC-style canonical JSON

5 releases

0.1.4 Oct 10, 2024
0.1.3 Mar 3, 2023
0.1.2 Oct 3, 2022
0.1.1 Jul 30, 2021
0.1.0 Nov 9, 2019

#442 in Encoding

Download history 12521/week @ 2024-07-18 12815/week @ 2024-07-25 13551/week @ 2024-08-01 12616/week @ 2024-08-08 14219/week @ 2024-08-15 14255/week @ 2024-08-22 12330/week @ 2024-08-29 13686/week @ 2024-09-05 14956/week @ 2024-09-12 13904/week @ 2024-09-19 14939/week @ 2024-09-26 14755/week @ 2024-10-03 18324/week @ 2024-10-10 19417/week @ 2024-10-17 19417/week @ 2024-10-24 16439/week @ 2024-10-31

76,985 downloads per month
Used in 75 crates (7 directly)

MIT/Apache

23KB
353 lines

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}"#);

Dependencies

~1.3–2MB
~54K SLoC