I am creating a game in a web application that uses custom circular objects that the user can manipulate and save. All objects are connected to a circular player object, which is stored using Circular-JSON .
An application requires storing object types, but JSON does not. I canβt declare a type for each individual object, because a player can have a couple of hundred objects of different types. I saw some regenerators that are of a type, but not a generic one, or one that will work with circular objects.
For instance:
function Room(name, description){
this.type = "room";
this.name = name;
this.description = description;
this.roomItems = [];
this.exits = [];
}
function Item(name, description, weight){
this.type = "item";
this.name = name;
this.description = description;
this.weight = weight;
this.components = [];
this.contents = [];
this.setContents = function(item){
this.contents.push(item);
this.weight += item.weight;
}
}
function Player(startroom){
this.type = "player"
this.weightLimit = 20;
this.totalWeight = 0;
this.currentRoom = startroom;
this.playerItems = [];
this.moveCount = 0;
}
The application saves and downloads through:
function saveGame(){
var d = new Date()
player.lastsave = d.toISOString();
localStorage.setItem('player', CircularJSON.stringify(player));
}
function loadGame(){
declareStart();
player = CircularJSON.parse(localStorage.getItem('player', reviver));
}
The examiner I tried looks like this:
function reviver(k, v){
switch (v.type) {
case "room":
$.extend(v, Room.prototype);
break;
case "item":
$.extend(v, Item.prototype);
break;
case "player":
$.extend(v, Player.prototype);
break;
}
return v;
}
, , .
reviver, ? ?
, , ?