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