-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonEx.js
245 lines (222 loc) · 6.42 KB
/
JsonEx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//-----------------------------------------------------------------------------
/**
* The static class that handles JSON with object information.
*
* @class JsonEx
*/
function JsonEx() {
throw new Error('This is a static class');
}
/**
* The maximum depth of objects.
*
* @static
* @property maxDepth
* @type Number
* @default 100
*/
JsonEx.maxDepth = 100;
JsonEx._id = 1;
JsonEx._generateId = function(){
return JsonEx._id++;
};
/**
* Converts an object to a JSON string with object information.
*
* @static
* @method stringify
* @param {Object} object The object to be converted
* @return {String} The JSON string
*/
JsonEx.stringify = function(object) {
var circular = [];
JsonEx._id = 1;
var json = JSON.stringify(this._encode(object, circular, 0));
this._cleanMetadata(object);
this._restoreCircularReference(circular);
return json;
};
JsonEx._restoreCircularReference = function(circulars){
circulars.forEach(function(circular){
var key = circular[0];
var value = circular[1];
var content = circular[2];
value[key] = content;
});
};
/**
* Parses a JSON string and reconstructs the corresponding object.
*
* @static
* @method parse
* @param {String} json The JSON string
* @return {Object} The reconstructed object
*/
JsonEx.parse = function(json) {
var circular = [];
var registry = {};
var contents = this._decode(JSON.parse(json), circular, registry);
this._cleanMetadata(contents);
this._linkCircularReference(contents, circular, registry);
return contents;
};
JsonEx._linkCircularReference = function(contents, circulars, registry){
circulars.forEach(function(circular){
var key = circular[0];
var value = circular[1];
var id = circular[2];
value[key] = registry[id];
});
};
JsonEx._cleanMetadata = function(object){
if(!object) return;
delete object['@'];
delete object['@c'];
if(typeof object === 'object'){
Object.keys(object).forEach(function(key){
var value = object[key];
if(typeof value === 'object'){
JsonEx._cleanMetadata(value);
}
});
}
};
/**
* Makes a deep copy of the specified object.
*
* @static
* @method makeDeepCopy
* @param {Object} object The object to be copied
* @return {Object} The copied object
*/
JsonEx.makeDeepCopy = function(object) {
return this.parse(this.stringify(object));
};
/**
* @static
* @method _encode
* @param {Object} value
* @param {Array} circular
* @param {Number} depth
* @return {Object}
* @private
*/
JsonEx._encode = function(value, circular, depth) {
depth = depth || 0;
if (++depth >= this.maxDepth) {
throw new Error('Object too deep');
}
var type = Object.prototype.toString.call(value);
if (type === '[object Object]' || type === '[object Array]') {
value['@c'] = JsonEx._generateId();
var constructorName = this._getConstructorName(value);
if (constructorName !== 'Object' && constructorName !== 'Array') {
value['@'] = constructorName;
}
for (var key in value) {
if ((!value.hasOwnProperty || value.hasOwnProperty(key)) && !key.match(/^@./)) {
if(value[key] && typeof value[key] === 'object'){
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);
}
}
}
}
depth--;
return value;
};
/**
* @static
* @method _decode
* @param {Object} value
* @param {Array} circular
* @param {Object} registry
* @return {Object}
* @private
*/
JsonEx._decode = function(value, circular, registry) {
var type = Object.prototype.toString.call(value);
if (type === '[object Object]' || type === '[object Array]') {
registry[value['@c']] = value;
if (value['@'] === null) {
value = this._resetPrototype(value, null);
} else if (value['@']) {
var constructor = window[value['@']];
if (constructor) {
value = this._resetPrototype(value, constructor.prototype);
}
}
for (var key in value) {
if (!value.hasOwnProperty || 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);
}
}
}
return value;
};
/**
* @static
* @method _getConstructorName
* @param {Object} value
* @return {String}
* @private
*/
JsonEx._getConstructorName = function(value) {
if (!value.constructor) {
return null;
}
var name = value.constructor.name;
if (name === undefined) {
var func = /^\s*function\s*([A-Za-z0-9_$]*)/;
name = func.exec(value.constructor)[1];
}
return name;
};
/**
* @static
* @method _resetPrototype
* @param {Object} value
* @param {Object} prototype
* @return {Object}
* @private
*/
JsonEx._resetPrototype = function(value, prototype) {
if (Object.setPrototypeOf !== undefined) {
Object.setPrototypeOf(value, prototype);
} else if ('__proto__' in value) {
value.__proto__ = prototype;
} else {
var newValue = Object.create(prototype);
for (var key in value) {
if (value.hasOwnProperty(key)) {
newValue[key] = value[key];
}
}
value = newValue;
}
return value;
};