Arrow Code
Arrow Code use unicode combining character "͢ " in string to store types that normally not allowed in JSON
examples
const Arrow = require('arrow-code').default;
Arrow.encodeJSON( [
NaN,
-Infinity,
new Date(),
new Uint8Array([1,2,3,4])
], 1);
[
"͢NaN",
"͢-Inf",
"͢Date:2018-02-07T19:07:18.207Z",
"͢Bin:wFg{A"
]
Advantage
(Arrow.encodeJSON vs MsgPack and BSON)
Arrow Code allows additional data types to be serialized in JSON, such as binary date (Uint8Array) and Date, while still keeps the verbose nature of JSON.
The output string is still a 100% valid JSON, and compatible with any JSON editing/parsing tool or library. This makes Arrow Code much easier to debug and trouble shoot than binary formats like BSON and MsgPack
Performance
Modern browsers and nodejs are highly optimized for JSON. This allows Arrow Code to be encoded and decoded faster than the other 2 formats in most of the cases.
benchmark result
Benchmark with sample data on Chrome 77, Firefox 69
Time are all in ms, smaller is better
| Chrome Encode | Chrome Decode | Firefox Encode | Firefox Decode |
---|
Arrow Code | 0.1434 | 0.1742 | 0.1708 | 0.1301 |
MsgPack | 0.2893 | 0.1818 | 0.6689 | 0.1933 |
BSON | 0.1573 | 0.1879 | 0.3945 | 0.5648 |
Constructor
new Arrow({
encodeBinary?: boolean | 'base64',
encodeDate?: boolean,
encodeBigInt?: boolean,
encodePrimitive?: boolean,
})
API
API | comments |
---|
encodeJSON( inpt: unknown, space?: number, sortKeys = false) | encode as JSON, can be used as static api |
decodeJSON( inpt: string) | decode JSON, can be used as static api |
encode( inpt: unknown) | encode to String, can be used as static api |
decode( inpt: string) | decode String, can be used as static api |
register( key: string, type: Constructor, encoder, decoder) | register a custom type |
Custom Types
Arrow Code's register API make it really easy to add custom type
class MyClass {
constructor(str) {
this.myStr = str;
}
}
let arrow = new Arrow();
arrow.register(
'My',
MyClass,
(obj) => obj.myStr,
(str) => new MyClass(str)
);
myJson.stringify(new MyClass("hello"));
Base93 encoding
Arrow Code use Base93 by default to encode binary data, it's more compact than Base64.
If you prefer Base64, set binaryFormat to base64 in the Arrow Code constructor
const Arrow = require('arrow-code').default;
const arrow = new Arrow({binaryFormat: 'base64'});
arrow.stringify({binary: new Uint8Array([1,2,3,4])});