|
67 | 67 | % back to the string form
|
68 | 68 | % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.
|
69 | 69 | % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)
|
70 |
| -% |
| 70 | +% opt.Compression 'zlib' or 'gzip': specify array compression |
| 71 | +% method; currently only support 'gzip' or 'zlib'. |
| 72 | +% opt.CompressArraySize [0|int]: only compress arrays with a total |
| 73 | +% element count larger than this number. |
71 | 74 | % opt can be replaced by a list of ('param',value) pairs. The param
|
72 | 75 | % string is equivallent to a field in opt and is case sensitive.
|
73 | 76 | % output:
|
|
105 | 108 | opt=varargin2struct(varargin{:});
|
106 | 109 | end
|
107 | 110 | opt.IsOctave=exist('OCTAVE_VERSION','builtin');
|
| 111 | + |
| 112 | +dozip=jsonopt('Compression','',opt); |
| 113 | +if(~opt.IsOctave && ~isempty(dozip)) |
| 114 | + if(~(strcmpi(dozip,'gzip') || strcmpi(dozip,'zlib'))) |
| 115 | + error('compression method "%s" is not supported',dozip); |
| 116 | + end |
| 117 | + try |
| 118 | + error(javachk('jvm')); |
| 119 | + matlab.net.base64decode('test'); |
| 120 | + catch |
| 121 | + error('java-based compression is not supported'); |
| 122 | + end |
| 123 | + opt.Compression=dozip; |
| 124 | +end |
| 125 | + |
108 | 126 | if(isfield(opt,'norowbracket'))
|
109 | 127 | warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)');
|
110 | 128 | if(~isfield(opt,'singletarray'))
|
|
370 | 388 | nl=ws.newline;
|
371 | 389 | sep=ws.sep;
|
372 | 390 |
|
| 391 | +dozip=jsonopt('Compression','',varargin{:}); |
| 392 | +zipsize=jsonopt('CompressArraySize',0,varargin{:}); |
| 393 | + |
373 | 394 | if(length(size(item))>2 || issparse(item) || ~isreal(item) || ...
|
374 |
| - (isempty(item) && any(size(item))) ||jsonopt('ArrayToStruct',0,varargin{:})) |
| 395 | + (isempty(item) && any(size(item))) ||jsonopt('ArrayToStruct',0,varargin{:}) || (~isempty(dozip) && numel(item)>zipsize)) |
375 | 396 | if(isempty(name))
|
376 | 397 | txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',...
|
377 | 398 | padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl);
|
|
411 | 432 | txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep);
|
412 | 433 | end
|
413 | 434 | txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep);
|
414 |
| - if(size(item,1)==1) |
415 |
| - % Row vector, store only column indices. |
416 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
417 |
| - matdata2json([iy(:),data'],level+2,varargin{:}), nl); |
418 |
| - elseif(size(item,2)==1) |
419 |
| - % Column vector, store only row indices. |
420 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
421 |
| - matdata2json([ix,data],level+2,varargin{:}), nl); |
| 435 | + if(~isempty(dozip) && numel(data*2)>zipsize) |
| 436 | + if(size(item,1)==1) |
| 437 | + % Row vector, store only column indices. |
| 438 | + fulldata=[iy(:),data']; |
| 439 | + elseif(size(item,2)==1) |
| 440 | + % Column vector, store only row indices. |
| 441 | + fulldata=[ix,data]; |
| 442 | + else |
| 443 | + % General case, store row and column indices. |
| 444 | + fulldata=[ix,iy,data]; |
| 445 | + end |
| 446 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressionSize_": ',regexprep(mat2str(size(fulldata)),'\s+',','), sep); |
| 447 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressionMethod_": "',dozip, ['"' sep]); |
| 448 | + if(strcmpi(dozip,'gzip')) |
| 449 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressedData_": "',base64encode(gzipencode(typecast(fulldata(:),'uint8'))),['"' nl]); |
| 450 | + elseif(strcmpi(dozip,'zlib')) |
| 451 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressedData_": "',base64encode(zlibencode(typecast(fulldata(:),'uint8'))),['"' nl]); |
| 452 | + else |
| 453 | + error('compression method not supported'); |
| 454 | + end |
422 | 455 | else
|
423 |
| - % General case, store row and column indices. |
424 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
425 |
| - matdata2json([ix,iy,data],level+2,varargin{:}), nl); |
| 456 | + if(size(item,1)==1) |
| 457 | + % Row vector, store only column indices. |
| 458 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
| 459 | + matdata2json([iy(:),data'],level+2,varargin{:}), nl); |
| 460 | + elseif(size(item,2)==1) |
| 461 | + % Column vector, store only row indices. |
| 462 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
| 463 | + matdata2json([ix,data],level+2,varargin{:}), nl); |
| 464 | + else |
| 465 | + % General case, store row and column indices. |
| 466 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
| 467 | + matdata2json([ix,iy,data],level+2,varargin{:}), nl); |
| 468 | + end |
426 | 469 | end
|
427 | 470 | else
|
428 |
| - if(isreal(item)) |
429 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
430 |
| - matdata2json(item(:)',level+2,varargin{:}), nl); |
| 471 | + if(~isempty(dozip) && numel(item)>zipsize) |
| 472 | + if(isreal(item)) |
| 473 | + fulldata=item(:)'; |
| 474 | + else |
| 475 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); |
| 476 | + fulldata=[real(item(:)) imag(item(:))]; |
| 477 | + end |
| 478 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressionSize_": ',regexprep(mat2str(size(fulldata)),'\s+',','), sep); |
| 479 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressionMethod_": "',dozip, ['"' sep]); |
| 480 | + if(strcmpi(dozip,'gzip')) |
| 481 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressedData_": "',base64encode(gzipencode(typecast(fulldata(:),'uint8'))),['"' nl]); |
| 482 | + elseif(strcmpi(dozip,'zlib')) |
| 483 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayCompressedData_": "',base64encode(zlibencode(typecast(fulldata(:),'uint8'))),['"' nl]); |
| 484 | + else |
| 485 | + error('compression method not supported'); |
| 486 | + end |
431 | 487 | else
|
432 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); |
433 |
| - txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
| 488 | + if(isreal(item)) |
| 489 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
| 490 | + matdata2json(item(:)',level+2,varargin{:}), nl); |
| 491 | + else |
| 492 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); |
| 493 | + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... |
434 | 494 | matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);
|
| 495 | + end |
435 | 496 | end
|
436 | 497 | end
|
437 | 498 | txt=sprintf('%s%s%s',txt,padding1,'}');
|
|
0 commit comments