Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function(config) {
// - PhantomJS
// - IE (only Windows)
// CLI --browsers Chrome,Firefox,Safari
browsers: ['PhantomJS'],
browsers: ['Chrome'],

// If browser does not capture in given timeout [ms], kill it
// CLI --capture-timeout 5000
Expand Down
46 changes: 42 additions & 4 deletions src/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,27 @@
*/
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})));
var zipped = gzip.zip(str,{level:9});
var data;
try{
data = String.fromCharCode.apply(String,zipped);
}catch(e){
if(e instanceof RangeError){
//Hit the max number of arguments for the JS engine
data = (function(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i=0; i<len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}(zipped));
}else{
throw(e);
}
}
return Base64.encode(data);
};
/**
* Decompress a compressed JSON
Expand Down Expand Up @@ -307,9 +327,27 @@
* @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);
var aArr = getArr(Base64.decode(gzipped));
var str, unzipped = gzip.unzip(aArr,{level:9});
try{
str = String.fromCharCode.apply(String, unzipped);
}catch(e){
if(e instanceof RangeError){
//Hit the max number of arguments for the JS engine
str = (function(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i=0; i<len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}(unzipped));
}else{
throw(e);
}
}
var json = JSON.parse(str);
return bDecompress ? JSONC.decompress(json) : json;
};
/*
Expand Down
28 changes: 28 additions & 0 deletions test/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ describe('JSONC.decompress', function (){

expect(retrieved).toEqual(obj);

delete window.Base64;
delete window.gzip;
});
it('should test decompressing a large object', function(){
var retrieved,
obj = largeTestObj.origData,
packed = largeTestObj.gzData;

window.gzip = {
unzip: function()
{
/*
* need to split the array in two because PhantomJS can't even parse the full size
*/
return largeTestObj.gunzippedData_0.concat(largeTestObj.gunzippedData_1);
}
};
window.Base64 = {
decode: function( str )
{
return str;
}
};

retrieved = JSONC.unpack( packed );

expect(retrieved).toEqual(obj);

delete window.Base64;
delete window.gzip;
});
Expand Down
248,180 changes: 248,180 additions & 0 deletions test/largeData.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion vendor/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ var Base64 = {

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

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