Skip to content

Commit

Permalink
Merge pull request #66 from rpgtkoolmv/fix-save-array
Browse files Browse the repository at this point in the history
fix circular referenced array
  • Loading branch information
liply committed Mar 13, 2017
2 parents 27262f0 + 4b33502 commit 2a2aab2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
29 changes: 22 additions & 7 deletions js/rpg_core/JsonEx.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ JsonEx._cleanMetadata = function(object){

delete object['@'];
delete object['@c'];
delete object['@m'];

if(typeof object === 'object'){
Object.keys(object).forEach(function(key){
Expand Down Expand Up @@ -125,21 +124,30 @@ JsonEx._encode = function(value, circular, depth) {
}
var type = Object.prototype.toString.call(value);
if (type === '[object Object]' || type === '[object Array]') {
value['@m'] = true;
value['@c'] = JsonEx._generateId();

var constructorName = this._getConstructorName(value);
if (constructorName !== 'Object' && constructorName !== 'Array') {
value['@'] = constructorName;
}
for (var key in value) {
if (value.hasOwnProperty(key)) {
if (value.hasOwnProperty(key) && !key.match(/^@./)) {
if(value[key] && typeof value[key] === 'object'){
if(!value[key]['@m']){
value[key] = this._encode(value[key], circular, depth + 1);
delete value[key]['@m'];
}else{
if(value[key]['@c']){
circular.push([key, value, value[key]]);
value[key] = {'@r': value[key]['@c']};
}else{
value[key] = this._encode(value[key], circular, depth + 1);

if(value[key] instanceof Array){
//wrap array
circular.push([key, value, value[key]]);

value[key] = {
'@c': value[key]['@c'],
'@a': value[key]
};
}
}
}else{
value[key] = this._encode(value[key], circular, depth + 1);
Expand Down Expand Up @@ -173,7 +181,14 @@ JsonEx._decode = function(value, circular, registry) {
}
for (var key in value) {
if (value.hasOwnProperty(key)) {
if(value[key] && value[key]['@a']){
//object is array wrapper
var body = value[key]['@a'];
body['@c'] = value[key]['@c'];
value[key] = body;
}
if(value[key] && value[key]['@r']){
//object is reference
circular.push([key, value, value[key]['@r']])
}
value[key] = this._decode(value[key], circular, registry);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"copy-all": "node copy-all.js ./corescript",
"zip": "bestzip corescript.zip ./corescript/",
"test": "run-s build copy",
"unittest": "node ./tests/test-save.js",
"start": "http-server ./game/",
"package": "run-s build copy-all zip"
},
Expand Down
23 changes: 23 additions & 0 deletions tests/test-save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let fs = require('fs');
let assert = require('assert');

eval(fs.readFileSync('./js/rpg_core/JsonEx.js').toString());

(function duplicatedArray(){
let array = [1, 2, 3];
let obj = {a1: array, a2: array};
array.hoge = "fuga";
let result = JsonEx.parse(JsonEx.stringify(obj));
assert(result.a1 === result.a2);
assert(obj.a1 === obj.a2);
})();

(function circularLink(){
let a = {};
let b = {a};
a.b = b;
let obj = {a1: a, a2: a};
let result = JsonEx.parse(JsonEx.stringify(obj));
assert(result.a1 === result.a2);
assert(obj.a1 === obj.a2);
})();

0 comments on commit 2a2aab2

Please sign in to comment.