Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSON.stringify in conjunction with TypeScript getter/setter

I am using getter/setter accessors in TypeScript. As it is not possible to have the same name for a variable and method, I started to prefix the variable with a lower dash, as is done in many examples:

private _major: number;

get major(): number {
  return this._major;
}
set major(major: number) {
  this._major = major;
}

Now when I use the JSON.stringify() method to convert the object into a JSON string, it will use the variable name as the key: _major.

As I don't want the JSON file to have all keys prefixed with a lower dash, is there any possibility to make TypeScript use the name of the getter method, if available? Or are there any other ways to use the getter/setter methods but still produce a clean JSON output?

I know that there are ways to manually modify the JSON keys before they are written to the string output. I am curious if there is simpler solution though.

Here is a JSFiddle which demonstrates the current behaviour.

like image 331
Dominic Avatar asked Oct 17 '16 07:10

Dominic


People also ask

Can we use getter and setter in TypeScript?

In TypeScript, there are two supported methods getter and setter to access and set the class members.

How do I Stringify a JSON object in TypeScript?

Use the JSON. stringify() Method to Convert an Object Into a JSON String in TypeScript. In TypeScript, we will use the JSON. stringify() method to turn any object into a JSON string.

Does JSON Stringify call toJSON?

toJSON() behaviorJSON.stringify() calls toJSON with one parameter: if this object is a property value, the property name. if it is in an array, the index in the array, as a string.

Can you use JSON Stringify on an array?

The JSON array data type cannot have named keys on an array. When you pass a JavaScript array to JSON. stringify the named properties will be ignored. If you want named properties, use an Object, not an Array.


2 Answers

based on @Jan-Aagaard solution I have tested this one

public toJSON(): string {
    let obj = Object.assign(this);
    let keys = Object.keys(this.constructor.prototype);
    obj.toJSON = undefined;
    return JSON.stringify(obj, keys);
}

in order to use the toJSON method

like image 104
alacambra Avatar answered Sep 21 '22 02:09

alacambra


No, you can't have JSON.stringify using the getter/setter name instead of the property name.

But you can do something like this:

class Version {
    private _major: number;

    get major(): number {
        return this._major;
    }

    set major(major: number) {
        this._major = major;
    }

    toJsonString(): string {
        let json = JSON.stringify(this);
        Object.keys(this).filter(key => key[0] === "_").forEach(key => {
            json = json.replace(key, key.substring(1));
        });

        return json;
    }
}

let version = new Version();
version.major = 2;
console.log(version.toJsonString()); // {"major":2}
like image 41
Nitzan Tomer Avatar answered Sep 20 '22 02:09

Nitzan Tomer