How come JSON can't save object's functions?

前端 未结 2 888
走了就别回头了
走了就别回头了 2021-01-05 20:02

In my game, I save the current state by converting all the objects to JSON and then saving that to a file. Some objects, like enemies, have functions on them, but JSON can\'

相关标签:
2条回答
  • 2021-01-05 20:34

    I think that you have to save the type on your object so that the functions can be re-added at parsing time. E.g. put a type property on your enemy in the constructor. At parsing time, first parse the string like normal JSON and then deep-traverse the resulting object. When you encounter something that was an Enemy, re-attach the methods or so.

    0 讨论(0)
  • 2021-01-05 20:41
    var Enemy = {
      toJSON: function () {
        // pack it up
      },
      fromJSON: function (json) {
        // unpack it.
      },
      /* methods */
    };
    
    var e = Object.create(Enemy);
    var json = JSON.stringify(e);
    var same_e = Enemy.fromJSON(json);
    

    the .toJSON method is a standard interface of JSON.stringify it will look this method and call it if it exists, it will stringify the returned object.

    The .fromJSON method is just a named constructor for your Enemy object.

    Concrete example JSfiddle

    var Enemy = {
      constructor: function(name, health) {
        this.health = health || 100;
        this.name = name;
      },
      shootThing: function (thing) { },
      move: function (x,y) { },
      hideBehindCover: function () {},
      toJSON: function () { 
        return {
          name: this.name,
          health: this.health
        };
      },
      fromJSON: function (json) {
        var data = JSON.parse(json);
        var e = Object.create(Enemy);
        e.health = data.health;
        e.name = data.name;
        return e;
      }
    }
    
    var e = Object.create(Enemy);
    e.constructor("bob");
    var json = JSON.stringify(e);
    var e2 = Enemy.fromJSON(json);
    console.log(e.name === e2.name);
    

    Meta-option:

    A meta option would be to write the class name to the object

    Game.Enemy = {
      ...
      class: "Enemy"
    };
    

    Then when you load all your json data you just do

    var instance = Game[json.class].fromJSON(json);

    0 讨论(0)
提交回复
热议问题