README.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ===============================================================================
  2. = JSONLab =
  3. = An open-source MATLAB/Octave JSON encoder and decoder =
  4. ===============================================================================
  5. *Copyright (C) 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>
  6. *License: BSD License, see License_BSD.txt for details
  7. *Version: 1.0 (Optimus - Final)
  8. -------------------------------------------------------------------------------
  9. Table of Content:
  10. I. Introduction
  11. II. Installation
  12. III.Using JSONLab
  13. IV. Known Issues and TODOs
  14. V. Contribution and feedback
  15. -------------------------------------------------------------------------------
  16. I. Introduction
  17. JSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable,
  18. human-readable and "[http://en.wikipedia.org/wiki/JSON fat-free]" text format
  19. to represent complex and hierarchical data. It is as powerful as
  20. [http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely
  21. used for data-exchange in applications, and is essential for the wild success
  22. of [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and
  23. [http://en.wikipedia.org/wiki/Web_2.0 Web2.0].
  24. UBJSON (Universal Binary JSON) is a binary JSON format, specifically
  25. optimized for compact file size and better performance while keeping
  26. the semantics as simple as the text-based JSON format. Using the UBJSON
  27. format allows to wrap complex binary data in a flexible and extensible
  28. structure, making it possible to process complex and large dataset
  29. without accuracy loss due to text conversions.
  30. We envision that both JSON and its binary version will serve as part of
  31. the mainstream data-exchange formats for scientific research in the future.
  32. It will provide the flexibility and generality achieved by other popular
  33. general-purpose file specifications, such as
  34. [http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly
  35. reduced complexity and enhanced performance.
  36. JSONLab is a free and open-source implementation of a JSON/UBJSON encoder
  37. and a decoder in the native MATLAB language. It can be used to convert a MATLAB
  38. data structure (array, struct, cell, struct array and cell array) into
  39. JSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB
  40. data structure. JSONLab supports both MATLAB and
  41. [http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).
  42. -------------------------------------------------------------------------------
  43. II. Installation
  44. The installation of JSONLab is no different than any other simple
  45. MATLAB toolbox. You only need to download/unzip the JSONLab package
  46. to a folder, and add the folder's path to MATLAB/Octave's path list
  47. by using the following command:
  48. addpath('/path/to/jsonlab');
  49. If you want to add this path permanently, you need to type "pathtool",
  50. browse to the jsonlab root folder and add to the list, then click "Save".
  51. Then, run "rehash" in MATLAB, and type "which loadjson", if you see an
  52. output, that means JSONLab is installed for MATLAB/Octave.
  53. -------------------------------------------------------------------------------
  54. III.Using JSONLab
  55. JSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder,
  56. and savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and
  57. two equivallent functions -- loadubjson and saveubjson for the binary
  58. JSON. The detailed help info for the four functions can be found below:
  59. === loadjson.m ===
  60. <pre>
  61. data=loadjson(fname,opt)
  62. or
  63. data=loadjson(fname,'param1',value1,'param2',value2,...)
  64. parse a JSON (JavaScript Object Notation) file or string
  65. authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  66. created on 2011/09/09, including previous works from
  67. Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713
  68. created on 2009/11/02
  69. François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393
  70. created on 2009/03/22
  71. Joel Feenstra:
  72. http://www.mathworks.com/matlabcentral/fileexchange/20565
  73. created on 2008/07/03
  74. $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $
  75. input:
  76. fname: input file name, if fname contains "{}" or "[]", fname
  77. will be interpreted as a JSON string
  78. opt: a struct to store parsing options, opt can be replaced by
  79. a list of ('param',value) pairs - the param string is equivallent
  80. to a field in opt. opt can have the following
  81. fields (first in [.|.] is the default)
  82. opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat
  83. for each element of the JSON data, and group
  84. arrays based on the cell2mat rules.
  85. opt.FastArrayParser [1|0 or integer]: if set to 1, use a
  86. speed-optimized array parser when loading an
  87. array object. The fast array parser may
  88. collapse block arrays into a single large
  89. array similar to rules defined in cell2mat; 0 to
  90. use a legacy parser; if set to a larger-than-1
  91. value, this option will specify the minimum
  92. dimension to enable the fast array parser. For
  93. example, if the input is a 3D array, setting
  94. FastArrayParser to 1 will return a 3D array;
  95. setting to 2 will return a cell array of 2D
  96. arrays; setting to 3 will return to a 2D cell
  97. array of 1D vectors; setting to 4 will return a
  98. 3D cell array.
  99. opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.
  100. output:
  101. dat: a cell array, where {...} blocks are converted into cell arrays,
  102. and [...] are converted to arrays
  103. examples:
  104. dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}')
  105. dat=loadjson(['examples' filesep 'example1.json'])
  106. dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)
  107. </pre>
  108. === savejson.m ===
  109. <pre>
  110. json=savejson(rootname,obj,filename)
  111. or
  112. json=savejson(rootname,obj,opt)
  113. json=savejson(rootname,obj,'param1',value1,'param2',value2,...)
  114. convert a MATLAB object (cell, struct or array) into a JSON (JavaScript
  115. Object Notation) string
  116. author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  117. created on 2011/09/09
  118. $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $
  119. input:
  120. rootname: the name of the root-object, when set to '', the root name
  121. is ignored, however, when opt.ForceRootName is set to 1 (see below),
  122. the MATLAB variable name will be used as the root name.
  123. obj: a MATLAB object (array, cell, cell array, struct, struct array).
  124. filename: a string for the file name to save the output JSON data.
  125. opt: a struct for additional options, ignore to use default values.
  126. opt can have the following fields (first in [.|.] is the default)
  127. opt.FileName [''|string]: a file name to save the output JSON data
  128. opt.FloatFormat ['%.10g'|string]: format to show each numeric element
  129. of a 1D/2D array;
  130. opt.ArrayIndent [1|0]: if 1, output explicit data array with
  131. precedent indentation; if 0, no indentation
  132. opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D
  133. array in JSON array format; if sets to 1, an
  134. array will be shown as a struct with fields
  135. "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for
  136. sparse arrays, the non-zero elements will be
  137. saved to _ArrayData_ field in triplet-format i.e.
  138. (ix,iy,val) and "_ArrayIsSparse_" will be added
  139. with a value of 1; for a complex array, the
  140. _ArrayData_ array will include two columns
  141. (4 for sparse) to record the real and imaginary
  142. parts, and also "_ArrayIsComplex_":1 is added.
  143. opt.ParseLogical [0|1]: if this is set to 1, logical array elem
  144. will use true/false rather than 1/0.
  145. opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
  146. numerical element will be shown without a square
  147. bracket, unless it is the root object; if 0, square
  148. brackets are forced for any numerical arrays.
  149. opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson
  150. will use the name of the passed obj variable as the
  151. root object name; if obj is an expression and
  152. does not have a name, 'root' will be used; if this
  153. is set to 0 and rootname is empty, the root level
  154. will be merged down to the lower level.
  155. opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern
  156. to represent +/-Inf. The matched pattern is '([-+]*)Inf'
  157. and $1 represents the sign. For those who want to use
  158. 1e999 to represent Inf, they can set opt.Inf to '$11e999'
  159. opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern
  160. to represent NaN
  161. opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),
  162. for example, if opt.JSONP='foo', the JSON data is
  163. wrapped inside a function call as 'foo(...);'
  164. opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson
  165. back to the string form
  166. opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.
  167. opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)
  168. opt can be replaced by a list of ('param',value) pairs. The param
  169. string is equivallent to a field in opt and is case sensitive.
  170. output:
  171. json: a string in the JSON format (see http://json.org)
  172. examples:
  173. 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],...
  174. '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],...
  175. 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...
  176. 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...
  177. 'MeshCreator','FangQ','MeshTitle','T6 Cube',...
  178. 'SpecialData',[nan, inf, -inf]);
  179. savejson('jmesh',jsonmesh)
  180. savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g')
  181. </pre>
  182. === loadubjson.m ===
  183. <pre>
  184. data=loadubjson(fname,opt)
  185. or
  186. data=loadubjson(fname,'param1',value1,'param2',value2,...)
  187. parse a JSON (JavaScript Object Notation) file or string
  188. authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  189. created on 2013/08/01
  190. $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $
  191. input:
  192. fname: input file name, if fname contains "{}" or "[]", fname
  193. will be interpreted as a UBJSON string
  194. opt: a struct to store parsing options, opt can be replaced by
  195. a list of ('param',value) pairs - the param string is equivallent
  196. to a field in opt. opt can have the following
  197. fields (first in [.|.] is the default)
  198. opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat
  199. for each element of the JSON data, and group
  200. arrays based on the cell2mat rules.
  201. opt.IntEndian [B|L]: specify the endianness of the integer fields
  202. in the UBJSON input data. B - Big-Endian format for
  203. integers (as required in the UBJSON specification);
  204. L - input integer fields are in Little-Endian order.
  205. output:
  206. dat: a cell array, where {...} blocks are converted into cell arrays,
  207. and [...] are converted to arrays
  208. examples:
  209. obj=struct('string','value','array',[1 2 3]);
  210. ubjdata=saveubjson('obj',obj);
  211. dat=loadubjson(ubjdata)
  212. dat=loadubjson(['examples' filesep 'example1.ubj'])
  213. dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)
  214. </pre>
  215. === saveubjson.m ===
  216. <pre>
  217. json=saveubjson(rootname,obj,filename)
  218. or
  219. json=saveubjson(rootname,obj,opt)
  220. json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)
  221. convert a MATLAB object (cell, struct or array) into a Universal
  222. Binary JSON (UBJSON) binary string
  223. author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  224. created on 2013/08/17
  225. $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $
  226. input:
  227. rootname: the name of the root-object, when set to '', the root name
  228. is ignored, however, when opt.ForceRootName is set to 1 (see below),
  229. the MATLAB variable name will be used as the root name.
  230. obj: a MATLAB object (array, cell, cell array, struct, struct array)
  231. filename: a string for the file name to save the output UBJSON data
  232. opt: a struct for additional options, ignore to use default values.
  233. opt can have the following fields (first in [.|.] is the default)
  234. opt.FileName [''|string]: a file name to save the output JSON data
  235. opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D
  236. array in JSON array format; if sets to 1, an
  237. array will be shown as a struct with fields
  238. "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for
  239. sparse arrays, the non-zero elements will be
  240. saved to _ArrayData_ field in triplet-format i.e.
  241. (ix,iy,val) and "_ArrayIsSparse_" will be added
  242. with a value of 1; for a complex array, the
  243. _ArrayData_ array will include two columns
  244. (4 for sparse) to record the real and imaginary
  245. parts, and also "_ArrayIsComplex_":1 is added.
  246. opt.ParseLogical [1|0]: if this is set to 1, logical array elem
  247. will use true/false rather than 1/0.
  248. opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
  249. numerical element will be shown without a square
  250. bracket, unless it is the root object; if 0, square
  251. brackets are forced for any numerical arrays.
  252. opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson
  253. will use the name of the passed obj variable as the
  254. root object name; if obj is an expression and
  255. does not have a name, 'root' will be used; if this
  256. is set to 0 and rootname is empty, the root level
  257. will be merged down to the lower level.
  258. opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),
  259. for example, if opt.JSON='foo', the JSON data is
  260. wrapped inside a function call as 'foo(...);'
  261. opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson
  262. back to the string form
  263. opt can be replaced by a list of ('param',value) pairs. The param
  264. string is equivallent to a field in opt and is case sensitive.
  265. output:
  266. json: a binary string in the UBJSON format (see http://ubjson.org)
  267. examples:
  268. 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],...
  269. '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],...
  270. 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...
  271. 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...
  272. 'MeshCreator','FangQ','MeshTitle','T6 Cube',...
  273. 'SpecialData',[nan, inf, -inf]);
  274. saveubjson('jsonmesh',jsonmesh)
  275. saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')
  276. </pre>
  277. === examples ===
  278. Under the "examples" folder, you can find several scripts to demonstrate the
  279. basic utilities of JSONLab. Running the "demo_jsonlab_basic.m" script, you
  280. will see the conversions from MATLAB data structure to JSON text and backward.
  281. In "jsonlab_selftest.m", we load complex JSON files downloaded from the Internet
  282. and validate the loadjson/savejson functions for regression testing purposes.
  283. Similarly, a "demo_ubjson_basic.m" script is provided to test the saveubjson
  284. and loadubjson pairs for various matlab data structures.
  285. Please run these examples and understand how JSONLab works before you use
  286. it to process your data.
  287. -------------------------------------------------------------------------------
  288. IV. Known Issues and TODOs
  289. JSONLab has several known limitations. We are striving to make it more general
  290. and robust. Hopefully in a few future releases, the limitations become less.
  291. Here are the known issues:
  292. # 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;
  293. # When processing names containing multi-byte characters, Octave and MATLAB \
  294. can give different field-names; you can use feature('DefaultCharacterSet','latin1') \
  295. in MATLAB to get consistant results
  296. # savejson can not handle class and dataset.
  297. # saveubjson converts a logical array into a uint8 ([U]) array
  298. # an unofficial N-D array count syntax is implemented in saveubjson. We are \
  299. actively communicating with the UBJSON spec maintainer to investigate the \
  300. possibility of making it upstream
  301. # loadubjson can not parse all UBJSON Specification (Draft 9) compliant \
  302. files, however, it can parse all UBJSON files produced by saveubjson.
  303. -------------------------------------------------------------------------------
  304. V. Contribution and feedback
  305. JSONLab is an open-source project. This means you can not only use it and modify
  306. it as you wish, but also you can contribute your changes back to JSONLab so
  307. that everyone else can enjoy the improvement. For anyone who want to contribute,
  308. please download JSONLab source code from it's subversion repository by using the
  309. following command:
  310. svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab
  311. You can make changes to the files as needed. Once you are satisfied with your
  312. changes, and ready to share it with others, please cd the root directory of
  313. JSONLab, and type
  314. svn diff > yourname_featurename.patch
  315. You then email the .patch file to JSONLab's maintainer, Qianqian Fang, at
  316. the email address shown in the beginning of this file. Qianqian will review
  317. the changes and commit it to the subversion if they are satisfactory.
  318. We appreciate any suggestions and feedbacks from you. Please use iso2mesh's
  319. mailing list to report any questions you may have with JSONLab:
  320. http://groups.google.com/group/iso2mesh-users?hl=en&pli=1
  321. (Subscription to the mailing list is needed in order to post messages).