📝BJSON

The module that provides the service to encode and decode objects.

Imports

import { BjsonService } from 'niro-health';

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import { BjsonService } from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IBjsonService',
      useClass: BjsonService,
    },
  ],
})
export class AppModule {}

Importing the service inside the module.

app.service.ts
import { Inject, Injectable } from '@nestjs/common';
import type { IBjsonService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IBjsonService')
    private readonly bjsonService: IBjsonService,
  ) {}

  stringify(obj: { name: string }) {
    return this.bjsonService.stringify(obj);
  }

  parse(json: Uint8Array) {
    return this.bjsonService.parse(json);
  }
}

Method of Use with classes

You should add the class constructor to the service before using the methods.

If you are going to perform encoding of an object with inheritance, you should follow this method of use.

app.module.ts
import { Module } from '@nestjs/common';
import { BjsonService } from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IBjsonService',
      useClass: BjsonService,
    },
  ],
})
export class AppModule {}
app.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { BjsonService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IBjsonService')
    private readonly bjsonService: BjsonService,
  ) {}

  addConstructors(constructor: any) {
    return this.bjsonService.addConstructors(constructor);
  }

  stringify(obj: { name: string }) {
    return this.bjsonService.stringify(obj);
  }

  parse<T>(json: Uint8Array) {
    return this.bjsonService.parse<T>(json);
  }
}
app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

class Person {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  sayHello() {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
}

class Hero extends Person {
  power: string;

  constructor(name: string, age: number, power: string) {
    super(name, age);
    this.power = power;
  }

  resume() {
    return `${super.sayHello()} I have the power of ${this.power}.`;
  }
}

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  async running() {
    this.appService.addConstructors(Hero);

    const hero = new Hero('Niro', 21, 'Programmer');
    const test = this.appService.stringify(hero);
    const result = this.appService.parse<Hero>(test);

    return result.resume();
  }
}

Test results

Without dependencies

You won't need to connect the pieces with any other module.

Properties

Property
Scope
Description

CONSTRUCTORS

static

The object that contains the constructors.

MAXDEPTH

static

The maximum depth of the object.

Methods

Method
Scope
Description

_encode

private

Encode an object to string.

_decode

private

Decode an object from string.

addConstructors

public

It adds the constructors to the _constructors object.

removeConstructors

public

It removes the constructors from the _constructors object.

setMaxDepth

public

Sets the maximum depth of the object.

getMaxDepth

public

Gets the maximum depth of the object.

makeDeepCopy

public

It takes an object of type custom, makes a deep copy of it, and returns it.

stringify

public

It takes an object of type custom, encodes it, stringifies it, and returns it.

parse

public

It takes a string, parses it, decodes it, and returns it.

Last updated