json-ignore

Json ignore is a small library using the experimental decorator feature of typescript to exclude certain properties from the serialisation process. It is usefull if you want to keep your json as small as possible or if you want to serialise a circular str

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
json-ignore
910.4.06 years ago7 years agoMinified + gzip package size for json-ignore in KB

Readme

json-ignore
Json ignore is a small library using the experimental decorator feature of typescript to exclude certain properties from the serialisation process. It is usefull if you want to keep your json as small as possible or if you want to serialise a circular structure.

Installation

npm install json-ignore --save

Usage

Since json-ignore is using a experimental feature of typescript you would want to add these two lines to your tsconfig.json to prevent warnings
{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

Afterwards use the @jsonIgnore() decorator to exlude properties form beeing serialised.

Example

import { jsonIgnoreReplacer, jsonIgnore } from 'json-ignore';

class Parent {
    @jsonIgnore() public arrayIgnored: string[] = ['1', '2', '3'];

    public array: string[] = ['1', '2', '3'];

    public child: Child = new Child(this);
}

class Child {
    @jsonIgnore() protected parent: Parent;
    constructor(parent: Parent) {
        this.parent = parent;
    }

    public childPropert: string = '123';
}

console.log(JSON.stringify(new Parent(), jsonIgnoreReplacer));

Output

{ "array": ["1", "2", "3"], "child": { "childPropert": "123" } }

Advanced

If you want to replace a properties value by a constat value, you can do so by using @jsonReplaceByConstant().

Example

@jsonReplaceByConstant("id-2")
public child:Child=new Child();

Output

{ "child": "id-2" }

You can also change the value of a property to the value of a direct child, by using @jsonReplaceByChildValue(). This is usefull e.g. if you only want to save the id of the child.

Example

class Parent {
    @jsonReplaceByChildValue('id') public child: Child = new Child();
}

class Child {
    public id: string = 'id-1';
}

Output

{ "child": "id-1" }