diff --git a/jdatadecode.m b/jdatadecode.m index 3526b61..fe49c93 100644 --- a/jdatadecode.m +++ b/jdatadecode.m @@ -80,6 +80,7 @@ end end + %% handle array data if(~isempty(strmatch(N_('_ArrayType_'),fn)) && (~isempty(strmatch(N_('_ArrayData_'),fn)) || ~isempty(strmatch(N_('_ArrayZipData_'),fn)))) newdata=cell(len,1); for j=1:len @@ -174,6 +175,38 @@ newdata=newdata{1}; end end + + %% handle table data + if(~isempty(strmatch(N_('_TableRecords_'),fn))) + newdata=cell(len,1); + for j=1:len + ndata=data(j).(N_('_TableRecords_')); + if(iscell(ndata)) + rownum=length(ndata); + colnum=length(ndata{1}); + nd=cell(rownum, colnum); + for i1=1:rownum; + for i2=1:colnum + nd{i1,i2}=ndata{i1}{i2}; + end + end + newdata{j}=cell2table(nd); + else + newdata{j}=array2table(ndata'); + end + if(isfield(data(j),N_('_TableRows_'))&& ~isempty(data(j).(N_('_TableRows_')))) + newdata{j}.Properties.RowNames=data(j).(N_('_TableRows_'))(:); + end + if(isfield(data(j),N_('_TableCols_')) && ~isempty(data(j).(N_('_TableCols_')))) + newdata{j}.Properties.VariableNames=data(j).(N_('_TableCols_')); + end + end + if(len==1) + newdata=newdata{1}; + end + end + + %% handle map data if(~isempty(strmatch(N_('_MapData_'),fn))) newdata=cell(len,1); for j=1:len diff --git a/loadjson.m b/loadjson.m index 7cfc3ff..abb0bdc 100644 --- a/loadjson.m +++ b/loadjson.m @@ -510,6 +510,7 @@ astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; + astr=regexprep(deblank(astr),'\s+,',','); [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) obj=reshape(obj,nextdim,numel(obj)/nextdim)'; diff --git a/savejson.m b/savejson.m index 5c6b43c..03c3731 100644 --- a/savejson.m +++ b/savejson.m @@ -621,27 +621,17 @@ %%------------------------------------------------------------------------- function txt=matlabtable2json(name,item,level,varargin) -if numel(item) == 0 %empty object - st = struct(); +st=containers.Map(); +st('_TableRecords_')=table2cell(item); +st('_TableRows_')=item.Properties.RowNames'; +st('_TableCols_')=item.Properties.VariableNames; +if(isempty(name)) + txt=map2json(name,st,level,varargin{:}); else - st = struct(); - propertynames = item.Properties.VariableNames; - if(isfield(item.Properties,'RowNames') && ~isempty(item.Properties.RowNames)) - rownames=item.Properties.RowNames; - for p = 1:(numel(propertynames)-1) - for j = 1:size(item(:,p),1) - st.(rownames{j}).(propertynames{p}) = item{j,p}; - end - end - else - for p = 1:numel(propertynames) - for j = 1:size(item(:,p),1) - st(j).(propertynames{p}) = item{j,p}; - end - end - end + temp=struct(name,struct()); + temp.(name)=st; + txt=map2json(name,temp.(name),level,varargin{:}); end -txt=struct2json(name,st,level,varargin{:}); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) diff --git a/saveubjson.m b/saveubjson.m index fad3d1f..270429c 100644 --- a/saveubjson.m +++ b/saveubjson.m @@ -578,27 +578,17 @@ %%------------------------------------------------------------------------- function txt=matlabtable2ubjson(name,item,level,varargin) -if numel(item) == 0 %empty object - st = struct(); +st=containers.Map(); +st('_TableRecords_')=table2cell(item); +st('_TableRows_')=item.Properties.RowNames'; +st('_TableCols_')=item.Properties.VariableNames; +if(isempty(name)) + txt=map2ubjson(name,st,level,varargin{:}); else - st = struct(); - propertynames = item.Properties.VariableNames; - if(isfield(item.Properties,'RowNames') && ~isempty(item.Properties.RowNames)) - rownames=item.Properties.RowNames; - for p = 1:(numel(propertynames)-1) - for j = 1:size(item(:,p),1) - st.(rownames{j}).(propertynames{p}) = item{j,p}; - end - end - else - for p = 1:numel(propertynames) - for j = 1:size(item(:,p),1) - st(j).(propertynames{p}) = item{j,p}; - end - end - end + temp=struct(name,struct()); + temp.(name)=st; + txt=map2ubjson(name,temp.(name),level,varargin{:}); end -txt=struct2ubjson(name,st,level,varargin{:}); %%------------------------------------------------------------------------- function txt=matlabobject2ubjson(name,item,level,varargin)