New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jsonm

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonm

json compressor for packing messages with memoization

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
439
increased by5.28%
Maintainers
1
Weekly downloads
 
Created
Source

jsonm

"Listen very carefully, I shall say 'zis only once!"

jsonm is a performant, safe way to compress JSON messages, in similar vein to jsonh and jsonc. jsonm makes messages up to several orders of magnitude smaller by getting rid of repeated names and values.

Examples

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

becomes

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

Note how common substrings "firstName", "lastName", and "John" are not repeated but replaced by a dictionary index.

The dictionary is built up on the fly and re-used for future messages sent. When sending the same message again it'll be even smaller:

[1,[3,4,5,6],[3,4,7,8],[3,4,9,8]]

Other messages that come after that also benefit from the dictionary:

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

becomes

[2,[3,4,"Bryan","Fuller"],[3,4,5,"Adams"],[3,4,"Tim","Peterson"]]

Usage

jsonm is designed for sending messages between a sender and a receiver. The sender packs messages and the receiver unpacks them.

Sender, packing a message:

var packer = new jsonm.Packer();
var packed = packer.pack(message);

Receiver, unpacking a message:

var unpacker = new jsonm.Unpacker();
unpacker.unpack(message);

Note that both the packer and unpacker maintain a stateful dictionary. Don't lose them! But when the connection ends just start over with a new packer and unpacker.

These projects pack uniform JavaScript objects, eliminating the need for repeating the keys of each object. As an example, JSONH can pack

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

into

[3,"firstName","lastName","isAlias","John","Doe",false,"Anna","Smith",false,"Agent","Smith",true]

JSONH and JSONM don't apply memoization and only help with uniform data. Unlike jsom, however, they are stateless, which can make it easier to use them in some cases.

FAQs

Package last updated on 07 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc