Path: blob/master/Week 9/Programming Assignment - 8/ex8/lib/jsonlab/README.txt
626 views
===============================================================================1= JSONLab =2= An open-source MATLAB/Octave JSON encoder and decoder =3===============================================================================45*Copyright (C) 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>6*License: BSD License, see License_BSD.txt for details7*Version: 1.0 (Optimus - Final)89-------------------------------------------------------------------------------1011Table of Content:1213I. Introduction14II. Installation15III.Using JSONLab16IV. Known Issues and TODOs17V. Contribution and feedback1819-------------------------------------------------------------------------------2021I. Introduction2223JSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable,24human-readable and "[http://en.wikipedia.org/wiki/JSON fat-free]" text format25to represent complex and hierarchical data. It is as powerful as26[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely27used for data-exchange in applications, and is essential for the wild success28of [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and29[http://en.wikipedia.org/wiki/Web_2.0 Web2.0].3031UBJSON (Universal Binary JSON) is a binary JSON format, specifically32optimized for compact file size and better performance while keeping33the semantics as simple as the text-based JSON format. Using the UBJSON34format allows to wrap complex binary data in a flexible and extensible35structure, making it possible to process complex and large dataset36without accuracy loss due to text conversions.3738We envision that both JSON and its binary version will serve as part of39the mainstream data-exchange formats for scientific research in the future.40It will provide the flexibility and generality achieved by other popular41general-purpose file specifications, such as42[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly43reduced complexity and enhanced performance.4445JSONLab is a free and open-source implementation of a JSON/UBJSON encoder46and a decoder in the native MATLAB language. It can be used to convert a MATLAB47data structure (array, struct, cell, struct array and cell array) into48JSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB49data structure. JSONLab supports both MATLAB and50[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).5152-------------------------------------------------------------------------------5354II. Installation5556The installation of JSONLab is no different than any other simple57MATLAB toolbox. You only need to download/unzip the JSONLab package58to a folder, and add the folder's path to MATLAB/Octave's path list59by using the following command:6061addpath('/path/to/jsonlab');6263If you want to add this path permanently, you need to type "pathtool",64browse to the jsonlab root folder and add to the list, then click "Save".65Then, run "rehash" in MATLAB, and type "which loadjson", if you see an66output, that means JSONLab is installed for MATLAB/Octave.6768-------------------------------------------------------------------------------6970III.Using JSONLab7172JSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder,73and savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and74two equivallent functions -- loadubjson and saveubjson for the binary75JSON. The detailed help info for the four functions can be found below:7677=== loadjson.m ===78<pre>79data=loadjson(fname,opt)80or81data=loadjson(fname,'param1',value1,'param2',value2,...)8283parse a JSON (JavaScript Object Notation) file or string8485authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)86created on 2011/09/09, including previous works from8788Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/2571389created on 2009/11/0290Fran�ois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/2339391created on 2009/03/2292Joel Feenstra:93http://www.mathworks.com/matlabcentral/fileexchange/2056594created on 2008/07/039596$Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $9798input:99fname: input file name, if fname contains "{}" or "[]", fname100will be interpreted as a JSON string101opt: a struct to store parsing options, opt can be replaced by102a list of ('param',value) pairs - the param string is equivallent103to a field in opt. opt can have the following104fields (first in [.|.] is the default)105106opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat107for each element of the JSON data, and group108arrays based on the cell2mat rules.109opt.FastArrayParser [1|0 or integer]: if set to 1, use a110speed-optimized array parser when loading an111array object. The fast array parser may112collapse block arrays into a single large113array similar to rules defined in cell2mat; 0 to114use a legacy parser; if set to a larger-than-1115value, this option will specify the minimum116dimension to enable the fast array parser. For117example, if the input is a 3D array, setting118FastArrayParser to 1 will return a 3D array;119setting to 2 will return a cell array of 2D120arrays; setting to 3 will return to a 2D cell121array of 1D vectors; setting to 4 will return a1223D cell array.123opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.124125output:126dat: a cell array, where {...} blocks are converted into cell arrays,127and [...] are converted to arrays128129examples:130dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}')131dat=loadjson(['examples' filesep 'example1.json'])132dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)133</pre>134135=== savejson.m ===136137<pre>138json=savejson(rootname,obj,filename)139or140json=savejson(rootname,obj,opt)141json=savejson(rootname,obj,'param1',value1,'param2',value2,...)142143convert a MATLAB object (cell, struct or array) into a JSON (JavaScript144Object Notation) string145146author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)147created on 2011/09/09148149$Id: savejson.m 458 2014-12-19 22:17:17Z fangq $150151input:152rootname: the name of the root-object, when set to '', the root name153is ignored, however, when opt.ForceRootName is set to 1 (see below),154the MATLAB variable name will be used as the root name.155obj: a MATLAB object (array, cell, cell array, struct, struct array).156filename: a string for the file name to save the output JSON data.157opt: a struct for additional options, ignore to use default values.158opt can have the following fields (first in [.|.] is the default)159160opt.FileName [''|string]: a file name to save the output JSON data161opt.FloatFormat ['%.10g'|string]: format to show each numeric element162of a 1D/2D array;163opt.ArrayIndent [1|0]: if 1, output explicit data array with164precedent indentation; if 0, no indentation165opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D166array in JSON array format; if sets to 1, an167array will be shown as a struct with fields168"_ArrayType_", "_ArraySize_" and "_ArrayData_"; for169sparse arrays, the non-zero elements will be170saved to _ArrayData_ field in triplet-format i.e.171(ix,iy,val) and "_ArrayIsSparse_" will be added172with a value of 1; for a complex array, the173_ArrayData_ array will include two columns174(4 for sparse) to record the real and imaginary175parts, and also "_ArrayIsComplex_":1 is added.176opt.ParseLogical [0|1]: if this is set to 1, logical array elem177will use true/false rather than 1/0.178opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single179numerical element will be shown without a square180bracket, unless it is the root object; if 0, square181brackets are forced for any numerical arrays.182opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson183will use the name of the passed obj variable as the184root object name; if obj is an expression and185does not have a name, 'root' will be used; if this186is set to 0 and rootname is empty, the root level187will be merged down to the lower level.188opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern189to represent +/-Inf. The matched pattern is '([-+]*)Inf'190and $1 represents the sign. For those who want to use1911e999 to represent Inf, they can set opt.Inf to '$11e999'192opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern193to represent NaN194opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),195for example, if opt.JSONP='foo', the JSON data is196wrapped inside a function call as 'foo(...);'197opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson198back to the string form199opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.200opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)201202opt can be replaced by a list of ('param',value) pairs. The param203string is equivallent to a field in opt and is case sensitive.204output:205json: a string in the JSON format (see http://json.org)206207examples:208jsonmesh=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],...209'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...210'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...2112 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...212'MeshCreator','FangQ','MeshTitle','T6 Cube',...213'SpecialData',[nan, inf, -inf]);214savejson('jmesh',jsonmesh)215savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g')216</pre>217218=== loadubjson.m ===219220<pre>221data=loadubjson(fname,opt)222or223data=loadubjson(fname,'param1',value1,'param2',value2,...)224225parse a JSON (JavaScript Object Notation) file or string226227authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)228created on 2013/08/01229230$Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $231232input:233fname: input file name, if fname contains "{}" or "[]", fname234will be interpreted as a UBJSON string235opt: a struct to store parsing options, opt can be replaced by236a list of ('param',value) pairs - the param string is equivallent237to a field in opt. opt can have the following238fields (first in [.|.] is the default)239240opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat241for each element of the JSON data, and group242arrays based on the cell2mat rules.243opt.IntEndian [B|L]: specify the endianness of the integer fields244in the UBJSON input data. B - Big-Endian format for245integers (as required in the UBJSON specification);246L - input integer fields are in Little-Endian order.247248output:249dat: a cell array, where {...} blocks are converted into cell arrays,250and [...] are converted to arrays251252examples:253obj=struct('string','value','array',[1 2 3]);254ubjdata=saveubjson('obj',obj);255dat=loadubjson(ubjdata)256dat=loadubjson(['examples' filesep 'example1.ubj'])257dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)258</pre>259260=== saveubjson.m ===261262<pre>263json=saveubjson(rootname,obj,filename)264or265json=saveubjson(rootname,obj,opt)266json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)267268convert a MATLAB object (cell, struct or array) into a Universal269Binary JSON (UBJSON) binary string270271author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)272created on 2013/08/17273274$Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $275276input:277rootname: the name of the root-object, when set to '', the root name278is ignored, however, when opt.ForceRootName is set to 1 (see below),279the MATLAB variable name will be used as the root name.280obj: a MATLAB object (array, cell, cell array, struct, struct array)281filename: a string for the file name to save the output UBJSON data282opt: a struct for additional options, ignore to use default values.283opt can have the following fields (first in [.|.] is the default)284285opt.FileName [''|string]: a file name to save the output JSON data286opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D287array in JSON array format; if sets to 1, an288array will be shown as a struct with fields289"_ArrayType_", "_ArraySize_" and "_ArrayData_"; for290sparse arrays, the non-zero elements will be291saved to _ArrayData_ field in triplet-format i.e.292(ix,iy,val) and "_ArrayIsSparse_" will be added293with a value of 1; for a complex array, the294_ArrayData_ array will include two columns295(4 for sparse) to record the real and imaginary296parts, and also "_ArrayIsComplex_":1 is added.297opt.ParseLogical [1|0]: if this is set to 1, logical array elem298will use true/false rather than 1/0.299opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single300numerical element will be shown without a square301bracket, unless it is the root object; if 0, square302brackets are forced for any numerical arrays.303opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson304will use the name of the passed obj variable as the305root object name; if obj is an expression and306does not have a name, 'root' will be used; if this307is set to 0 and rootname is empty, the root level308will be merged down to the lower level.309opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),310for example, if opt.JSON='foo', the JSON data is311wrapped inside a function call as 'foo(...);'312opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson313back to the string form314315opt can be replaced by a list of ('param',value) pairs. The param316string is equivallent to a field in opt and is case sensitive.317output:318json: a binary string in the UBJSON format (see http://ubjson.org)319320examples:321jsonmesh=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],...322'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...323'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...3242 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...325'MeshCreator','FangQ','MeshTitle','T6 Cube',...326'SpecialData',[nan, inf, -inf]);327saveubjson('jsonmesh',jsonmesh)328saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')329</pre>330331332=== examples ===333334Under the "examples" folder, you can find several scripts to demonstrate the335basic utilities of JSONLab. Running the "demo_jsonlab_basic.m" script, you336will see the conversions from MATLAB data structure to JSON text and backward.337In "jsonlab_selftest.m", we load complex JSON files downloaded from the Internet338and validate the loadjson/savejson functions for regression testing purposes.339Similarly, a "demo_ubjson_basic.m" script is provided to test the saveubjson340and loadubjson pairs for various matlab data structures.341342Please run these examples and understand how JSONLab works before you use343it to process your data.344345-------------------------------------------------------------------------------346347IV. Known Issues and TODOs348349JSONLab has several known limitations. We are striving to make it more general350and robust. Hopefully in a few future releases, the limitations become less.351352Here are the known issues:353354# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;355# When processing names containing multi-byte characters, Octave and MATLAB \356can give different field-names; you can use feature('DefaultCharacterSet','latin1') \357in MATLAB to get consistant results358# savejson can not handle class and dataset.359# saveubjson converts a logical array into a uint8 ([U]) array360# an unofficial N-D array count syntax is implemented in saveubjson. We are \361actively communicating with the UBJSON spec maintainer to investigate the \362possibility of making it upstream363# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \364files, however, it can parse all UBJSON files produced by saveubjson.365366-------------------------------------------------------------------------------367368V. Contribution and feedback369370JSONLab is an open-source project. This means you can not only use it and modify371it as you wish, but also you can contribute your changes back to JSONLab so372that everyone else can enjoy the improvement. For anyone who want to contribute,373please download JSONLab source code from it's subversion repository by using the374following command:375376svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab377378You can make changes to the files as needed. Once you are satisfied with your379changes, and ready to share it with others, please cd the root directory of380JSONLab, and type381382svn diff > yourname_featurename.patch383384You then email the .patch file to JSONLab's maintainer, Qianqian Fang, at385the email address shown in the beginning of this file. Qianqian will review386the changes and commit it to the subversion if they are satisfactory.387388We appreciate any suggestions and feedbacks from you. Please use iso2mesh's389mailing list to report any questions you may have with JSONLab:390391http://groups.google.com/group/iso2mesh-users?hl=en&pli=1392393(Subscription to the mailing list is needed in order to post messages).394395396