#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

#259 in Compression

Download history 47/week @ 2024-07-20 55/week @ 2024-07-27 42/week @ 2024-08-03 42/week @ 2024-08-10 159/week @ 2024-08-17 93/week @ 2024-08-24 65/week @ 2024-08-31 25/week @ 2024-09-07 40/week @ 2024-09-14 115/week @ 2024-09-21 270/week @ 2024-09-28 158/week @ 2024-10-05 130/week @ 2024-10-12 201/week @ 2024-10-19 48/week @ 2024-10-26 186/week @ 2024-11-02

566 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
~89K SLoC