Skip to content
Snippets Groups Projects
Commit d310d10b authored by Qianqian Fang's avatar Qianqian Fang
Browse files

saveubjson now supports 2D cell and struct arrays

git-svn-id: http://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab@438 786e58fb-9377-0410-9ff7-e4ac0ac0635c
parent a3035952
Branches
Tags
No related merge requests found
...@@ -157,5 +157,24 @@ fprintf(1,'%%=================================================\n\n') ...@@ -157,5 +157,24 @@ fprintf(1,'%%=================================================\n\n')
json2data=loadubjson(saveubjson('',loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3, "项目":"绝密"}'))) json2data=loadubjson(saveubjson('',loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3, "项目":"绝密"}')))
fprintf(1,'\n%%=================================================\n')
fprintf(1,'%% a 2D cell array\n')
fprintf(1,'%%=================================================\n\n')
data2json={{1,{2,3}},{4,5},{6};{7},{8,9},{10}};
saveubjson('data2json',data2json)
json2data=loadubjson(ans) % only savejson works for cell arrays, loadjson has issues
fprintf(1,'\n%%=================================================\n')
fprintf(1,'%% a 2D struct array\n')
fprintf(1,'%%=================================================\n\n')
data2json=repmat(struct('idx',0,'data','structs'),[2,3])
for i=1:6
data2json(i).idx=i;
end
saveubjson('data2json',data2json)
json2data=loadubjson(ans)
rand ('state',rngstate); rand ('state',rngstate);
...@@ -134,9 +134,11 @@ if(~iscell(item)) ...@@ -134,9 +134,11 @@ if(~iscell(item))
end end
dim=size(item); dim=size(item);
if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now
item=reshape(item,dim(1),numel(item)/dim(1));
dim=size(item);
end
len=numel(item); % let's handle 1D cell first len=numel(item); % let's handle 1D cell first
padding1='';
padding0='';
if(len>1) if(len>1)
if(~isempty(name)) if(~isempty(name))
txt=[S_(checkname(name,varargin{:})) '[']; name=''; txt=[S_(checkname(name,varargin{:})) '[']; name='';
...@@ -150,9 +152,13 @@ elseif(len==0) ...@@ -150,9 +152,13 @@ elseif(len==0)
txt='Z'; txt='Z';
end end
end end
for i=1:len for j=1:dim(2)
if(dim(1)>1) txt=[txt '[']; end
for i=1:dim(1)
txt=[txt obj2ubjson(name,item{i},level+(len>1),varargin{:})]; txt=[txt obj2ubjson(name,item{i},level+(len>1),varargin{:})];
end end
if(dim(1)>1) txt=[txt ']']; end
end
if(len>1) txt=[txt ']']; end if(len>1) txt=[txt ']']; end
%%------------------------------------------------------------------------- %%-------------------------------------------------------------------------
...@@ -161,31 +167,36 @@ txt=''; ...@@ -161,31 +167,36 @@ txt='';
if(~isstruct(item)) if(~isstruct(item))
error('input is not a struct'); error('input is not a struct');
end end
dim=size(item);
if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now
item=reshape(item,dim(1),numel(item)/dim(1));
dim=size(item);
end
len=numel(item); len=numel(item);
padding1='';
padding0='';
sep=',';
if(~isempty(name)) if(~isempty(name))
if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end
else else
if(len>1) txt='['; end if(len>1) txt='['; end
end end
for e=1:len for j=1:dim(2)
names = fieldnames(item(e)); if(dim(1)>1) txt=[txt '[']; end
for i=1:dim(1)
names = fieldnames(item(i,j));
if(~isempty(name) && len==1) if(~isempty(name) && len==1)
txt=[txt S_(checkname(name,varargin{:})) '{']; txt=[txt S_(checkname(name,varargin{:})) '{'];
else else
txt=[txt '{']; txt=[txt '{'];
end end
if(~isempty(names)) if(~isempty(names))
for i=1:length(names) for e=1:length(names)
txt=[txt obj2ubjson(names{i},getfield(item(e),... txt=[txt obj2ubjson(names{e},getfield(item(i,j),...
names{i}),level+1+(len>1),varargin{:})]; names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];
end end
end end
txt=[txt '}']; txt=[txt '}'];
if(e==len) sep=''; end end
if(dim(1)>1) txt=[txt ']']; end
end end
if(len>1) txt=[txt ']']; end if(len>1) txt=[txt ']']; end
...@@ -197,10 +208,6 @@ if(~ischar(item)) ...@@ -197,10 +208,6 @@ if(~ischar(item))
end end
item=reshape(item, max(size(item),[1 0])); item=reshape(item, max(size(item),[1 0]));
len=size(item,1); len=size(item,1);
sep='';
padding1='';
padding0='';
if(~isempty(name)) if(~isempty(name))
if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end
...@@ -217,8 +224,6 @@ for e=1:len ...@@ -217,8 +224,6 @@ for e=1:len
else else
txt=[txt,'',['',S_(val),'']]; txt=[txt,'',['',S_(val),'']];
end end
if(e==len) sep=''; end
txt=[txt sep];
end end
if(len>1) txt=[txt ']']; end if(len>1) txt=[txt ']']; end
...@@ -228,20 +233,17 @@ if(~isnumeric(item) && ~islogical(item)) ...@@ -228,20 +233,17 @@ if(~isnumeric(item) && ~islogical(item))
error('input is not an array'); error('input is not an array');
end end
padding1='';
padding0='';
if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... if(length(size(item))>2 || issparse(item) || ~isreal(item) || ...
isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))
cid=I_(uint32(max(size(item)))); cid=I_(uint32(max(size(item))));
if(isempty(name)) if(isempty(name))
txt=['{' S_('_ArrayType_'),S_(class(item)),padding0,S_('_ArraySize_'),I_a(size(item),cid(1)) ]; txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];
else else
if(isempty(item)) if(isempty(item))
txt=[S_(checkname(name,varargin{:})),'Z']; txt=[S_(checkname(name,varargin{:})),'Z'];
return; return;
else else
txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),padding0,S_('_ArraySize_'),I_a(size(item),cid(1))]; txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];
end end
end end
else else
...@@ -257,8 +259,6 @@ else ...@@ -257,8 +259,6 @@ else
end end
return; return;
end end
dataformat='%s%s%s%s%s';
if(issparse(item)) if(issparse(item))
[ix,iy]=find(item); [ix,iy]=find(item);
data=full(item(find(item))); data=full(item(find(item)));
...@@ -307,7 +307,7 @@ if(size(mat,1)==1) ...@@ -307,7 +307,7 @@ if(size(mat,1)==1)
level=level-1; level=level-1;
end end
type=''; type='';
hasnegtive=find(mat<0); hasnegtive=(mat<0);
if(isa(mat,'integer') || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isa(mat,'integer') || (isfloat(mat) && all(mod(mat(:),1) == 0)))
if(isempty(hasnegtive)) if(isempty(hasnegtive))
if(max(mat(:))<=2^8) if(max(mat(:))<=2^8)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment