`JsonService` is a custom json service that can be used in a NestJS to stringify or parse a json data
npm install abs-nestjs-json-serviceJsonService is a custom json service that can be used in a NestJS to stringify or parse a json data
To install the necessary dependencies, run:
``bash`
npm install abs-nestjs-json-service
1. Import the JsonService into your module:
`typescript
import { Module } from '@nestjs/common';
import { JsonService } from 'abs-nestjs-json-service';
@Module({
providers: [JsonService],
exports: [JsonService],
})
export class AppModule {}
`
2. Inject the JsonService into your service or controller:
`typescript
import { Injectable } from '@nestjs/common';
import { JsonService } from 'abs-nestjs-json-service';
@Injectable()
export class SomeService {
constructor(private readonly json: JsonService) {}
someMethod() {
const data = { message: 'Hello world' };
const jsonRawData = this.json.stringify(data, {
default: 'This is a default value if data is null or empty',
});
const jsonData = this.json.safeParse(jsonRawData);
}
}
``