json=savejson(rootname,obj,filename) or 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 (fangq<at> nmr.mgh.harvard.edu) created on 2011/09/09
$Id: savejson.m 439 2014-09-17 05:31:08Z fangq $
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) filename: a string for the file name to save the output JSON data opt: a struct for additional options, use [] if all use default opt can have the following fields (first in [.|.] is the default)
opt.FileName [''|string]: a file name to save the output JSON data 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; 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. opt.ParseLogical [0|1]: if this is set to 1, logical array elem will use true/false rather than 1/0. opt.NoRowBracket [1|0]: 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. 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.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' opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern to represent NaN opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), for example, if opt.JSON='foo', the JSON data is wrapped inside a function call as 'foo(...);' opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson back to the string form opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. opt can be replaced by a list of ('param',value) pairs. The param string is equivallent to a field in opt. 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,'ArrayIndent',0,'FloatFormat','\t%.5g')