Skip to content

linwrui/reflect2json

Repository files navigation

reflect2json

A lib for reflecting class to json with decorator in .ts file.

Install

npm i reflect2json

yarn add reflect2json

Usage

First enable emitDecoratorMetadata and experimentalDecorators in tsconfig.json

{
  "compilerOptions": {
    "...otherCompilerOptions": "...",

    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,

    "...otherCompilerOptions": "..."
  }
}

Now you can use jsonReflect in any class

import { jsonFormat, jsonIgnore, jsonPick, jsonReflect } from "reflect2json"; 
// use 'reflect2json/lib/src/reflect2json' instead when import failed!

@jsonReflect() // <-- Decorator jsonReflect, it will rewrite this.toJSON for reflcting
export class Example {
  private _name: string = "example"; // <-- it will be ignored automanic when property key starts with '_'
  public get name(): string { // <-- notice: getter property will position to the end of json
    return this._name;
  }
  public set name(value: string) {
    this._name = value;
  }

  @jsonIgnore() // <-- ignore property to json
  public noSerialize = "no!!!";

  @jsonIgnore((value: string) => value.toUpperCase() === "A") // <-- only ignore when value is A or a (only ignore when predicate = true)
  public ignoreA = "A";

  @jsonFormat((value: string) => value.toUpperCase()) // <-- format value to json
  public formatString = "my name is linwrui";

  @jsonPick("a", "b", "c") // <-- pick keys to json
  public pickABC = {
    a: "1",
    b: "2",
    c: "3",
    d: "noPick",
    e: "noPick",
  };
}

Then you just need to get jsonStringfy as usual

const example = new Example();
console.log(JSON.stringify(example)); // <-- '{"formatString":"MY NAME IS LINWRUI","pickABC":{"a":"1","b":"2","c":"3"},"name":"example"}'

Thanks for using!

About

A lib for reflect class to json with decorator.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published