Skip to content

Commit 2a2aab2

Browse files
author
liply
authored
Merge pull request #66 from rpgtkoolmv/fix-save-array
fix circular referenced array
2 parents 27262f0 + 4b33502 commit 2a2aab2

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

js/rpg_core/JsonEx.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ JsonEx._cleanMetadata = function(object){
8484

8585
delete object['@'];
8686
delete object['@c'];
87-
delete object['@m'];
8887

8988
if(typeof object === 'object'){
9089
Object.keys(object).forEach(function(key){
@@ -125,21 +124,30 @@ JsonEx._encode = function(value, circular, depth) {
125124
}
126125
var type = Object.prototype.toString.call(value);
127126
if (type === '[object Object]' || type === '[object Array]') {
128-
value['@m'] = true;
129127
value['@c'] = JsonEx._generateId();
128+
130129
var constructorName = this._getConstructorName(value);
131130
if (constructorName !== 'Object' && constructorName !== 'Array') {
132131
value['@'] = constructorName;
133132
}
134133
for (var key in value) {
135-
if (value.hasOwnProperty(key)) {
134+
if (value.hasOwnProperty(key) && !key.match(/^@./)) {
136135
if(value[key] && typeof value[key] === 'object'){
137-
if(!value[key]['@m']){
138-
value[key] = this._encode(value[key], circular, depth + 1);
139-
delete value[key]['@m'];
140-
}else{
136+
if(value[key]['@c']){
141137
circular.push([key, value, value[key]]);
142138
value[key] = {'@r': value[key]['@c']};
139+
}else{
140+
value[key] = this._encode(value[key], circular, depth + 1);
141+
142+
if(value[key] instanceof Array){
143+
//wrap array
144+
circular.push([key, value, value[key]]);
145+
146+
value[key] = {
147+
'@c': value[key]['@c'],
148+
'@a': value[key]
149+
};
150+
}
143151
}
144152
}else{
145153
value[key] = this._encode(value[key], circular, depth + 1);
@@ -173,7 +181,14 @@ JsonEx._decode = function(value, circular, registry) {
173181
}
174182
for (var key in value) {
175183
if (value.hasOwnProperty(key)) {
184+
if(value[key] && value[key]['@a']){
185+
//object is array wrapper
186+
var body = value[key]['@a'];
187+
body['@c'] = value[key]['@c'];
188+
value[key] = body;
189+
}
176190
if(value[key] && value[key]['@r']){
191+
//object is reference
177192
circular.push([key, value, value[key]['@r']])
178193
}
179194
value[key] = this._decode(value[key], circular, registry);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"copy-all": "node copy-all.js ./corescript",
2222
"zip": "bestzip corescript.zip ./corescript/",
2323
"test": "run-s build copy",
24+
"unittest": "node ./tests/test-save.js",
2425
"start": "http-server ./game/",
2526
"package": "run-s build copy-all zip"
2627
},

tests/test-save.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let fs = require('fs');
2+
let assert = require('assert');
3+
4+
eval(fs.readFileSync('./js/rpg_core/JsonEx.js').toString());
5+
6+
(function duplicatedArray(){
7+
let array = [1, 2, 3];
8+
let obj = {a1: array, a2: array};
9+
array.hoge = "fuga";
10+
let result = JsonEx.parse(JsonEx.stringify(obj));
11+
assert(result.a1 === result.a2);
12+
assert(obj.a1 === obj.a2);
13+
})();
14+
15+
(function circularLink(){
16+
let a = {};
17+
let b = {a};
18+
a.b = b;
19+
let obj = {a1: a, a2: a};
20+
let result = JsonEx.parse(JsonEx.stringify(obj));
21+
assert(result.a1 === result.a2);
22+
assert(obj.a1 === obj.a2);
23+
})();

0 commit comments

Comments
 (0)