
PSON is a super efficient protocol serialization format for JSON. It outperforms JSON, BSON, BJSON and, if used wisely, even protobuf and thrift in encoding size!
PSON combines the best of JSON, BJSON, ProtoBuf and 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:
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.
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]
}
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:
npm install pson
var PSON = require("pson");
...
require.config({
...
"paths": {
"Long": "/path/to/Long.js", // optional
"ByteBuffer": "/path/to/ByteBuffer.js",
"PSON": "/path/to/PSON.js"
},
...
});
require(["PSON"], function(PSON) {
...
});
<script src="https://github.com/dcodeIO/PSON/raw/v0.7.0/raw.github.com/dcodeIO/Long.js/master/Long.min.js"></script>
<script src="https://github.com/dcodeIO/PSON/raw/v0.7.0/raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.min.js"></script>
<script src="https://github.com/dcodeIO/PSON/raw/v0.7.0/raw.github.com/dcodeIO/PSON/master/PSON.min.js"></script>
var PSON = dcodeIO.PSON;
...
// 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);
...
});
The API is pretty much straight forward:
PSON.Pair#encode(json: *): ByteBuffer encodes JSON to PSON dataPSON.Pair#toBuffer(json: *): Buffer encodes straight to a node.js BufferPSON.Pair#toArrayBuffer(json: *): ArrayBuffer encodes straight to an ArrayBufferPSON.Pair#decode(pson: ByteBuffer|Buffer|ArrayBuffer): * decodes PSON data to JSONnew PSON.ProgressivePair([initialDictionary: Array.<string>]) constructs a new progressive encoder and decoder pair
with an automatically filling keyword dictionaryPSON.ProgressivePair#exclude(obj: Object) Excludes an object's and its children's keywords from being added to the progressive
dictionaryPSON.ProgressivePair#include(obj: Object) Undoes the formernew PSON.StaticPair([dictionary: Array.<string>]) constructs a new static encoder and decoder pair
with a static (or empty) dictionaryLicense: Apache License, Version 2.0