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?

4
  • 1
    nope, you need to create a new object and set only the properties you want to send. Commented Dec 12, 2016 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}); }. Commented Dec 12, 2016 at 15:31
  • Using a custom replacer as suggested in this answer will help, I believe. stackoverflow.com/a/41685627/2685234 Commented Jun 14, 2017 at 7:10
  • 1
    Found one more approach of having toJSON function in each type.. stackoverflow.com/a/35138341/2685234 Commented Jun 14, 2017 at 7:22

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 stackoverflow.com/questions/41685082/…
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;
@LukeKroon I updated this code for a property which is ref of another class having same kind of functionality
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.