Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore an entity field in TypeScript when sending a JSON to a Web Service?

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?

like image 483
Marcel Avatar asked Dec 12 '16 13:12

Marcel


People also ask

How do you ignore a field in JSON response?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

How do I ignore properties in JSON?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How should I parse a JSON string in TypeScript?

TS has a JavaScript runtime Typescript has a JavaScript runtime because it gets compiled to JS. This means JS objects which are built in as part of the language such as JSON , Object , and Math are also available in TS. Therefore we can just use the JSON. parse method to parse the JSON string.

How do I cast a JSON object to a TypeScript interface?

1) Add a constructor in your Typescript class that takes the json data as parameter. In that constructor you extend your json object with jQuery, like this: $. extend( this, jsonData) .


1 Answers

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.

like image 60
Nitzan Tomer Avatar answered Oct 03 '22 23:10

Nitzan Tomer