|
272 | 272 | */ |
273 | 273 | JSONC.pack = function (json, bCompress) { |
274 | 274 | 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); |
276 | 296 | }; |
277 | 297 | /** |
278 | 298 | * Decompress a compressed JSON |
|
307 | 327 | * @returns {Object} |
308 | 328 | */ |
309 | 329 | 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); |
313 | 351 | return bDecompress ? JSONC.decompress(json) : json; |
314 | 352 | }; |
315 | 353 | /* |
|
0 commit comments