đź“– Guide Documents
Enhanced JSON
stringify() functions

stringify() functions

export namespace json {
  export function stringify<T>(input: T): string;
  export function isStringify<T>(input: T | unknown): string | null;
  export function assertStringify<T>(input: T | unknown): string;
  export function validateStringify<T>(input: T | unknown): IValidation<string>;
}

You can boost up JSON serialization speed just by calling typia.json.stringify<T>() function. Also, you even can ensure type safety of JSON serialization by calling other functions like typia.json.isStringify() and typia.json.assertStringify() functions.

As typia.json.stringify<T>() function writes dedicated JSON serialization code only for the target type T, its performance is much faster than native JSON.stringify() function. However, because of the dedicated optimal JSON serialization code, when wrong typed data comes, unexpected error be occured.

Instead, typia supports type safe JSON serialization functions like typia.json.isStringify(). The typia.json.isStringify() is a combination function of typia.is<T>() and typia.json.stringify<T>() function. It checks whether the input value is valid for the target type T or not first, and operate JSON serialization later. If the input value is not matched with the type T, it returns null value.


examples/src/isStringify.ts
import typia, { tags } from "typia";
 
const department: IDepartment = typia.random<IDepartment>();
const json: string | null = typia.json.isStringify(department);
 
console.log(json); // not null, but string
 
interface IDepartment {
    id: string & tags.Format<"uuid">;
    name: string & tags.MinLength<3>;
    limit: number & tags.Type<"int32">;
    clerks: IClerk[];
}
interface IClerk {
    name: string;
    age: number 
        & tags.Type<"uint32"> 
        & tags.ExclusiveMinimum<19> 
        & tags.Maximum<100>;
    authority: number;
    joined_at: string & tags.Format<"date">;
}

Reusable functions

export namespace json {
  export function createStringify<T>: (input: T) => string;
  export function createIsStringify<T>: (
      input: T | unknown
  ) => string | null;
  export function createAssertStringify<T>: (
      input: T | unknown
  ) => string;
  export function createValidateStringify<T>: (
      input: T | unknown
  ) => IValidation<string>;
}

Reusable typia.json.stringify<T>() function generators.

If you repeat to call typia.json.stringify<T>() function on the same type, size of JavaScript files would be larger because of duplicated AOT compilation. To prevent it, you can generate reusable function through typia.json.createStringify<T>() function.

Just look at the code below, then you may understand how to use it.

examples/src/createAssertStringify.ts
import typia, { tags } from "typia";
 
export const assertDepartment = typia.json.createAssertStringify<IDepartment>();
 
interface IDepartment {
  id: string & tags.Format<"uuid">;
  name: string & tags.MinLength<3>;
  limit: number & tags.Type<"int32">;
  clerks: IClerk[];
}
interface IClerk {
  name: string;
  age: number &
    tags.Type<"uint32"> &
    tags.ExclusiveMinimum<19> &
    tags.Maximum<100>;
  authority: number;
  joined_at: string & tags.Format<"date">;
}

Performance

Comparing JSON serialization speed with others, it is maximum 200x faster than class-transformer.

For reference, class-transformer is the most famous library used in NestJS with class-validator. Also, fast-json-stringify is another famous one used in fastify. However, whether they are fast or slow, both of them require extra schema definition, that is different with TypeScript type. If you see the code below without experience of them, you may get shocked: how complicate and inefficient they are:

Stringify Function Benchmark

Measured on Intel i5-1135g7, Surface Pro 8 (opens in a new tab)

Server Performance

Someone may ask:

JSON serialization speed affects on the server performance?

I think that the JSON serialization is just a tiny thing in the server side, isn't it?

My answer is, "Yes, it affects on the server performance".

Most operations in NodeJS server are asynchronously executed in background thread, what are called "event based non-blocking I/O model". However, JSON serialization is a synchronous operation running on the main thread. Therefore, if the JSON serialization speed is slow, it makes the entire server program slow.

I'll show you the benchmark result that, how JSON serizliation speed affects on the server performance.

Server Benchmark

Measured on Intel i5-1135g7, Surface Pro 8 (opens in a new tab)