diff --git a/jdatadecode.m b/jdatadecode.m index 24472c7..4b23e3d 100644 --- a/jdatadecode.m +++ b/jdatadecode.m @@ -124,7 +124,7 @@ decompfun=str2func([zipmethod 'decode']); arraytype=data(j).(N_('_ArrayType_')); chartype=0; - if(strcmp(arraytype,'char')) + if(strcmp(arraytype,'char') || strcmp(arraytype,'logical')) chartype=1; arraytype='uint8'; end diff --git a/jsave.m b/jsave.m index d2541e2..bb0861c 100644 --- a/jsave.m +++ b/jsave.m @@ -96,4 +96,4 @@ function jsave(filename, varargin) savefun('WorkspaceHeader',header,'filename',filename,varargin{:}); savefun('WorkspaceData',body,'filename',filename,'append',1,... - 'compression','zlib',varargin{:}); + 'compression','zlib','keeptype',1,'array2struct',1,varargin{:}); diff --git a/loadjson.m b/loadjson.m index 00be04a..da1e1f0 100644 --- a/loadjson.m +++ b/loadjson.m @@ -25,7 +25,7 @@ % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % -% SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat +% SimplifyCell [1|0]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % FastArrayParser [1|0 or integer]: if set to 1, use a @@ -281,22 +281,27 @@ end if(varargin{1}.simplifycell) - try - oldobj=object; - if(iscell(object) && length(object)>1 && ndims(object{1})>=2) - catdim=size(object{1}); - catdim=ndims(object{1})-(catdim(end)==1)+1; - object=cat(catdim,object{:}); - object=permute(object,ndims(object):-1:1); - else - object=cell2mat(object')'; - end - if(iscell(oldobj) && isstruct(object) && numel(object)>1 && varargin{1}.simplifycellarray==0) - object=oldobj; - elseif(~iscell(object) && size(object,1)>1 && ndims(object)==2) + if(iscell(object) && ~isempty(object) && isnumeric(object{1})) + if(all(cellfun(@(e) isequal(size(object{1}), size(e)) , object(2:end)))) + try + oldobj=object; + if(iscell(object) && length(object)>1 && ndims(object{1})>=2) + catdim=size(object{1}); + catdim=ndims(object{1})-(catdim(end)==1)+1; + object=cat(catdim,object{:}); + object=permute(object,ndims(object):-1:1); + else + object=cell2mat(object')'; + end + if(iscell(oldobj) && isstruct(object) && numel(object)>1 && varargin{1}.simplifycellarray==0) + object=oldobj; + end + catch + end + end + end + if(~iscell(object) && size(object,1)>1 && ndims(object)==2) object=object'; - end - catch end end pos=parse_char(inputstr, pos, ']'); diff --git a/loadmsgpack.m b/loadmsgpack.m index 516e0ea..87ec3b3 100644 --- a/loadmsgpack.m +++ b/loadmsgpack.m @@ -228,20 +228,22 @@ end if(varargin{1}.simplifycell) if(iscell(out) && ~isempty(out) && isnumeric(out{1})) - try - oldobj=out; - if(iscell(out) && length(out)>1 && ndims(out{1})>=2) - catdim=size(out{1}); - catdim=ndims(out{1})-(catdim(end)==1)+1; - out=cat(catdim,out{:}); - out=permute(out,ndims(out):-1:1); - else - out=cell2mat(out')'; + if(all(cellfun(@(e) isequal(size(out{1}), size(e)) , out(2:end)))) + try + oldobj=out; + if(iscell(out) && length(out)>1 && ndims(out{1})>=2) + catdim=size(out{1}); + catdim=ndims(out{1})-(catdim(end)==1)+1; + out=cat(catdim,out{:}); + out=permute(out,ndims(out):-1:1); + else + out=cell2mat(out')'; + end + if(iscell(oldobj) && isstruct(out) && numel(out)>1 && varargin{1}.simplifycellarray==0) + out=oldobj; + end + catch end - if(iscell(oldobj) && isstruct(out) && numel(out)>1 && varargin{1}.simplifycellarray==0) - out=oldobj; - end - catch end end if(~iscell(out) && size(out,2)>1 && ndims(out)==2) diff --git a/loadubjson.m b/loadubjson.m index 659bc12..180c449 100644 --- a/loadubjson.m +++ b/loadubjson.m @@ -72,7 +72,7 @@ opt=varargin2struct(varargin{:}); opt.arraytoken_=arraytoken; - opt.simplifycell=jsonopt('SimplifyCell',0,opt); + opt.simplifycell=jsonopt('SimplifyCell',1,opt); opt.simplifycellarray=jsonopt('SimplifyCellArray',0,opt); opt.usemap=jsonopt('UseMap',0,opt); opt.nameisstring=jsonopt('NameIsString',0,opt); @@ -193,21 +193,23 @@ end if(varargin{1}.simplifycell) if(iscell(object) && ~isempty(object) && isnumeric(object{1})) - try - oldobj=object; - if(iscell(object) && length(object)>1 && ndims(object{1})>=2) - catdim=size(object{1}); - catdim=ndims(object{1})-(catdim(end)==1)+1; - object=cat(catdim,object{:}); - object=permute(object,ndims(object):-1:1); - else - object=cell2mat(object')'; - end - if(iscell(oldobj) && isstruct(object) && numel(object)>1 && varargin{1}.simplifycellarray==0) - object=oldobj; - end - catch - end + if(all(cellfun(@(e) isequal(size(object{1}), size(e)) , object(2:end)))) + try + oldobj=object; + if(iscell(object) && length(object)>1 && ndims(object{1})>=2) + catdim=size(object{1}); + catdim=ndims(object{1})-(catdim(end)==1)+1; + object=cat(catdim,object{:}); + object=permute(object,ndims(object):-1:1); + else + object=cell2mat(object')'; + end + if(iscell(oldobj) && isstruct(object) && numel(object)>1 && varargin{1}.simplifycellarray==0) + object=oldobj; + end + catch + end + end end if(~iscell(object) && size(object,1)>1 && ndims(object)==2) object=object'; diff --git a/saveubjson.m b/saveubjson.m index 65bac6f..ca871be 100644 --- a/saveubjson.m +++ b/saveubjson.m @@ -156,6 +156,7 @@ opt.messagepack=jsonopt('MessagePack',0,opt); opt.num2cell_=0; opt.ubjson=bitand(jsonopt('UBJSON',0,opt), ~opt.messagepack); +opt.keeptype=jsonopt('KeepType',0,opt); opt.nosubstruct_=0; if(jsonopt('PreEncode',1,opt)) @@ -375,7 +376,7 @@ Amarker=varargin{1}.AM_; Omarker=varargin{1}.OM_; -if(isfield(item,N_('_ArrayType_',varargin{:}))) +if(isfield(item,encodevarname('_ArrayType_',varargin{:}))) varargin{1}.nosubstruct_=1; end @@ -721,11 +722,19 @@ varargin{1}.num2cell_=1; end -if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) +if(isa(mat,'integer') || isinteger(mat) || (~varargin{1}.keeptype && isfloat(mat) && all(mod(mat(:),1) == 0))) if(~isvector(mat) && isnest==1) txt=cell2ubjson('',num2cell(mat,1),level,varargin{:}); elseif(~ismsgpack || size(mat,1)==1) - if(~any(mat<0)) + if(0 && varargin{1}.keeptype) + itype=class(mat); + idx=find(ismember(varargin{1}.IType_,itype)); + if(isempty(idx)) + idx=find(ismember(varargin{1}.IType_,itype(2:end))); + end + type=Imarker(idx); + varargin{1}.inttype_=idx; + elseif(~any(mat<0)) cid=varargin{1}.IType_; type=Imarker(end); maxdata=max(double(mat(:))); @@ -826,8 +835,16 @@ error('input is not an integer'); end Imarker=varargin{1}.IM_; +cid=varargin{1}.IType_; isdebug=varargin{1}.debug; - +if(isfield(varargin{1},'inttype_')) + if(isdebug) + val=[Imarker(varargin{1}.inttype_) sprintf('<%d>',num)]; + else + val=[Imarker(varargin{1}.inttype_) data2byte(swapbytes(cast(num,cid{varargin{1}.inttype_})),'uint8')]; + end + return; +end if(Imarker(1)~='U') if(num>=0 && num<127) val=uint8(num); @@ -843,7 +860,6 @@ return; end key=Imarker; -cid=varargin{1}.IType_; for i=1:length(cid) if(num==cast(num,cid{i})) if(isdebug)