1 个不稳定版本
使用旧的Rust 2015
0.2.1 | 2024年4月3日 |
---|
#231 in 压缩
28KB
614 行
rust-jsonm
jsonm的Rust实现端口。
原始库是用JS编写的,在此处:https://github.com/lennartcl/jsonm
jsonm是一种通过memoization快速安全地压缩JSON消息的方式。jsonm通过消除重复的名字和值,使消息的大小减少几个数量级。
jsonm打包
[
{ "firstName": "Francis", "lastName": "Doe" },
{ "firstName": "Anna", "lastName": "Smith" },
{ "firstName": "Agent", "lastName": "Smith", "isAlias": true },
{ "firstName": "Anna", "lastName": "Francis" }
]
为
[ 0,
["firstName", "lastName", "Francis", "Doe"],
[3, 4, "Anna", "Smith"],
[3, 4, "isAlias", "Agent", 8, true],
[3, 4, 7, 5]
]
注意它如何通过memoization消除所有常见子字符串,例如"firstName"! jsonm通过保持一个字典来压缩未来的消息,使其进一步缩小。发送上述消息第二次,它变为
[0,[3,4,5,6],[3,4,7,8],[3,4,9,10,8,11],[3,4,7,5],1]
并且
[
{ "firstName": "Bryan", "lastName": "Fuller" },
{ "firstName": "Anna", "lastName": "Adams" },
{ "firstName": "Tim", "lastName": "Peterson" },
{ "firstName": "Francis", "lastName": "Peterson" }
]
变为
[0,[3,4,"Bryan","Fuller"],[3,4,7,"Adams"],[3,4,"Tim","Peterson"],[3,4,5,16]]
通过避免重复,jsonm可以帮助减少从Web服务器发送到客户端的消息的大小。它有效地排除了客户端已知的所有信息。
安装
[dependencies]
"jsonm" = "0.1"
示例
#[macro_use]
extern crate serde_json;
extern crate jsonm;
use jsonm::packer::{PackOptions, Packer};
use jsonm::unpacker::Unpacker;
let mut packer = Packer::new();
let options = PackOptions::new();
let packed = packer.pack(&json!({ "bar": 1, "foo": "1" }), &options).unwrap(); // packed is ["bar", "foo", "1", "~1", 0]
let mut unpacker = Unpacker::new();
let unpacked: Value = unpacker.unpack(&packed).unwrap(); // unpacked is Object({ "bar": 1, "foo": "1" })
依赖项
~3–4.5MB
~84K SLoC