1.19.3 • Published 11 months ago

typed-binary-json v1.19.3

Weekly downloads
111
License
MIT
Repository
-
Last release
11 months ago

Typed Binary JSON

Typed Binary JSON or TBJSON, is a binary serialization format that is compatible with JSON. It stores known object prototypes in a JSON header, and serializes the data in a binary format following the header.

TBJSON is useful for serializing known objects, classes, or types, otherwise it will offer little advantage if any in terms of size or performance over JSON.

For a browser compatible version of this package, use TBJSON in the Browser.

Format

File Format

Each file starts off with ".tbj" to singinify that it is a Typed Binary JSON file, followed by a unit32 which is the length of the header.

         length of header                 raw binary data
   .tbj                header in JSON             
.  t  b  j  [ uint32 ]  {  .  .  .  }  .  .  d  a  t  a  .  .
0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  0
0                             10                            20
OffsetValueMeaning
0.tbjStates the file type.
4uint32Size of the JSON header.
8JSONA utf-8 serialized JSON map of the binary data to follow.
xbinaryThe binary data. Always the next byte after the last header byte.

Header Format

The header contains information necessary to parse the binary data. It is raw JSON and makes it easy to peak at the file and see how the data is structured.

EntryMeaning
typeRefsA map that translates known type names to their codes.
typesCustom primitive types that have been defined for this serialization.
protoRefsA map that translates known class and object names (either passed in or the object's constructor name) to their codes.
protosDefinitions for known prototypes or classes that are referenced in the root definition.
objsDefinitions for unknown objects that are referenced in known prototypes.
rootThe object that was serialized. Contains the definition needed to decode the binary format.

Types

The types used by TBJSON.

TypeCodeDefinition
Primitives--
NULL0Null value.
BOOL1Boolean.
UINT828 bit unsigned integer.
INT838 bit signed integer.
UINT16416 bit unsigned integer.
INT16516 bit signed integer.
UINT32632 bit unsigned integer.
INT32732 bit signed integer.
FLOAT32832 bit floating point.
FLOAT64964 bit double precision floating point.
Complex Types--
STRING10String.
ARRAY11Array. Used like Tbjson.TYPES.ARRAY or [Tbjson.TYPES.ARRAY, <TYPE>]. Example: x: [Tbjson.TYPES.ARRAY, MyClass].
OBJECT12Object. Used like Tbjson.TYPES.OBJECT or [Tbjson.TYPES.OBJECT, <TYPE>] if all the values in the object are the same type. Example: x: [Tbjson.TYPES.OBJECT, MyClass].
NULLABLE13Nullable value, Used like [Tbjson.TYPES.NULLABLE, <TYPE>]. Example: x: [Tbjson.TYPES.NULLABLE, Tbjson.TYPES.STRING].
TYPED_ARRAY14Typed array like Float32Array or Int16Array. Used like [Tbjson.TYPES.TYPED_ARRAY, <TYPE>. Example: x: [Tbjson.TYPES.TYPED_ARRAY, Tbjson.TYPES.INT32].
UNKNOWN15Unknown type. Wildcard that can represent a JS number, boolean, or string.

Reference

import Tbjson from 'typed-binary-json';
const Tbjson = require('typed-binary-json');

let tbjson = new Tbjson();

let serializedToBuffer = tbjson.serializeToBuffer({ a: 'a', b: 1, c: true }); // serialize the object
tbjson.parseBuffer(serializedToBuffer); // parse the buffer

// use these to register classes and types
tbjson.registerPrototype();
tbjson.registerType();

Working Example

import Tbjson from 'typed-binary-json';

class A {
	x = 0;
	y = 0;
	z = 0;
}

// make "A" a known class type
A.tbjson = {
	definition: {
		x: Tbjson.TYPES.FLOAT32,
		y: Tbjson.TYPES.FLOAT32,
		z: Tbjson.TYPES.FLOAT32,
	}
};

class B {
	as = [new A()];
	string = 'string';
	bool = false;
	number = 100.5;
}

// make "B" a known class type
B.tbjson = {
	definition: {
		as: [Tbjson.TYPES.ARRAY, A], // use the [ array, type ] notation to say that "B.as" is an array of A
		string: Tbjson.TYPES.STRING,
		bool: Tbjson.TYPES.BOOL,
		number: Tbjson.TYPES.FLOAT64
	}
}

// make a root object (untyped)
let root = {
	b: new B()
};

(async function() {
	let tbjson = new Tbjson();

	// serialize to a file
	await tbjson.serializeToFile('test.tbj', root);

	// parse from a file
	let obj = tbjson.parseFileAsBuffer('test.tbj');

	console.log(obj);
})();

Methods

Serialization

serializeToBuffer(obj)

Serialize obj, create a buffer and write to it.

serializeToStream(stream, obj)

Serialize obj and write to stream.

serializeToFile(filename, obj)

Serialize obj, create a write stream for filename, and write out to the file stream.

Parsing

parseBuffer(buffer)

Parse the buffer. Returns the parsed object.

parseStream(stream)

Parse the stream. Returns the parsed object.

parseFileAsBuffer(filename)

Read the whole file filename into memory and parse its conents. Preferred for performance. Returns the parsed object.

parseFileAsStream(filename)

Create a read stream for filename and parse its contents. Useful for very large files, but slower. Returns the parsed object.

Prototypes

registerPrototype(obj)

Register a prototype. obj is the definition for the prototype.

registerPrototypeRecur(obj)

Register a prototype and then walk it's definition and register those prototypes. obj is the definition for the prototype.

Types

registerType(obj)

Register a custom type (a primitive, like int48, etc...). obj is the definition for the custom type.

Static

cast(obj, prototype)

Cast the given obj as the prototype.

Performance

Performance varies on the data type, but you'll get best performance if your types have lots of numeric values, and even better performance if you can take advantage of float32, int32, int16, and int8 to save space.
Tested on a SSD, 16 GB, and a E3-1505M @ 2.8 GHz machine.

100 of root.first
10K per each root.first of root.second

{
	"root": {
		"first": [{
			"second": [{
				"x": 100000.666666666666,
				"y": -999999.999,
				"z": 1234.5678901234,
				"details": {
					"alpha": "oranges",
					"beta": 10,
					"gamma": [-3.14159, false, true, "!@#$%^&*()"]
				}
			}],
			"anotherString": "apples",
			"number": 86,
			"bool": true,
			"array": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
		}]
	}
}
BenchmarkFilesizeTime
JSON Write140 MB2,648 ms
TBJSON Write37 MB1,154 ms
JSON ReadN/A2,073 ms
TBJSON ReadN/A1,453 ms

TODO

  • Better stream handling
  • Finish implementing custom types

Contributing

Feel free to make changes and submit pull requests whenever.

License

Typed Binary JSON uses the MIT license.

1.19.3

11 months ago

1.19.2

11 months ago

1.19.1

2 years ago

1.19.0

2 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.17.5

3 years ago

1.17.4

3 years ago

1.17.3

4 years ago

1.17.2

4 years ago

1.17.1

4 years ago

1.16.0

4 years ago

1.17.0

4 years ago

1.15.6

4 years ago

1.15.5

4 years ago

1.15.4

4 years ago

1.15.3

4 years ago

1.15.2

4 years ago

1.15.1

4 years ago

1.15.0

4 years ago

1.14.10

4 years ago

1.14.9

4 years ago

1.14.8

4 years ago

1.14.7

4 years ago

1.14.6

4 years ago

1.14.5

4 years ago

1.14.4

4 years ago

1.14.3

4 years ago

1.14.2

4 years ago

1.14.1

4 years ago

1.14.0

4 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.2

5 years ago

1.12.1

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.17

5 years ago

1.10.16

5 years ago

1.10.15

5 years ago

1.10.14

5 years ago

1.10.13

5 years ago

1.10.12

5 years ago

1.10.11

5 years ago

1.10.10

5 years ago

1.10.9

5 years ago

1.10.8

5 years ago

1.10.7

5 years ago

1.10.6

5 years ago

1.10.5

5 years ago

1.10.4

5 years ago

1.10.3

5 years ago

1.10.2

5 years ago

1.10.1

5 years ago

1.10.0

5 years ago

1.9.9

5 years ago

1.9.8

5 years ago

1.9.7

5 years ago

1.9.6

5 years ago

1.9.5

5 years ago

1.9.4

5 years ago

1.9.3

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago