-
Notifications
You must be signed in to change notification settings - Fork 90
/
JSONC.js
327 lines (303 loc) · 7.26 KB
/
JSONC.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*global gzip, Base64*/
(function () {
var root,
JSONC = {},
isNodeEnvironment,
_nCode = -1,
toString = {}.toString;
/**
* set the correct root depending from the environment.
* @type {Object}
* @private
*/
root = this;
/**
* Check if JSONC is loaded in Node.js environment
* @type {Boolean}
* @private
*/
isNodeEnvironment = typeof exports === 'object' && typeof module === 'object' && typeof module.exports === 'object' && typeof require === 'function';
/**
* Checks if the value exist in the array.
* @param arr
* @param v
* @returns {boolean}
*/
function contains(arr, v) {
var nIndex,
nLen = arr.length;
for (nIndex = 0; nIndex < nLen; nIndex++) {
if (arr[nIndex][1] === v) {
return true;
}
}
return false;
}
/**
* Removes duplicated values in an array
* @param oldArray
* @returns {Array}
*/
function unique(oldArray) {
var nIndex,
nLen = oldArray.length,
aArr = [];
for (nIndex = 0; nIndex < nLen; nIndex++) {
if (!contains(aArr, oldArray[nIndex][1])) {
aArr.push(oldArray[nIndex]);
}
}
return aArr;
}
/**
* Escapes a RegExp
* @param text
* @returns {*}
*/
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
/**
* Returns if the obj is an object or not.
* @param obj
* @returns {boolean}
* @private
*/
function _isObject(obj) {
return toString.call(obj) === '[object Object]';
}
/**
* Returns if the obj is an array or not
* @param obj
* @returns {boolean}
* @private
*/
function _isArray(obj) {
return toString.call(obj) === '[object Array]';
}
/**
* Converts a bidimensional array to object
* @param aArr
* @returns {{}}
* @private
*/
function _biDimensionalArrayToObject(aArr) {
var obj = {},
nIndex,
nLen = aArr.length,
oItem;
for (nIndex = 0; nIndex < nLen; nIndex++) {
oItem = aArr[nIndex];
obj[oItem[0]] = oItem[1];
}
return obj;
}
/**
* Convert a number to their ascii code/s.
* @param index
* @param totalChar
* @param offset
* @returns {Array}
* @private
*/
function _numberToKey(index, totalChar, offset) {
var sKeys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=_!?()*',
aArr = [],
currentChar = index;
totalChar = totalChar || sKeys.length;
offset = offset || 0;
while (currentChar >= totalChar) {
aArr.push(sKeys.charCodeAt((currentChar % totalChar) + offset));
currentChar = Math.floor(currentChar / totalChar - 1);
}
aArr.push(sKeys.charCodeAt(currentChar + offset));
return aArr.reverse();
}
/**
* Returns the string using an array of ASCII values
* @param aKeys
* @returns {string}
* @private
*/
function _getSpecialKey(aKeys) {
return String.fromCharCode.apply(String, aKeys);
}
/**
* Traverse all the objects looking for keys and set an array with the new keys
* @param json
* @param aKeys
* @returns {*}
* @private
*/
function _getKeys(json, aKeys) {
var aKey,
sKey,
oItem;
for (sKey in json) {
if (json.hasOwnProperty(sKey)) {
oItem = json[sKey];
if (_isObject(oItem) || _isArray(oItem)) {
aKeys = aKeys.concat(unique(_getKeys(oItem, aKeys)));
}
if (isNaN(Number(sKey))) {
if (!contains(aKeys, sKey)) {
_nCode += 1;
aKey = [];
aKey.push(_getSpecialKey(_numberToKey(_nCode)), sKey);
aKeys.push(aKey);
}
}
}
}
return aKeys;
}
/**
* Method to compress array objects
* @private
* @param json
* @param aKeys
*/
function _compressArray(json, aKeys) {
var nIndex,
nLenKeys;
for (nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++) {
json[nIndex] = JSONC.compress(json[nIndex], aKeys);
}
}
/**
* Method to compress anything but array
* @private
* @param json
* @param aKeys
* @returns {*}
*/
function _compressOther(json, aKeys) {
var oKeys,
aKey,
str,
nLenKeys,
nIndex,
obj;
aKeys = _getKeys(json, aKeys);
aKeys = unique(aKeys);
oKeys = _biDimensionalArrayToObject(aKeys);
str = JSON.stringify(json);
nLenKeys = aKeys.length;
for (nIndex = 0; nIndex < nLenKeys; nIndex++) {
aKey = aKeys[nIndex];
str = str.replace(new RegExp(escapeRegExp('"' + aKey[1] + '"'), 'g'), '"' + aKey[0] + '"');
}
obj = JSON.parse(str);
obj._ = oKeys;
return obj;
}
/**
* Method to decompress array objects
* @private
* @param json
*/
function _decompressArray(json) {
var nIndex, nLenKeys;
for (nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++) {
json[nIndex] = JSONC.decompress(json[nIndex]);
}
}
/**
* Method to decompress anything but array
* @private
* @param jsonCopy
* @returns {*}
*/
function _decompressOther(jsonCopy) {
var oKeys, str, sKey;
oKeys = JSON.parse(JSON.stringify(jsonCopy._));
delete jsonCopy._;
str = JSON.stringify(jsonCopy);
for (sKey in oKeys) {
if (oKeys.hasOwnProperty(sKey)) {
str = str.replace(new RegExp('"' + sKey + '"', 'g'), '"' + oKeys[sKey] + '"');
}
}
return str;
}
/**
* Compress a RAW JSON
* @param json
* @param optKeys
* @returns {*}
*/
JSONC.compress = function (json, optKeys) {
if (!optKeys) {
_nCode = -1;
}
var aKeys = optKeys || [],
obj;
if (_isArray(json)) {
_compressArray(json, aKeys);
obj = json;
}
else {
obj = _compressOther(json, aKeys);
}
return obj;
};
/**
* Use LZString to get the compressed string.
* @param json
* @param bCompress
* @returns {String}
*/
JSONC.pack = function (json, bCompress) {
var str = JSON.stringify((bCompress ? JSONC.compress(json) : json));
return Base64.encode(String.fromCharCode.apply(String, gzip.zip(str,{level:9})));
};
/**
* Decompress a compressed JSON
* @param json
* @returns {*}
*/
JSONC.decompress = function (json) {
var str,
jsonCopy = JSON.parse(JSON.stringify(json));
if (_isArray(jsonCopy)) {
_decompressArray(jsonCopy);
}
else {
str = _decompressOther(jsonCopy);
}
return str ? JSON.parse(str) : jsonCopy;
};
function getArr(str) {
var nIndex = 0,
nLen = str.length,
arr = [];
for (; nIndex < nLen; nIndex++) {
arr.push(str.charCodeAt(nIndex));
}
return arr;
}
/**
* Returns the JSON object from the LZW string
* @param gzipped
* @param bDecompress
* @returns {Object}
*/
JSONC.unpack = function (gzipped, bDecompress) {
var aArr = getArr(Base64.decode(gzipped)),
str = String.fromCharCode.apply(String, gzip.unzip(aArr,{level:9})),
json = JSON.parse(str);
return bDecompress ? JSONC.decompress(json) : json;
};
/*
* Expose Hydra to be used in node.js, as AMD module or as global
*/
root.JSONC = JSONC;
if (isNodeEnvironment) {
module.exports = JSONC;
}
else if (typeof define !== 'undefined') {
define('jsoncomp', [], function () {
return JSONC;
});
}
}.call(this));