#serde-json #canonical #json #serde

serde_canonical_json

为serde_json实现了一个CanonicalFormatter

1个稳定版本

1.0.0 2023年3月7日

#1246编码

Download history 1/week @ 2024-03-11 1/week @ 2024-03-18 20/week @ 2024-04-01 2/week @ 2024-04-08 34/week @ 2024-04-15 21/week @ 2024-04-22 27/week @ 2024-04-29 98/week @ 2024-05-06 89/week @ 2024-05-13 92/week @ 2024-05-20 9/week @ 2024-05-27 55/week @ 2024-06-03 93/week @ 2024-06-10 205/week @ 2024-06-17 111/week @ 2024-06-24

464 每月下载量

MIT 许可证

26KB
710 代码行

serde_canonical_json

此crate为serde_json提供了一个Canonical JSON格式化器。

用法

use serde::Serialize;
use serde_json::Serializer;
use serde_canonical_json::CanonicalFormatter;


#[derive(Serialize)]
struct Data
{
    c: isize,
    b: bool,
    a: String,
}


fn main()
{
    let data = Data { c: 120, b: false, a: "Hello!".to_owned() };

    let mut ser = Serializer::with_formatter(Vec::new(), CanonicalFormatter::new());

    data.serialize(&mut ser).expect("Failed to serialize");
    
    let json = String::from_utf8(ser.into_inner()).expect("Failed to convert buffer to string");

    assert_eq!(json, r#"{"a":"Hello!","b":false,"c":120}"#);
}

lib.rs:

serde_canonical_json

此crate为serde_json提供了一个Canonical JSON格式化器。

用法

use serde::Serialize;
use serde_json::Serializer;
use serde_canonical_json::CanonicalFormatter;

// CanonicalFormatter will ensure our keys are in lexical order
#[derive(Serialize)]
struct Data
{
    c: isize,
    b: bool,
    a: String,
}

let data = Data { c: 120, b: false, a: "Hello!".to_owned() };

let mut ser = Serializer::with_formatter(Vec::new(), CanonicalFormatter::new());

data.serialize(&mut ser).unwrap();

let json = String::from_utf8(ser.into_inner()).unwrap();

assert_eq!(json, r#"{"a":"Hello!","b":false,"c":120}"#);

依赖项

~2.6–4MB
~71K SLoC