#json #memoization #safe #messages #port #size

jsonm

jsonm implementation port for Rust. jsonm is a fast and safe way to compress JSON messages using memoization. jsonm makes messages up to several orders of magnitude smaller by getting rid of repeated names and values.

6 releases

Uses old Rust 2015

0.2.0 Nov 26, 2021
0.1.4 May 8, 2019
0.1.3 Nov 8, 2018
0.1.2 Sep 26, 2018

#235 in Compression

Download history 89/week @ 2024-06-12 149/week @ 2024-06-19 82/week @ 2024-06-26 135/week @ 2024-07-03 132/week @ 2024-07-10 55/week @ 2024-07-17 33/week @ 2024-07-24 61/week @ 2024-07-31 35/week @ 2024-08-07 118/week @ 2024-08-14 107/week @ 2024-08-21 100/week @ 2024-08-28 26/week @ 2024-09-04 38/week @ 2024-09-11 72/week @ 2024-09-18 110/week @ 2024-09-25

272 downloads per month
Used in 2 crates

MIT license

26KB
614 lines

rust-jsonm

Build Status

jsonm implementation port for Rust.

Original library written in JS is here: https://github.com/lennartcl/jsonm

jsonm is a fast and safe way to compress JSON messages using memoization. jsonm makes messages up to several orders of magnitude smaller by getting rid of repeated names and values.

jsonm packs

[
    { "firstName": "Francis", "lastName": "Doe" },
    { "firstName": "Anna", "lastName": "Smith" },
    { "firstName": "Agent", "lastName": "Smith", "isAlias": true },
    { "firstName": "Anna", "lastName": "Francis" }
]

into

[ 0,
    ["firstName", "lastName", "Francis", "Doe"],
    [3, 4, "Anna", "Smith"],
    [3, 4, "isAlias", "Agent", 8, true],
    [3, 4, 7, 5]
]

Notice how it eliminates all common substrings like "firstName" using memoization! jsonm keeps a dictionary to compress future messages even further. Send the message above a second time, and it becomes:

[0,[3,4,5,6],[3,4,7,8],[3,4,9,10,8,11],[3,4,7,5],1]

And

[
    { "firstName": "Bryan", "lastName": "Fuller" },
    { "firstName": "Anna", "lastName": "Adams" },
    { "firstName": "Tim", "lastName": "Peterson" },
    { "firstName": "Francis", "lastName": "Peterson" }
]

becomes

[0,[3,4,"Bryan","Fuller"],[3,4,7,"Adams"],[3,4,"Tim","Peterson"],[3,4,5,16]]

By avoiding repetition, jsonm can for example help decrease the size of messages sent from a web server to the client. It effectively leaves out all information the client already knows about.

Installation

[dependencies]
"jsonm" = "0.1"

Examples

#[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" })

Dependencies

~3.5–5MB
~90K SLoC