A lib for reflecting class to json with decorator in .ts file
.
npm i reflect2json
yarn add reflect2json
{
"compilerOptions": {
"...otherCompilerOptions": "...",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"...otherCompilerOptions": "..."
}
}
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",
};
}
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!