4 releases

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

#1164 in Parser implementations

Download history 10618/week @ 2024-03-14 9900/week @ 2024-03-21 9061/week @ 2024-03-28 10661/week @ 2024-04-04 9118/week @ 2024-04-11 8359/week @ 2024-04-18 9037/week @ 2024-04-25 9062/week @ 2024-05-02 9487/week @ 2024-05-09 10532/week @ 2024-05-16 9039/week @ 2024-05-23 10767/week @ 2024-05-30 10146/week @ 2024-06-06 10852/week @ 2024-06-13 12427/week @ 2024-06-20 9522/week @ 2024-06-27

44,510 downloads per month
Used in 54 crates (6 directly)

MIT/Apache

22KB
328 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–1.5MB
~57K SLoC