forked from gllmflndn/JSONio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_jsonread.m
169 lines (136 loc) · 4.91 KB
/
test_jsonread.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function tests = test_jsonread
% Unit Tests for jsonread
% $Id: test_jsonread.m 7014 2017-02-13 12:31:33Z guillaume $
tests = functiontests(localfunctions);
function test_jsonread_from_string_1(testCase)
json = '["one", "two", "three"]';
exp = {'one';'two';'three'};
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
function test_jsonread_from_string_2(testCase)
json = '{"Width":800,"Height":600,"Title":"View from the 15th Floor","Animated":false,"IDs":[116,943,234,38793]}';
exp = struct('Width',800,'Height',600,'Title','View from the 15th Floor','Animated',false,'IDs',[116;943;234;38793]);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
function test_jsonread_all_types(testCase)
% JSON Data Type | MATLAB Data Type
% null, in numeric arrays | NaN
json = '[1, 2, null, 4]';
exp = [1; 2; NaN; 4];
act = jsonread(json);
%testCase.verifyTrue(isequaln(exp, act));
% null, in nonnumeric arrays| empty double []
json = '{"null": null}';
exp = struct('null',[]);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Boolean | scalar logical
json = '{"logical": false}';
exp = struct('logical',false);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '{"logical": true}';
exp = struct('logical',true);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Number | scalar double
json = '{"number": 3.14}';
exp = struct('number',3.14);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% String | character vector
json = '{"string": "string"}';
exp = struct('string','string');
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Object (In JSON, object | scalar structure
% means an unordered set | (Names are made
% of name-value pairs.) | valid.)
json = '{"object": {"field1": 1, "field-2": 2, "3field": 3}}';
exp = struct('object',struct('field1',1,'field_2',2,'x3field',3));
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '{"object": {"field 1": 1, "field two": 2, " field Three ": 3}}';
exp = struct('object',struct('field1',1,'fieldTwo',2,'fieldThree',3));
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '{"object": {"": 1}}';
exp = struct('object',struct('x',1));
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array, when elements are | cell array
% of different data types |
json = '{"array": ["a", 1]}';
exp = struct('array',{{'a';1}});
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array of booleans | logical array
json = '{"logical_array": [true, false]}';
exp = struct('logical_array',[true;false]);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array of numbers | double array
json = '{"number_array": [1, 2, 3, 5, 8, 13]}';
exp = struct('number_array',[1; 2; 3; 5; 8; 13]);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array of strings | cellstr
json = '{"cellstr": ["Statistical","Parametric","Mapping"]}';
exp = struct('cellstr',{{'Statistical';'Parametric';'Mapping'}});
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array of objects, when | structure array
% all objects have the |
% same set of names |
json = '{"structarray": [{"a":1,"b":2},{"a":3,"b":4}]}';
exp = struct('structarray',struct('a',{1;3},'b',{2;4}));
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% Array of objects, when | cell array of
% objects have different | scalar structures
% names |
json = '{"cellarray": [{"a":1,"b":2},{"a":3,"c":4}]}';
exp = struct('cellarray',{{struct('a',1,'b',2);struct('a',3,'c',4)}});
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% empty struct
json = '{"a":"aa","b":{},"c":"cc"}';
exp = struct('a','aa','b',struct(),'c','cc');
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% empty array
json = '{"a":"aa","b":[],"c":"cc"}';
exp = struct('a','aa','b',[],'c','cc');
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
% NaN and Inf
json = '[1,NaN,Inf]';
exp = [1;NaN;Inf];
act = jsonread(json);
testCase.verifyTrue(isequaln(exp, act));
% numbers and booleans
json = '[1,NaN,Inf,true]';
exp = {1;NaN;Inf;true};
act = jsonread(json);
testCase.verifyTrue(isequaln(exp, act));
% numbers and arrays of numbers
json = '[1,2,[3,4]]';
exp = {1;2;[3;4]};
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '[[1,2]]';
exp = [1 2];
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '[[1,2],[3,4]]';
exp = [1 2;3 4];
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '[[1,2],[3,4,5]]';
exp = {[1;2];[3;4;5]};
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));
json = '[[[1,2],[3,4]],[[5,6],[7,8]]]';
exp = cat(3,[1,3;5,7],[2,4;6,8]);
act = jsonread(json);
testCase.verifyTrue(isequal(exp, act));