https://github.com/victorqueiroz/binobject
Encode JavaScript objects in binary format with support for native types
https://github.com/victorqueiroz/binobject
javascript json nodejs
Last synced: 10 months ago
JSON representation
Encode JavaScript objects in binary format with support for native types
- Host: GitHub
- URL: https://github.com/victorqueiroz/binobject
- Owner: VictorQueiroz
- License: mit
- Created: 2018-04-18T03:50:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-27T20:37:52.000Z (over 2 years ago)
- Last Synced: 2025-08-11T10:45:50.079Z (11 months ago)
- Topics: javascript, json, nodejs
- Language: TypeScript
- Size: 469 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# binobject
Easy way to encode / decode JavaScript objects in binary format with support for Date, ArrayBuffer and Node.js Buffer.
## Installation
```
yarn add binobject
```
## Usage
```js
import { ObjectEncoder, ObjectDecoder } from 'binobject';
import { randomBytes } from 'crypto';
const sourceObject = {
users: [{
name: 'Astrid',
birthday: new Date(),
uniqueSignature: randomBytes(256)
}]
};
const buffer = new ObjectEncoder().encode(sourceObject);
assert.deepEqual(new ObjectDecoder(buffer).decode(), sourceObject);
```
## Custom types
Both encoder and decoder class give you the possibility to define custom types for encoding and decoding. All you have to do is create a processor to decode and encode your custom type and then define a value. When defining a custom type it's important not to override a default type, you can check a full list starting from [here](https://github.com/VictorQueiroz/binobject/blob/master/src/constants.ts#L1). Also you need to be aware that currently this library support only up to 255 types (custom types included). See an example bellow:
```ts
import { BinaryObject, Processor } from 'binobject';
import { ObjectID } from 'mongodb';
import { deepEqual } from 'assert';
class ProcessorObjectID extends Processor {
validate(id: any): boolean {
return id instanceof ObjectID == true;
}
decode(buffer: Buffer): ObjectID {
return new ObjectID(buffer.toString('hex'));
}
encode(input: ObjectID): Buffer {
return Buffer.from(input.toHexString(), 'hex');
}
}
const binaryObject = new BinaryObject([{
processor: new ProcessorObjectID,
value: 60
}]);
const buffer = binaryObject.encode({
_id: new ObjectID()
});
deepEqual(binaryObject.decode(buffer), {
_id: new ObjectID()
});
```
## More examples
Need more examples? Check out our [test.ts](https://github.com/VictorQueiroz/binobject/blob/master/test/test.ts) file