2.2.2 • Published 5 years ago

json-crusher v2.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

json-crusher

Minify your json with auto-keymap serialization AND message-pack binary serialization. For multiplayer games, also check out: https://www.npmjs.com/package/netpack

npm

Install

npm i json-crusher

Quick Start

const JsonCrusher = require('json-crusher')

// Create instance
let crusher = new JsonCrusher()

PARAMS

JsonCrusher(keymap, maxkeys)

keymap => an existing keymap that you would like to import. (buffer)

maxkeys => the max number of keys you expect your json to include. Defaults to 1000. This is a soft limit meaning that if you do go over, it will just serialize any additional keys as their original value.

ON SERVER:

// Example data let json = { foo: { x: 10, y: 200, bar: { baz: true }, // test recursion and key reuse baz: [2345, {bar: true}, null, 340], } }

// Crush it! let payload = crusher.crush(json) let keymap_updates = crusher.getKeymapUpdates()

// Now send keymap_updates and payload to client

### ON CLIENT:
```js
let crusher = new JsonCrusher()

// Receive keymap_updates and payload
crusher.updateKeyMap(keymap_updates)
let uncrushed_payload = crusher.uncrush(payload)

console.log('Crushed:', payload)
console.log('Uncrushed:', uncrushed_payload)

API

Methods:

.crush(json) // returns serialized json
.uncrush(serialized_json) // returns deserialized json
.getKeymapUpdates() // returns mappings object
.updateKeyMap(keymap_updates)

Props:

.keymap // The current keymap (you can pass this into .updateKeyMap() on a second instance)

Syncing server's keymap with client in games

For multiplayer games, also check out: https://www.npmjs.com/package/netpack

SERVER

In your networking logic, you would call .getKeymapUpdates() every tick which returns an object of the newly added mappings or null if there are none.

Your logic should be:

if (keymap_updates !== null) // send these keymap_updates to client

CLIENT

The client maintains it's own instance of the Crusher, so whenever it receive keymap_updates, it calls:

crusher.updateKeyMap(keymap_updates)

This should be the first thing the client does before trying to uncrush any json messages receive in the packet. As long as the client keeps it's keymap synced with the server's, it can call .uncrush() to deserialize the received json messages

let data = crusher.uncrush(payload)
2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago