savejson.m
json=savejson(rootname,obj,opt)
convert a MATLAB object (cell, struct or array) into a JSON (JavaScript
Object Notation) string
authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
date: 2011/09/09
input:
rootname: name of the root-object, if set to '', will use variable name
obj: a MATLAB object (array, cell, cell array, struct, struct array)
opt: a struct for additional options, use [] if all use default
opt can have the following fields (first in [.|.] is the default)
opt.FloatFormat ['%.10g'|string]: format to show each numeric element
of a 1D/2D array;
opt.ArrayIndent [1|0]: if 1, output explicit data array with
precedent indentation; if 0, no indentation
opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D
array in JSON array format; if sets to 1, an
array will be shown as a struct with fields
"_ArrayType", "_ArraySize" and "_ArrayData"; for
sparse arrays, the non-zero elements will be
saved to _ArrayData field in triplet-format i.e.
(ix,iy,val) and "_ArrayIsSparse" will be added
with a value of 1.
output:
json: a string in the JSON format (see http://json.org)
examples:
a=struct('node',[1 9 10; 2 1 1.2], 'elem',[9 1;1 2;2 3],...
'face',[9 01 2; 1 2 3; NaN,Inf,-Inf], 'author','FangQ');
savejson('mesh',a)
savejson('',a,struct('ArrayIndent',0,'FloatFormat','\t%.5g'))
license:
BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
-- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)