Skip to content

Commit 3fc3e19

Browse files
Alamantusrobbie-inquisicorp
authored andcommitted
Apply PR from karlr42:
tcorral#3
1 parent 2230d7e commit 3fc3e19

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/JSONC.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,27 @@
272272
*/
273273
JSONC.pack = function (json, bCompress) {
274274
var str = JSON.stringify((bCompress ? JSONC.compress(json) : json));
275-
return Base64.encode(String.fromCharCode.apply(String, gzip.zip(str,{level:9})));
275+
var zipped = gzip.zip(str, { level: 9 });
276+
var data;
277+
try {
278+
data = String.fromCharCode.apply(String, zipped);
279+
} catch (e) {
280+
if (e instanceof RangeError) {
281+
//Hit the max number of arguments for the JS engine
282+
data = (function (buffer) {
283+
var binary = '';
284+
var bytes = new Uint8Array(buffer);
285+
var len = bytes.byteLength;
286+
for (var i = 0; i < len; i++) {
287+
binary += String.fromCharCode(bytes[i]);
288+
}
289+
return binary;
290+
}(zipped));
291+
} else {
292+
throw (e);
293+
}
294+
}
295+
return Base64.encode(data);
276296
};
277297
/**
278298
* Decompress a compressed JSON
@@ -307,9 +327,27 @@
307327
* @returns {Object}
308328
*/
309329
JSONC.unpack = function (gzipped, bDecompress) {
310-
var aArr = getArr(Base64.decode(gzipped)),
311-
str = String.fromCharCode.apply(String, gzip.unzip(aArr,{level:9})),
312-
json = JSON.parse(str);
330+
var aArr = getArr(Base64.decode(gzipped));
331+
var str, unzipped = gzip.unzip(aArr, { level: 9 });
332+
try {
333+
str = String.fromCharCode.apply(String, unzipped);
334+
} catch (e) {
335+
if (e instanceof RangeError) {
336+
//Hit the max number of arguments for the JS engine
337+
str = (function (buffer) {
338+
var binary = '';
339+
var bytes = new Uint8Array(buffer);
340+
var len = bytes.byteLength;
341+
for (var i = 0; i < len; i++) {
342+
binary += String.fromCharCode(bytes[i]);
343+
}
344+
return binary;
345+
}(unzipped));
346+
} else {
347+
throw (e);
348+
}
349+
}
350+
var json = JSON.parse(str);
313351
return bDecompress ? JSONC.decompress(json) : json;
314352
};
315353
/*

test/JSONC.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,32 @@ describe('JSONC.decompress', function (){
355355

356356
expect(retrieved).toEqual(obj);
357357

358+
delete window.Base64;
359+
delete window.gzip;
360+
});
361+
it('should test decompressing a large object', function () {
362+
var retrieved,
363+
obj = largeTestObj.origData,
364+
packed = largeTestObj.gzData;
365+
366+
window.gzip = {
367+
unzip: function () {
368+
/*
369+
* need to split the array in two because PhantomJS can't even parse the full size
370+
*/
371+
return largeTestObj.gunzippedData_0.concat(largeTestObj.gunzippedData_1);
372+
}
373+
};
374+
window.Base64 = {
375+
decode: function (str) {
376+
return str;
377+
}
378+
};
379+
380+
retrieved = JSONC.unpack(packed);
381+
382+
expect(retrieved).toEqual(obj);
383+
358384
delete window.Base64;
359385
delete window.gzip;
360386
});

vendor/base64.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ var Base64 = {
8383

8484
// private method for UTF-8 encoding
8585
_utf8_encode : function (string) {
86-
string = string.replace(/\r\n/g,"\n");
8786
var utftext = "";
8887

8988
for (var n = 0; n < string.length; n++) {

0 commit comments

Comments
 (0)