#json #data-encoding #serialization #codec #compact #byte #helper

serde_tran

将您的数据序列化为编码字节的辅助工具

4个版本 (2个稳定版)

1.1.0 2024年6月6日
1.0.0 2024年6月6日
0.2.0 2024年6月5日
0.1.1 2024年6月5日

#968编码

Download history 348/week @ 2024-06-05 2/week @ 2024-06-12 2/week @ 2024-07-03

每月91次下载

MIT 许可证

420KB
559

serde_tran: 将您的JSON数据编码/解码的辅助工具

背景

当我们使用HTTP协议(或其他传输协议)时,将Json数据格式存储在HTTP请求体或响应体中既方便又常见。

但是,当我们传输大量数据(例如,大数组,Map类型数据中的键相对较长)时,直接使用Json数据并不高效。

因此,您可以考虑使用这个库。这个库将帮助您将Json类型数据(即impl Serialize)封装一层(结果是Json,但数据更紧凑)。

编码示例

此库将编码

{
  "username": "serde_tran:username",
  "password": "serde_tran:password:123456"
}

为类似的内容

{
  "f": "base64",
  "v": "NgAAAAAAAAATAAAAAAAAAHNlcmRlX3RyYW46dXNlcm5hbWUTAAAAAAAAAHNlcmRlX3RyYW46cGFzc3dvcmRnmz7nMG94SA"
}

用法

首先,我们在示例中定义一个结构体,它impl Serialize

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct MyStruct {
    // fields...    
}
pub fn example() {
    let s = MyStruct {};
    
    // 1. convert it to serde_tran::Json
    let json = serde_tran::to_json(&s).unwrap();
    
    // json is something like: {"f": "base64", "v": "(base64 encoded string)"}
    // you can use json.to_string() to get the json string.
    
    // 2. convert it back to MyStruct
    let ds: MyStruct = json.to_value().unwrap();
    
    assert_eq!(s, ds); // they are the same (remember to derive PartialEq to use macro assert_eq!)
}

更多示例,请参阅文件夹examples

工作原理

此图显示了serde_tran的所有公共函数

功能

  • serde_json (默认):启用Json。如果您使用serde_json,则必须启用base58base64(至少一个)。
  • base64 (默认):启用base64编码,使用crate(base64)https://docs.rs/base64/latest/base64
  • bs58:启用base58编码,使用crate(bs58)https://docs.rs/bs58。警告:此编码速度较慢。
  • flate2:启用gzip,比serde_json慢10+倍。

Wasm

在您的Web客户端中使用wasm

正在进行中

基准测试

  • n:数组的长度
  • slstring_length,数组中元素的字符串长度

总结:节省了35%~50%的空间。

n=1, sl=10, 
	json.time=3000ns/op, json.space=816bytes/bench, 
	tran.time=2000ns/op, tran.space=488bytes/bench
	[tran/json] time=-33.33333333333333%, space=-40.19607843137255%
n=1, sl=200, 
	json.time=13000ns/op, json.space=11328bytes/bench, 
	tran.time=13000ns/op, tran.space=7128bytes/bench
	[tran/json] time=0%, space=-37.07627118644068%
n=1, sl=400, 
	json.time=20000ns/op, json.space=22221bytes/bench, 
	tran.time=23000ns/op, tran.space=14017bytes/bench
	[tran/json] time=15%, space=-36.92003060168309%
n=10, sl=10, 
	json.time=10000ns/op, json.space=8276bytes/bench, 
	tran.time=7000ns/op, tran.space=4652bytes/bench
	[tran/json] time=-30%, space=-43.78927017883035%
n=10, sl=200, 
	json.time=118000ns/op, json.space=112964bytes/bench, 
	tran.time=117000ns/op, tran.space=70636bytes/bench
	[tran/json] time=-0.847457627118644%, space=-37.47034453454198%
