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

#975 in Parser implementations

Download history 5238/week @ 2023-12-17 2432/week @ 2023-12-24 5054/week @ 2023-12-31 7113/week @ 2024-01-07 6630/week @ 2024-01-14 7190/week @ 2024-01-21 7388/week @ 2024-01-28 7570/week @ 2024-02-04 6910/week @ 2024-02-11 5977/week @ 2024-02-18 7069/week @ 2024-02-25 8329/week @ 2024-03-03 8544/week @ 2024-03-10 11232/week @ 2024-03-17 9183/week @ 2024-03-24 10458/week @ 2024-03-31

39,759 downloads per month
Used in 45 crates (5 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.6MB
~57K SLoC