-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjsonZipper.js
303 lines (293 loc) · 8.21 KB
/
jsonZipper.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Author: Gerard Lamusse
Created: 08/2015
Version: 1.0
URL: https://github.com/u12206050/jsonZipper
*/
var jsonZipper = (function(){
var jz = function(_jsonObj, _options) {
var Z = this;
var MAP = [];
var opts = _options && typeof(_options) !== "boolean" ? _options : {};
/* Public Functions */
Z.zip = function() {
if (Z.status === "zipable") {
Z.uzOpts = {I:[],A:Z.isArray,eC:[],iC:[]};
if (Z.isArray) {
var x = 0;
var y = Z.JO.length;
while (x < y) {
compress(Z.JO[x++]);
}
} else {
compress(Z.JO);
}
Z.status = "zipped";
return {M:MAP,D:Z.JO,O:Z.uzOpts};
} return false;
};
Z.unzip = function() {
if (Z.status === "unzipable") {
if (Z.isArray) {
var x = 0;
var y = Z.JO.length;
while (x < y) {
extract(Z.JO[x++]);
}
} else {
extract(Z.JO);
}
Z.status = "unzipped";
return Z.JO;
} return false;
};
Z.compress = function(obj) {
if (Z.status === "compressing") {
Z.JO.push(obj);
compress(obj);
} else if (Z.status === "ready to load object") {
Z.isArray = true;
Z.uzOpts = {I:[],A:Z.isArray,eC:[],iC:[]};
Z.status = "compressing";
Z.JO = [];
Z.JO.push(obj);
compress(obj);
} else return false;
return {M:MAP,D:Z.JO,O:Z.uzOpts};
};
var prevExtractIndex = false;
var extracted = [];
Z.extract = function(i) {
if (Z.status === "unzipable" || Z.status === "zipped") {
if (extracted.indexOf(i) > -1) {
prev = Z.JO[i];
} else {
if (!prevExtractIndex || prevExtractIndex+1 !== i) {
setPrev(i);
}
extract(Z.JO[i]);
extracted.push(i);
}
prevExtractIndex = i;
}
return Z.JO[i];
};
Z.length = function() {
return JSON.stringify(Z.JO).length + (MAP ? JSON.stringify(MAP).length : 0);
};
Z.options = function(opts,isArray) {
/* []: An array of key names that will be used as identifiers.
WARGING: Should be within every object, but repeating, NO Booleans or Integers allowed.
Hint: Most common values that can be guessed/used from previous objects */
Z.identifiers = opts.identifiers || [];
/* boolean: If _jsonObj is an array or not */
Z.isArray = opts.isArray || isArray;
/* []: An array of key names not to map or zip */
Z.exclude = opts.exclude || [];
/* []: An array of key names which values to include in mapping will need identifiers */
Z.include = opts.include || [];
/* []: An array of key names to be removed from the object */
Z.remove = opts.remove || false;
/* {}: An object containing key(s) to add, with function(s) which return the value */
Z.add = opts.add || false;
}
Z.load = function(_jsonObj, isJZobj) {
Z.startLength = 0;
MAP = [];
try {
var stringIT = JSON.stringify(_jsonObj);
Z.startLength = stringIT.length;
Z.JO = JSON.parse(stringIT);
}
catch (err) {
throw "The json object has recursive references or is too big to load into memory";
}
Z.status = "zipable";
if (isJZobj) {
if (Z.JO.D && Z.JO.O && Z.JO.M) {
MAP = Z.JO.M;
Z.identifiers = Z.JO.O.I || [];
Z.isArray = Z.JO.O.A;
Z.exclude = Z.JO.O.eC || false;
Z.include = Z.JO.O.iC || false;
Z.JO = Z.JO.D;
Z.remove = false;
Z.add = false;
Z.status = "unzipable";
} else
Z.options(isJZobj,_jsonObj.constructor === Array);
}
prev = false;
prevID = false;
};
/* Private Functions */
var getID = function(key, value) {
var mI = MAP.indexOf(key);
if (mI < 0) {
if (value) {
return MAP.push(key) - 1;
}
if (Z.exclude.indexOf(key) > -1) {
Z.uzOpts.eC.push(key);
return key;
} else {
mI = MAP.push(key) - 1;
if (Z.identifiers.indexOf(key) > -1) {
Z.uzOpts.I.push(mI);
}
if (Z.include.indexOf(key) > -1) {
Z.uzOpts.iC.push(mI);
}
}
}
return mI;
};
/* Compress the given object, taking note of the previous object */
var prev = false;
var prevID = false;
var compress = function(J) {
add(J);
var keys = Object.keys(J);
var prevSame = prev ? true : false;
var id = '';
var i=0;
for (xend=Z.identifiers.length; i<xend; i++) {
var ikey = Z.identifiers[i];
J[ikey] = getID(J[ikey],1);
id += J[ikey];
}
if (!prevSame || !prevID || prevID !== id) {
prevSame = false;
prev = J;
prevID = id;
}
i=0;
for (iend=keys.length; i<iend; i++) {
var key = keys[i];
if (Z.remove && Z.remove.indexOf(key) > -1)
delete J[key];
else {
var mI = getID(key);
if (prevSame && (MAP[prev[mI]] === J[key] || prev[mI] === J[key]))
delete J[key];
else if (Z.include.indexOf(key) > -1) {
if (Z.identifiers.indexOf(key) > -1)
J[mI] = J[key];
else J[mI] = getID(J[key],1);
delete J[key];
} else if (mI !== key) {
J[mI] = J[key];
delete J[key];
}
}
}
};
/* Extract the given object, taking note of the previous object */
var extract = function(J) {
if (J === prev)
return;
add(J);
var prevSame = Z.isArray ? isSame(prev, J) : false;
var keys = Object.keys(J);
if (prevSame)
extend(prev,J);
else if (Z.identifiers) {
var x=0;
for (xend=Z.identifiers.length; x<xend; x++) {
var ikey = Z.identifiers[x];
J[ikey] = MAP[J[ikey]];
}
}
var i=0;
for (iend=keys.length; i<iend; i++) {
var key = keys[i]*1;
var value = J[key];
if (Z.remove && Z.remove.indexOf(key) > -1)
delete J[key];
else {
if (Z.exclude.indexOf(key) > -1) {
J[key] = J[key];
if (Z.include.indexOf(key) > -1)
J[key] = MAP[J[key]];
} else {
if (Z.include.indexOf(key) > -1)
J[MAP[key]] = MAP[J[key]];
else
J[MAP[key]] = J[key];
delete J[key];
}
}
}
prev = J;
};
/* Add the additional keys and values to the given object */
var add = function(J) {
if (Z.add) {
for (var key in Z.add) {
if('undefined' !== typeof Z.add[key]){
if (typeof(Z.add[key]) === "function")
J[key] = Z.add[key](J);
else
J[key] = Z.add[key];
}
}
}
};
/* Set the previous full object from the current index, incl. */
var setPrev = function(i) {
if (i > 0) {
var x=0;
for (xend=Z.identifiers.length; x<xend; x++) {
if ('undefined' === typeof Z.JO[i][Z.identifiers[x]]) {
setPrev(i-1);
return;
}
}
extract(Z.JO[i]);
} else
extract(Z.JO[0]);
};
/* Checks if identiifiers match */
var isSame = function(obj1, obj2) {
if (Z.identifiers && obj1 && obj2 && obj1 !== obj2) {
var x=0;
for (xend=Z.identifiers.length; x<xend; x++) {
var key = Z.identifiers[x];
var mKey = MAP[Z.identifiers[x]];
if ('undefined' === typeof obj1[mKey] || ('undefined' !== typeof obj2[key] && MAP[obj2[key]] !== obj1[mKey]))
return false;
}
} else return false;
return true;
};
/* Merges an object by reference into the first one, replacing values from the second object into the first if duplicate keys exist */
var merge = function(obj1,obj2) {
for (var key in obj2) {
if('undefined' !== typeof obj2[key]) {
obj1[key] = obj2[key];
}
}
};
/* Adds all keys and values from the base to obj2 for each key that does not exist in obj2 */
var extend = function(base,obj2) {
for (var key in base) {
if('undefined' === typeof obj2[key]) {
obj2[key] = base[key];
}
}
};
Z.setID = opts.setID || false;
Z.options(opts,_jsonObj.constructor === Array)
Z.status = "ready to load object";
/* Check if object is given and if options is object or 'compressed' flag */
if (_jsonObj && typeof(_jsonObj) === "object") {
/* When unzipping an object ensure _options is true and not an object, once loaded, you can set the options */
if (_options && typeof(_options) === "boolean") {
Z.load(_jsonObj,true);
} else {
Z.load(_jsonObj,false);
}
}
};
return jz;
})();