Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ES6 Class with Symbols to JSON

I have hardcoded classes to represent models in my Aurelia application. Here's a model 'PostEdit':

var _postID = Symbol();
var _title = Symbol();
var _text = Symbol();

export class PostEdit {

    constructor(postEdit) {
        this[_postID] = postEdit.postID;
        this.title = postEdit.title;
        this.text= postEdit.text;
    }

    get postID() { return this[_postID]; }

    get title() { return this[_title]; }
    set title(val) { this[_title] = val; }

    get text() { return this[_text]; }
    set text(val) { this[_text] = val; }

}

After the object is manipulated, I need to PUT and POST it back to the server. But it looks like Aurelia's HttpClient is sending an empty JSON string ({}). Looking into it, it seems that Symbols are ignored when converting an ES6 class to JSON.

How can I go about getting all my properties into a JSON string to submit back to the server?

like image 795
Jonesopolis Avatar asked Oct 28 '15 04:10

Jonesopolis


3 Answers

I'm assuming you're using symbols to keep the data private, but this means you're going to have to go through some extra steps if you want that data included in the JSON representation.

Here's an example using toJSON on your model to explicitly export the properties you care about

export class PostEdit {

  // ...
  toJSON() {
    return {
      postID: this.postID,
      title:  this.title,
      text:   this.text
    };
  }
}

Or

export class PostEdit {

  // ...
  toJSON() {
    let {postID, title, text} = this;
    return {postID, title, text};
  }
}

When JSON.stringify is called on your instance, it will automatically call toJSON

like image 169
Mulan Avatar answered Nov 01 '22 11:11

Mulan


for more dynamic solution use this:

export class MeMe(){
 toJSON() {
    return Object.getOwnPropertyNames(this).reduce((a, b) => {
      a[b] = this[b];
      return a;
    }, {});
  }
}

or you can use my json-decorator :)

import json from "json-decorator";  

@json("postID") // pass the property names that you want to ignore
export class MeMe(){
  // ...
}
like image 17
Fareed Alnamrouti Avatar answered Nov 01 '22 11:11

Fareed Alnamrouti


Give your class a toJSON method that returns a stringifyable object:

export class PostEdit {

    constructor(postEdit) {
        this[_postID] = postEdit.postID;
        this.title = postEdit.title;
        this.text = postEdit.text;
    }

    get postID() { return this[_postID]; }

    get title() { return this[_title]; }
    set title(val) { this[_title] = val; }

    get text() { return this[_text]; }
    set text(val) { this[_text] = val; }

    toJSON() {
        return {
            postId: this.postId,
            title: this.title,
            text: this.text
        };
    }
}

JSON.stringify will automatically call that and replace your instance with the result.

Also you might want to add a fromJSON method to your class that you can use to revive instances during JSON.parse. It is trivial in your case:

    static fromJSON(obj) {
        return new this(obj);
    }

but you might need something more complicated in other classes.

like image 7
Bergi Avatar answered Nov 01 '22 13:11

Bergi