# BJSON

### Imports

{% tabs %}
{% tab title="Service" %}

```typescript
import { BjsonService } from 'niro-health';
```

{% endtab %}

{% tab title="Module" %}

```typescript
import { BjsonModule } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { IBjsonService } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Method of Use

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

```typescript
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.

{% code title="app.service.ts" overflow="wrap" %}

```typescript
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);
  }
}
```

{% endcode %}

### Method of Use with classes

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

{% hint style="info" %}
If you are going to perform encoding of an object with inheritance, you should follow this method of use.
{% endhint %}

{% code title="app.module.ts" %}

```typescript
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 {}
```

{% endcode %}

{% code title="app.service.ts" %}

```typescript
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);
  }
}
```

{% endcode %}

{% code title="app.controller.ts" %}

```typescript
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();
  }
}
```

{% endcode %}

#### Test results

<figure><img src="https://717359273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxaCLvdAgAIA124Mfl6Io%2Fuploads%2Fns4Vr3oGtXolHw2XvS4a%2Fniro_result.png?alt=media&#x26;token=8943db68-1a83-4fde-84d3-401f0260622a" alt=""><figcaption></figcaption></figure>

### Without dependencies

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

### Properties

<table><thead><tr><th width="266.3333333333333">Property</th><th width="133" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>CONSTRUCTORS</code></td><td align="center"><code>static</code></td><td><code>The object that contains the constructors.</code></td></tr><tr><td><code>MAXDEPTH</code></td><td align="center"><code>static</code></td><td><code>The maximum depth of the object.</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="266.3333333333333">Method</th><th width="132" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>_encode</code></td><td align="center"><code>private</code></td><td><code>Encode an object to string.</code></td></tr><tr><td><code>_decode</code></td><td align="center"><code>private</code></td><td><code>Decode an object from string.</code></td></tr><tr><td><code>addConstructors</code></td><td align="center"><code>public</code></td><td><code>It adds the constructors to the _constructors object.</code></td></tr><tr><td><code>removeConstructors</code></td><td align="center"><code>public</code></td><td><code>It removes the constructors from the _constructors object.</code></td></tr><tr><td><code>setMaxDepth</code></td><td align="center"><code>public</code></td><td><code>Sets the maximum depth of the object.</code></td></tr><tr><td><code>getMaxDepth</code></td><td align="center"><code>public</code></td><td><code>Gets the maximum depth of the object.</code></td></tr><tr><td><code>makeDeepCopy</code></td><td align="center"><code>public</code></td><td><code>It takes an object of type custom, makes a deep copy of it, and returns it.</code></td></tr><tr><td><code>stringify</code></td><td align="center"><code>public</code></td><td><code>It takes an object of type custom, encodes it, stringifies it, and returns it.</code></td></tr><tr><td><code>parse</code></td><td align="center"><code>public</code></td><td><code>It takes a string, parses it, decodes it, and returns it.</code></td></tr></tbody></table>
