lzo-bjson
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

LZO: Bjson

Data Serializer and Deserializer.

Sponsor Commitizen friendly TypeScript version Node.js version MIT Build Status - GitHub Actions

Installation

npm install lzo-bjson OR yarn add lzo-bjson

Usage

Just

import { Bjson } from 'lzo-bjson';

const bjson = new Bjson({});

const obj = bjson.stringify({ a: 1, b: 2, c: 3 });
const arr = bjson.stringify([1, 2, 3]);
const int = bjson.stringify(2023);
const bol = bjson.stringify(true);
const str = bjson.stringify('John Doe');

console.log(bjson.parse(obj), typeof bjson.parse(obj)); // { a: 1, b: 2, c: 3 } object
console.log(bjson.parse(arr), typeof bjson.parse(arr)); // [1, 2, 3] object
console.log(bjson.parse(int), typeof bjson.parse(int)); // 2023 number
console.log(bjson.parse(bol), typeof bjson.parse(bol)); // true boolean
console.log(bjson.parse(str), typeof bjson.parse(str)); // John Doe string
import { bjson } from 'lzo-bjson';

class Person {
  constructor(private name: string) {}

  public getName() {
    return this.name;
  }

  public setName(name: string) {
    this.name = name;
  }
}

const bjson = new Bjson({
  Person,
});

const person = new Person('John Doe');

const compressed = bjson.stringify(person); // Uint8Array
const decompressed = bjson.parse(compressed); // Person
console.log(decompressed.getName()); // John Doe

// ! Maintains class integrity

decompressed.setName('Jane Doe');
const compressed2 = bjson.stringify(decompressed); // Uint8Array
const decompressed2 = bjson.parse(compressed2); // Person
console.log(decompressed2.getName()); // Jane Doe

Maintaining Inheritance

import { bjson } from 'lzo-bjson';

class Person {
  constructor(protected name: string) {}

  public getName() {
    return this.name;
  }

  public setName(name: string) {
    this.name = name;
  }
}

class Hero extends Person {
  constructor(protected name: string, protected power: string) {
    super(name);
  }

  public getPower() {
    return this.power;
  }
}

const bjson = new Bjson({
  Person,
  Hero,
});

const person = new Hero('John Doe', 'Super Strength');

const compressed = bjson.stringify(person); // Uint8Array
const decompressed = bjson.parse<Hero>(compressed); // Person
console.log(decompressed.getName()); // John Doe
console.log(decompressed.getPower()); // Super Strength

API

Bjson.getMaxDepth(): number

Get the maximum depth of the object

Bjson.makeDeepCopy<T>(object: T): T

Make a deep copy of the object

Bjson.stringify<T>(value: T): Uint8Array

Stringify the object to Uint8Array

Bjson.parse<T>(json: Uint8Array): T

Parse the Uint8Array to object maintaining the prototype chain

Backers & Sponsors

Support this project by becoming a sponsor.

License

Licensed under the MIT. See the LICENSE file for details.

Package Sidebar

Install

npm i lzo-bjson

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

14.1 kB

Total Files

5

Last publish

Collaborators

  • guilhermesantos001