10

I have this class:

export class TblColabAdmin {
    snomatrcompl: string;
    nflativo: number;
    ativo: boolean;
}

The attribute ativo doesn't exist in my web service entity, so I would like to avoid that it was added in JSON.

In Java, for example, we have @JsonIgnore annotation. Does exist something similar in TypeScript?

Marcel
  • 2,810
  • 2
  • 26
  • 46
  • 1
    nope, you need to create a new object and set only the properties you want to send. – toskv Dec 12 '16 at 13:40
  • Did you look at adding a 'toJSON' method? You could tr something like `toJSON() { const obj = Object.assign({}, this); delete obj.ativo; return obj; }`. Or `toJSON() { return Object.assign({}, this, {ativo: undefined}); }`. –  Dec 12 '16 at 15:31
  • Using a custom replacer as suggested in this answer will help, I believe. https://stackoverflow.com/a/41685627/2685234 – ramtech Jun 14 '17 at 07:10
  • 1
    Found one more approach of having toJSON function in each type.. https://stackoverflow.com/a/35138341/2685234 – ramtech Jun 14 '17 at 07:22

1 Answers1

11

You can create a JsonIgnore decorator so that it will work like with java:

const IGNORE_FIELDS = new Map<string, string[]>();
function JsonIgnore(cls: any, name: string) {
    let clsName = cls.constructor.name;
    let list: string[];

    if (IGNORE_FIELDS.has(clsName)) {
        list = IGNORE_FIELDS.get(clsName);
    } else {
        list = [];
        IGNORE_FIELDS.set(clsName, list);
    }

    list.push(name);
}

class Base {
    toJson(): { [name: string]: any } {
        let json = {};
        let ignore = IGNORE_FIELDS.get(this.constructor.name);

        Object.getOwnPropertyNames(this).filter(name => ignore.indexOf(name) < 0).forEach(name => {
            json[name] = this[name];
        });

        return json;
    }
}

class TblColabAdmin extends Base {
    snomatrcompl: string;
    nflativo: number;

    @JsonIgnore
    ativo: boolean;

    constructor(snomatrcompl: string, nflativo: number, ativo: boolean) {
        super();

        this.snomatrcompl = snomatrcompl;
        this.nflativo = nflativo;
        this.ativo = ativo;
    }
}

let obj = new TblColabAdmin("str", 43, true).toJson();
console.log(obj); // Object {snomatrcompl: "few", nflativo: 43}

(code in playground)

It's quite a lot of work if you're only doing it once, but if it's a common issue in your code then this approach should work well.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • 1
    This works well if you are not using the JSON.stringify, in other case a good approach is using a replacer as suggested in this post https://stackoverflow.com/questions/41685082/how-to-ignore-properties-sent-via-http/41685627#41685627 – Gabriel Cerutti Nov 14 '17 at 13:46
  • I use this in my project and extend all my classes from the Base class. Just make sure to check `let ignore` before you filter because if you do not have a `@JsonIgnore` in your class it will be undefined and `json` will be `{}`. So `if(!ignore) json = this;` – Luke Kroon May 14 '20 at 12:02
  • @LukeKroon I updated this code for a property which is ref of another class having same kind of functionality – Gourav Garg Jun 15 '20 at 09:51
  • @GouravGarg your edit changes my reply, which is old and i prefer to keep it as is. you're welcome to write a new answer with your improved code. – Nitzan Tomer Jun 15 '20 at 16:34