Lakshya Sharma
Lakshya Sharma

Reputation: 85

JSON.stringify not working for all properties of object in Typescript

JSON.stringify not working for all properties of the outermost object in Typescript. Please refer to code snippet below:

class Criterion {
    '@CLASS' = 'xyz.abc.Criterion'
    operator: string;
    operand: string[];
    field: string;
    constructor(field:string,operator:string,operand: string[]) {
      this.field = field;
      this.operator = operator;
      this.operand = operand;
    }
}

class Filter {
    '@CLASS': 'xyz.ada.Filter';
    criteria: Criterion[];
    constructor(criteria:Criterion[]) {
        this.criteria=criteria;
    }
}

var criterion = new Criterion("a","b",["c"]);
var a = new Filter([criterion]);
JSON.stringify(a);
console.log(JSON.stringify(a));

Output:

{"criteria":[{"@CLASS":"xyz.abc.Criterion","field":"a","operator":"b","operand":["c"]}]}

Expected:

{"@CLASS":"xyz.ada.Filter","criteria":[{"@CLASS":"xyz.abc.Criterion","field":"a","operator":"b","operand":["c"]}]}

It is not stringifying the @CLASS property of the Filter class object.

code link

Upvotes: 0

Views: 182

Answers (1)

Luís Silva
Luís Silva

Reputation: 146

There is a typo in your class filter. Replace the two dots with:

'@CLASS'= 'xyz.ada.Filter';

Upvotes: 1

Related Questions