serde_json_canonicalizer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
extern crate serde;
extern crate serde_json_canonicalizer;
use serde_json_canonicalizer::{to_string, to_vec};

#[derive(serde::Serialize)]
struct Data {
    c: isize,
    b: bool,
    a: String,
}
fn main() {
    let data = Data {
        c: 120,
        b: false,
        a: "Hello!".to_string(),
    };
    let expected = r#"{"a":"Hello!","b":false,"c":120}"#;

    // serlialize to string or bytes, drop-in replacement for serde_json
    let json_string = to_string(&data).unwrap();
    let json_bytes = to_vec(&data).unwrap();

    assert_eq!(json_string, expected);
    assert_eq!(json_bytes, expected.as_bytes());
    println!("{json_string}");
}
(stdout)
{"a":"Hello!","b":false,"c":120}