savejson.m
json=savejson(obj)
or
json=savejson(rootname,obj,filename)
json=savejson(rootname,obj,opt)
json=savejson(rootname,obj,'param1',value1,'param2',value2,...)
convert a MATLAB object (cell, struct or array) into a JSON (JavaScript
Object Notation) string
author: Qianqian Fang (q.fang <at> neu.edu)
initially created on 2011/09/09
input:
rootname: the name of the root-object, when set to '', the root name
is ignored, however, when opt.ForceRootName is set to 1 (see below),
the MATLAB variable name will be used as the root name.
obj: a MATLAB object (array, cell, cell array, struct, struct array,
class instance).
filename: a string for the file name to save the output JSON data.
opt: a struct for additional options, ignore to use default values.
opt can have the following fields (first in [.|.] is the default)
FileName [''|string]: a file name to save the output JSON data
FloatFormat ['%.10g'|string]: format to show each numeric element
of a 1D/2D array;
IntFormat ['%d'|string]: format to display integer elements
of a 1D/2D array;
ArrayIndent [1|0]: if 1, output explicit data array with
precedent indentation; if 0, no indentation
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; for a complex array, the
_ArrayData_ array will include two columns
(4 for sparse) to record the real and imaginary
parts, and also "_ArrayIsComplex_":1 is added.
NestArray [0|1]: If set to 1, use nested array constructs
to store N-dimensional arrays; if set to 0,
use the annotated array format defined in the
JData Specification (Draft 1 or later).
ParseLogical [0|1]: if this is set to 1, logical array elem
will use true/false rather than 1/0.
SingletArray [0|1]: if this is set to 1, arrays with a single
numerical element will be shown without a square
bracket, unless it is the root object; if 0, square
brackets are forced for any numerical arrays.
SingletCell [1|0]: if 1, always enclose a cell with "[]"
even it has only one element; if 0, brackets
are ignored when a cell has only 1 element.
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.
Inf ['"$1_Inf_"'|string]: a customized regular expression pattern
to represent +/-Inf. The matched pattern is '([-+]*)Inf'
and $1 represents the sign. For those who want to use
1e999 to represent Inf, they can set opt.Inf to '$11e999'
NaN ['"_NaN_"'|string]: a customized regular expression pattern
to represent NaN
JSONP [''|string]: to generate a JSONP output (JSON with padding),
for example, if opt.JSONP='foo', the JSON data is
wrapped inside a function call as 'foo(...);'
UnpackHex [1|0]: conver the 0x[hex code] output by loadjson
back to the string form
SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.
Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)
Compression 'zlib', 'gzip', 'lzma', 'lzip', 'lz4' or 'lz4hc': specify array
compression method; currently only supports 6 methods. The
data compression only applicable to numerical arrays
in 3D or higher dimensions, or when ArrayToStruct
is 1 for 1D or 2D arrays. If one wants to
compress a long string, one must convert
it to uint8 or int8 array first. The compressed
array uses three extra fields
"_ArrayZipType_": the opt.Compression value.
"_ArrayZipSize_": a 1D interger array to
store the pre-compressed (but post-processed)
array dimensions, and
"_ArrayZipData_": the "base64" encoded
compressed binary array data.
CompressArraySize [100|int]: only to compress an array if the total
element count is larger than this number.
FormatVersion [2|float]: set the JSONLab output version; since
v2.0, JSONLab uses JData specification Draft 1
for output format, it is incompatible with all
previous releases; if old output is desired,
please set FormatVersion to 1.9 or earlier.
Encoding ['']: json file encoding. Support all encodings of
fopen() function
PreEncode [1|0]: if set to 1, call jdataencode first to preprocess
the input data before saving
opt can be replaced by a list of ('param',value) pairs. The param
string is equivallent to a field in opt and is case sensitive.
output:
json: a string in the JSON format (see http://json.org)
examples:
jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],...
'MeshElem',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...
'MeshSurf',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...
2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...
'MeshCreator','FangQ','MeshTitle','T6 Cube',...
'SpecialData',[nan, inf, -inf]);
savejson('jmesh',jsonmesh)
savejson('',jsonmesh,'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)