Skip to content
Snippets Groups Projects
Commit ab8f3e3b authored by fangq's avatar fangq
Browse files

patch to handle root-less objects, contributed by Blake Johnson

git-svn-id: http://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab@341 786e58fb-9377-0410-9ff7-e4ac0ac0635c
parent 5b82753c
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,12 @@ function json=savejson(rootname,obj,varargin) ...@@ -38,6 +38,12 @@ function json=savejson(rootname,obj,varargin)
% numerical element will be shown without a square % numerical element will be shown without a square
% bracket, unless it is the root object; if 0, square % bracket, unless it is the root object; if 0, square
% brackets are forced for any numerical arrays. % brackets are forced for any numerical arrays.
% opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson
% will use the name of the passed obj variable as the
% root object name; if obj is an expression and
% does not have a name, 'root' will be used; if this
% is set to 0 and rootname is empty, the root level
% will be merged down to the lower level.
% opt can be replaced by a list of ('param',value) pairs. The param % opt can be replaced by a list of ('param',value) pairs. The param
% string is equivallent to a field in opt. % string is equivallent to a field in opt.
% output: % output:
...@@ -56,18 +62,22 @@ function json=savejson(rootname,obj,varargin) ...@@ -56,18 +62,22 @@ function json=savejson(rootname,obj,varargin)
% %
varname=inputname(2); varname=inputname(2);
if(~isempty(rootname)) opt=varargin2struct(varargin{:});
varname=rootname;
end
rootisarray=0; rootisarray=0;
rootlevel=1; rootlevel=1;
if((isnumeric(obj) || islogical(obj) || ischar(obj)) && isempty(rootname)) forceroot=jsonopt('ForceRootName',0,opt);
if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)
rootisarray=1; rootisarray=1;
rootlevel=0; rootlevel=0;
varname=''; else
if(isempty(rootname))
rootname=varname;
end end
opt=varargin2struct(varargin{:}); end
json=obj2json(varname,obj,rootlevel,opt); if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)
rootname='root';
end
json=obj2json(rootname,obj,rootlevel,opt);
if(rootisarray) if(rootisarray)
json=sprintf('%s\n',json); json=sprintf('%s\n',json);
else else
...@@ -77,8 +87,6 @@ end ...@@ -77,8 +87,6 @@ end
%%------------------------------------------------------------------------- %%-------------------------------------------------------------------------
function txt=obj2json(name,item,level,varargin) function txt=obj2json(name,item,level,varargin)
cname=class(item);
if(iscell(item)) if(iscell(item))
txt=cell2json(name,item,level,varargin{:}); txt=cell2json(name,item,level,varargin{:});
elseif(isstruct(item)) elseif(isstruct(item))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment