Skip to content

Commit

Permalink
supports ND cell array, fix #66
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jul 8, 2020
1 parent 5a58faf commit ce40fdf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
50 changes: 17 additions & 33 deletions savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
elseif(isnumeric(item) || islogical(item))
txt=mat2json(name,item,level,varargin{:});
elseif(ischar(item))
if(numel(item)>=varargin{1}.compressstringsize)
if(~isempty(varargin{1}.compression) && numel(item)>=varargin{1}.compressstringsize)
txt=mat2json(name,item,level,varargin{:});
else
txt=str2json(name,item,level,varargin{:});
Expand Down Expand Up @@ -294,7 +294,6 @@
isnum2cell=varargin{1}.num2cell_;
if(isnum2cell)
item=squeeze(item);
else
format=varargin{1}.formatversion;
if(format>1.9 && ~isvector(item))
item=permute(item,ndims(item):-1:1);
Expand All @@ -309,43 +308,31 @@
len=numel(item);
ws=varargin{1}.whitespaces_;
padding0=repmat(ws.tab,1,level);
padding2=repmat(ws.tab,1,level+1);
nl=ws.newline;
bracketlevel=~varargin{1}.singletcell;
if(len>bracketlevel)
if(~isa(item,'string'))
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[', nl}; name='';
else
txt={padding0, '[', nl};
end
if(~isempty(name))
txt={padding0, '"', decodevarname(name,varargin{1}.unpackhex),'":[', nl}; name='';
else
txt={padding0, '[', nl};
end
elseif(len==0)
if(~isempty(name))
txt={padding0, '"' decodevarname(name,varargin{1}.unpackhex) '":[]'}; name='';
else
txt={padding0, '[]'};
end
txt = sprintf('%s',txt{:});
return;
end
for j=1:dim(2)
if(dim(1)>1)
txt(end+1:end+3)={padding2,'[',nl};
end
for i=1:dim(1)
txt{end+1}=obj2json(name,item{i,j},level+(dim(1)>1)+(len>bracketlevel),varargin{:});
if(i<dim(1))
txt(end+1:end+2)={',' nl};
end
end
if(dim(1)>1)
txt(end+1:end+3)={nl,padding2,']'};
end
if(j<dim(2))
txt(end+1:end+2)={',' nl};
end
%if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end
if(size(item,1)>1)
item=num2cell(item,2:ndims(item))';
end
if(len>bracketlevel && ~isa(item,'string'))
idx=num2cell(1:length(item));
sep={[',' nl],''};
txt=[txt{:},cellfun(@(x,id) [obj2json(name,x,level+(dim(1)>1)+(len>bracketlevel),varargin{:}), sep{(id==length(item))+1}], item, idx, 'UniformOutput',false)];

if(len>bracketlevel)
txt(end+1:end+3)={nl,padding0,']'};
end
txt = sprintf('%s',txt{:});
Expand Down Expand Up @@ -718,14 +705,11 @@
mat=permute(mat,ndims(mat):-1:1);
end
varargin{1}.num2cell_=1;
varargin{1}.singletcell=0;
txt=cell2json('',num2cell(mat,1),level-1,varargin{:});
return;
else
if(isnest)
if(isnum2cell)
mat=mat(:).';
end
end
elseif(isvector(mat) && isnum2cell==1)
mat=mat(:).';
end

if(size(mat,1)==1)
Expand Down
6 changes: 3 additions & 3 deletions test/run_jsonlab_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function run_jsonlab_test(tests)
test_jsonlab('empty string',@savejson,'','""','compact',1);
test_jsonlab('string escape',@savejson,sprintf('jdata\n\b\ashall\tprevail\t"\"\\'),'"jdata\n\b\ashall\tprevail\t\"\"\\"');
if(exist('isstring'))
test_jsonlab('string type',@savejson,string(sprintf('jdata\n\b\ashall\tprevail')),'"jdata\n\b\ashall\tprevail"','compact',1);
test_jsonlab('string array',@savejson,[string('jdata');string('shall');string('prevail')],'["jdata","shall","prevail"]','compact',1);
test_jsonlab('string type',@savejson,string(sprintf('jdata\n\b\ashall\tprevail')),'["jdata\n\b\ashall\tprevail"]','compact',1);
test_jsonlab('string array',@savejson,[string('jdata'),string('shall'),string('prevail')],'["jdata","shall","prevail"]','compact',1);
end
test_jsonlab('row vector',@savejson,[1,2,3],'[1,2,3]');
test_jsonlab('column vector',@savejson,[1;2;3],'[[1],[2],[3]]','compact',1);
Expand All @@ -58,7 +58,7 @@ function run_jsonlab_test(tests)
test_jsonlab('3d (row-major) nested array',@savejson,reshape(1:(2*3*2),2,3,2),...
'[[[1,7],[3,9],[5,11]],[[2,8],[4,10],[6,12]]]','compact',1,'nestarray',1);
test_jsonlab('3d (column-major) nested array',@savejson,reshape(1:(2*3*2),2,3,2),...
'[[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]]','compact',1,'nestarray',1,'formatversion',1.9);
'[[[1,2],[7,8]],[[3,4],[9,10]],[[5,6],[11,12]]]','compact',1,'nestarray',1,'formatversion',1.9);
test_jsonlab('3d annotated array',@savejson,reshape(int8(1:(2*3*2)),2,3,2),...
'{"_ArrayType_":"int8","_ArraySize_":[2,3,2],"_ArrayData_":[1,7,3,9,5,11,2,8,4,10,6,12]}','compact',1);
test_jsonlab('complex number',@savejson,single(2+4i),...
Expand Down

0 comments on commit ce40fdf

Please sign in to comment.