n=10, sl=400, 
	json.time=217000ns/op, json.space=223148bytes/bench, 
	tran.time=227000ns/op, tran.space=140019bytes/bench
	[tran/json] time=4.6082949308755765%, space=-37.25285460770431%
n=100, sl=10, 
	json.time=83000ns/op, json.space=81750bytes/bench, 
	tran.time=72000ns/op, tran.space=45460bytes/bench
	[tran/json] time=-13.253012048192772%, space=-44.391437308868504%
n=100, sl=200, 
	json.time=1004000ns/op, json.space=1128094bytes/bench, 
	tran.time=2115000ns/op, tran.space=705407bytes/bench
	[tran/json] time=110.65737051792827%, space=-37.46912934560418%
n=100, sl=400, 
	json.time=3192000ns/op, json.space=2231179bytes/bench, 
	tran.time=3775000ns/op, tran.space=1402308bytes/bench
	[tran/json] time=18.264411027568922%, space=-37.14946223498877%
n=1024, sl=10, 
	json.time=1169000ns/op, json.space=837345bytes/bench, 
	tran.time=1086000ns/op, tran.space=466460bytes/bench
	[tran/json] time=-7.100085543199315%, space=-44.29297362496939%
n=1024, sl=200, 
	json.time=11045000ns/op, json.space=11554178bytes/bench, 
	tran.time=13268000ns/op, tran.space=7228320bytes/bench
	[tran/json] time=20.12675418741512%, space=-37.4397728683079%
n=1024, sl=400, 
	json.time=20444000ns/op, json.space=22834571bytes/bench, 
	tran.time=25655000ns/op, tran.space=14344660bytes/bench
	[tran/json] time=25.489141068284095%, space=-37.18007664781616%
n=2048, sl=10, 
	json.time=1811000ns/op, json.space=1674880bytes/bench, 
	tran.time=1890000ns/op, tran.space=932763bytes/bench
	[tran/json] time=4.362230811706239%, space=-44.30866688956821%
n=2048, sl=200, 
	json.time=20665000ns/op, json.space=23114902bytes/bench, 
	tran.time=25878000ns/op, tran.space=14454992bytes/bench
	[tran/json] time=25.22622792160658%, space=-37.464619144827005%
n=2048, sl=400, 
	json.time=42064000ns/op, json.space=45670596bytes/bench, 
	tran.time=51634000ns/op, tran.space=28690505bytes/bench
	[tran/json] time=22.7510460251046%, space=-37.17948195815093%
n=5120, sl=10, 
	json.time=4211000ns/op, json.space=4189273bytes/bench, 
	tran.time=4124000ns/op, tran.space=2331513bytes/bench
	[tran/json] time=-2.066017573023035%, space=-44.345641833320485%
n=5120, sl=200, 
	json.time=52865000ns/op, json.space=57772057bytes/bench, 
	tran.time=63971000ns/op, tran.space=36140128bytes/bench
	[tran/json] time=21.00822850657335%, space=-37.44358453430176%
n=5120, sl=400, 
	json.time=102073000ns/op, json.space=114183541bytes/bench, 
	tran.time=129650000ns/op, tran.space=71728965bytes/bench
	[tran/json] time=27.01693885748435%, space=-37.18099441319656%
n=10240, sl=10, 
	json.time=9128000ns/op, json.space=8376513bytes/bench, 
	tran.time=8968000ns/op, tran.space=4665019bytes/bench
	[tran/json] time=-1.7528483786152498%, space=-44.308341669140844%
n=10240, sl=200, 
	json.time=104960000ns/op, json.space=115546296bytes/bench, 
	tran.time=138643000ns/op, tran.space=72273849bytes/bench
	[tran/json] time=32.09127286585366%, space=-37.45031082606058%
n=10240, sl=400, 
	json.time=208326000ns/op, json.space=228338452bytes/bench, 
	tran.time=296174000ns/op, tran.space=143440585bytes/bench
	[tran/json] time=42.16852433205649%, space=-37.180714091904235%

依赖项

~1.2–2.3MB
~48K SLoC