• Stars
    star
    459
  • Rank 95,377 (Top 2 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A super efficient binary serialization format for JSON.

PSON

PSON is a super efficient binary serialization format for JSON focused on minimal encoding size.

How does it work?

PSON combines the best of JSON, BJSON, ProtoBuf and a bit of ZIP to achieve a superior small footprint on the network level. Basic constants and small integer values are efficiently encoded as a single byte. Other integer values are always encoded as variable length integers. Additionally it comes with progressive and static dictionaries to reduce data redundancy to a minimum. In a nutshell:

  • 246 single byte values
  • Base 128 variable length integers (varints) as in protobuf
  • 32 bit floats instead of 64 bit doubles if possible without information loss
  • Progressive and static dictionaries
  • Raw binary data support
  • Long support

Reference implementation

This repository contains a plain node.js/CommonJS, RequireJS/AMD and Browser compatible JavaScript implementation of the PSON specification on top of ByteBuffer.js and optionally Long.js:

A PSON.StaticPair contains the PSON encoder and decoder for a static (or empty) dictionary and can be shared between all connections. It's recommended for production.

A PSON.ProgressivePair contains the PSON encoder and decoder for a progressive (automatically filling) dictionary. On the one hand this requires no dictionary work from the developer but on the other requires one pair per connection.

tl;dr Numbers, please!

The test suite contains the following basic example message:

{
    "hello": "world!",
    "time": 1234567890,
    "float": 0.01234,
    "boolean": true,
    "otherbool": false,
    "null": null,
    "obj": {
        "what": "that"
    },
    "arr": [1,2,3]
}
  • JSON stringify: 133 bytes
  • PSON without a dictionary: 103 bytes (about 22% smaller than JSON)
  • PSON with a progressive dictionary: 103 bytes for the first and 59 bytes for each subsequent message (about 22% smaller for the first and about 55% smaller for each subsequent message than JSON.
  • PSON with the same but static dictionary: 59 bytes for each message (about 55% smaller than JSON)
 F6 08 FE 00 FC 06 77 6F 72 6C 64 21 FE 01 F8 A4   ......world!....
 8B B0 99 79 FE 02 FB F6 0B 76 C3 B6 45 89 3F FE   ...y.....v..E.?.
 03 F1 FE 04 F2 FE 05 F0 FE 06 F6 01 FE 07 FC 04   ................
 74 68 61 74 FE 08 F7 03 02 04 06                  that.......

Another example that's also contained in the test suite is encoding our package.json, which is of course a string value centered file, to PSON using a general purpose static dictionary:

  • JSON stringify: 813 bytes
  • PSON with empty dict: 760 bytes (about 6% smaller than JSON)
  • PSON with static dict: 613 bytes (about 24% smaller than JSON)

Usage

node.js/CommonJS

npm install pson

var PSON = require("pson");
...

RequireJS/AMD

require.config({
    ...
    "paths": {
        "Long": "/path/to/Long.js", // optional
        "ByteBuffer": "/path/to/ByteBufferAB.js",
        "PSON": "/path/to/PSON.js"
    },
    ...
});
require(["PSON"], function(PSON) {
    ...
});

Browser

<script src="Long.min.js"></script>
<script src="ByteBufferAB.min.js"></script>
<script src="PSON.min.js"></script>
var PSON = dcodeIO.PSON;
...

Example

// Sender
var initialDictionary = ["hello"];
var pson = new PSON.ProgressivePair(initialDictionary);
var data = { "hello": "world!" };
var buffer = pson.encode(data);
someSocket.send(buffer);
// Receiver
var initialDictionary = ["hello"];
var pson = new PSON.ProgressivePair(initialDictionary);
someSocket.on("data", function(data) {
    data = pson.decode(data);
    ...
});

API

The API is pretty much straight forward:

  • PSON.Pair#encode(json: *): ByteBuffer encodes JSON to PSON data
    • PSON.Pair#toBuffer(json: *): Buffer encodes straight to a node.js Buffer
    • PSON.Pair#toArrayBuffer(json: *): ArrayBuffer encodes straight to an ArrayBuffer
  • PSON.Pair#decode(pson: ByteBuffer|Buffer|ArrayBuffer): * decodes PSON data to JSON

Progressive

  • new PSON.ProgressivePair([initialDictionary: Array.<string>]) constructs a new progressive encoder and decoder pair with an automatically filling keyword dictionary
  • PSON.ProgressivePair#exclude(obj: Object) Excludes an object's and its children's keywords from being added to the progressive dictionary
  • PSON.ProgressivePair#include(obj: Object) Undoes the former

Static

  • new PSON.StaticPair([dictionary: Array.<string>]) constructs a new static encoder and decoder pair with a static (or empty) dictionary

Downloads

Documentation

License: Apache License, Version 2.0

More Repositories

1

bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
JavaScript
3,300
star
2

long.js

A Long class for representing a 64-bit two's-complement integer value.
JavaScript
854
star
3

webassembly

A minimal toolkit and runtime to produce and run WebAssembly modules.
C
814
star
4

ClosureCompiler.js

Closure Compiler for node.js - the all-round carefree package.
JavaScript
143
star
5

MetaScript

Sophisticated meta programming in JavaScript, e.g. to build different versions of a library from a single source tree.
JavaScript
134
star
6

node.js-closure-compiler-externs

node.js externs for use with Closure Compiler.
JavaScript
76
star
7

Preprocessor.js

A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
JavaScript
70
star
8

node-memcpy

Copies data between node Buffers and/or ArrayBuffers up to ~75 times faster than in pure JS.
JavaScript
54
star
9

utfx

A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.
JavaScript
53
star
10

btree.js

A ridiculously lean B-tree of variable orders in plain JavaScript.
JavaScript
45
star
11

WebAssembly-prototype

[OUTDATED] JavaScript tools for working with WebAssembly (WASM) binaries.
JavaScript
41
star
12

colour.js

A cored, fixed, documented and optimized version of the popular `colors.js`: Get colors in your node.js console like what...
JavaScript
33
star
13

node-harmonize

Enables --harmony flags programmatically.
JavaScript
25
star
14

BattleCon

A Battlefield / Frostbite engine RCON layer on node.js.
JavaScript
20
star
15

WebRcon

RCON over WebSocket client library and command line interface.
JavaScript
18
star
16

setup-node-nvm

Set up your GitHub Actions workflow with a specific version of node.js using nvm.
JavaScript
16
star
17

grunt-closurecompiler

The ClosureCompiler.js Grunt Task
JavaScript
15
star
18

test.js

A compact testing module for node.js.
JavaScript
14
star
19

dcodeio.github.io

JavaScript
13
star
20

purerc

JavaScript
12
star
21

IntN.js

A library for representing arbitrary byte size integers in JavaScript, both signed and unsigned.
JavaScript
12
star
22

JustMath.js

A rich toolset for two dimensional vector math.
JavaScript
11
star
23

opt.js

Probably the sole command line option parser you'll ever need to...
JavaScript
10
star
24

SharpJS

An embeddable, node.js-like JavaScript environment for Mono and .NET.
C#
10
star
25

lxiv

A compact library to encode and decode base64 data in JavaScript.
JavaScript
10
star
26

___wildcards

A party game for horrible people.
JavaScript
9
star
27

asc-native

AssemblyScript, JavaScript, WebAssembly, C, Binaryen, WABT, Clang, LLVM, CMake, Node.js, Visual Studio and Win32 walk into a bar...
C
9
star
28

ascli

A uniform foundation for unobtrusive (ASCII art in) cli apps.
JavaScript
9
star
29

wapi

A minimal yet viable Web-first Wasm/JS bridge.
TypeScript
7
star
30

quickjs

C
7
star
31

endecrypt

Password based en-/decryption of arbitrary data with and for node.js. http://dcode.io
JavaScript
5
star
32

node-BufferView

A DataView for node Buffers.
JavaScript
5
star
33

FalseSkill

A literal implementation of the Glicko-2 rating system in TypeScript.
JavaScript
4
star
34

doco

A JavaScript Documentation Generator.
JavaScript
4
star
35

PSONSharp

An implementation of Protocol JSON for .NET and Mono.
C#
4
star
36

SourceCon

Simple SRCDS RCON for node.js
JavaScript
3
star
37

BPlusTree.js

Santanu Basu's B+ tree implementation. Streamlined.
JavaScript
3
star
38

esm2umd

Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback.
JavaScript
2
star
39

SourceQry

JavaScript
1
star
40

raytrace

TypeScript
1
star
41

Friends

Universal friends plugin for the Oxide modding framework.
C#
1
star
42

llvm.js

C
1
star
43

asext

JavaScript
1
star