Skip to content

Commit

Permalink
add keeptype option to jsave and saveubjson
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jun 3, 2020
1 parent 584de19 commit 15ca7ae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
2 changes: 1 addition & 1 deletion jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jsave.m
Original file line number Diff line number Diff line change
Expand Up @@ -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{:});
37 changes: 21 additions & 16 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ']');
Expand Down
28 changes: 15 additions & 13 deletions loadmsgpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 18 additions & 16 deletions loadubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down
26 changes: 21 additions & 5 deletions saveubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(:)));
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit 15ca7ae

Please sign in to comment.