comparison test/json/jsonencodetest.tst @ 28615:5da49e37a6c9

New functions jsondecode and jsonencode (bug #53100). * NEWS: Add to list of new functions. * configure.ac: Look for RapidJSON library. * libinterp/corefcn/jsondecode.cc: New function. * libinterp/corefcn/jsonencode.cc: New function. * libinterp/corefcn/module.mk: Add new functions to build system. * scripts/help/__unimplemented__.m: Remove the function. * test/json/jsondecodetest.tst: New test files. * test/json/jsonencodetest.tst: New test files. * test/json/module.mk: Add new functions to build system. * test/module.mk: Add new functions to build system. This is the result of GSoC 2020 by Abdallah Elshamy.
author Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
date Thu, 13 Aug 2020 23:57:07 +0900
parents
children 174550af014f
comparison
equal deleted inserted replaced
28614:652a675c0916 28615:5da49e37a6c9
1 % test jsonencode
2
3 % Note: This script is intended to be a script-based unit test
4 % for MATLAB to test compatibility. Don't break that!
5
6 % some tests here are just the reverse of tests in jsondecode with some modifiactions
7 %% Test 1: encode logical and numeric scalars, NaN and Inf
8 %!testif HAVE_RAPIDJSON
9 %! assert (isequal (jsonencode (true), 'true'));
10 %! assert (isequal (jsonencode (50.025), '50.025'));
11 %! assert (isequal (jsonencode (NaN), 'null'));
12 %! assert (isequal (jsonencode (Inf), 'null'));
13 %! assert (isequal (jsonencode (-Inf), 'null'));
14
15 % Customized encoding of Nan, Inf, -Inf
16 %!testif HAVE_RAPIDJSON
17 %! assert (isequal (jsonencode (NaN, 'ConvertInfAndNaN', true), 'null'));
18 %! assert (isequal (jsonencode (Inf, 'ConvertInfAndNaN', true), 'null'));
19 %! assert (isequal (jsonencode (-Inf, 'ConvertInfAndNaN', true), 'null'));
20
21 %!testif HAVE_RAPIDJSON
22 %! assert (isequal (jsonencode (NaN, 'ConvertInfAndNaN', false), 'NaN'));
23 %! assert (isequal (jsonencode (Inf, 'ConvertInfAndNaN', false), 'Infinity'));
24 %! assert (isequal (jsonencode (-Inf, 'ConvertInfAndNaN', false), '-Infinity'));
25
26 %% Test 2: encode character vectors and arrays
27 %!testif HAVE_RAPIDJSON
28 %! assert (isequal (jsonencode (''), '""'));
29 %! assert (isequal (jsonencode ('hello there'), '"hello there"'));
30 %! assert (isequal (jsonencode (['foo'; 'bar']), '["foo","bar"]'));
31 %! assert (isequal (jsonencode (['foo', 'bar'; 'foo', 'bar']), '["foobar","foobar"]'));
32
33 %!testif HAVE_RAPIDJSON
34 %! data = [[['foo'; 'bar']; ['foo'; 'bar']], [['foo'; 'bar']; ['foo'; 'bar']]];
35 %! exp = '["foofoo","barbar","foofoo","barbar"]';
36 %! act = jsonencode (data);
37 %! assert (isequal (exp, act));
38
39 %!testif HAVE_RAPIDJSON
40 %! data = cat (3, ['a', 'b'; 'c', 'd'], ['e', 'f'; 'g', 'h']);
41 %! exp = '[["ab","ef"],["cd","gh"]]';
42 %! act = jsonencode (data);
43 %! assert (isequal (exp, act));
44
45 % try different dimensions for the array
46 %!testif HAVE_RAPIDJSON
47 %! data = cat (3, ['a', 'b'; 'c', 'd'; '1', '2'], ['e', 'f'; 'g', 'h'; '3', '4']);
48 %! exp = '[["ab","ef"],["cd","gh"],["12","34"]]';
49 %! act = jsonencode (data);
50 %! assert (isequal (exp, act));
51
52 % try higher dimensions for the array
53 %!testif HAVE_RAPIDJSON
54 %! tmp1 = cat (3, ['1', '3'; '5', '7'; '9', 'e'; 'f', 'g'], ...
55 %! ['2', '4'; '6', '8'; 'a', 'b'; 'c', 'd']);
56 %! tmp2 = cat (3, ['1', '3'; '5', '7'; '9', 'e'; 'f', 'g'], ...
57 %! ['2', '4'; '6', '8'; 'a', 'b'; 'c', 'd']);
58 %! data = cat (4, tmp1, tmp2);
59 %! exp = ['[[["13","13"],["24","24"]],[["57","57"],["68","68"]],', ...
60 %! '[["9e","9e"],["ab","ab"]],[["fg","fg"],["cd","cd"]]]'];
61 %! act = jsonencode (data);
62 %! assert (isequal (exp, act));
63
64 % try different dimensions for the array with one of its dimensions equals one
65 %!testif HAVE_RAPIDJSON
66 %! data = cat (4, ['a'; 'b'], ['c'; 'd']);
67 %! exp = '[[["a","c"]],[["b","d"]]]';
68 %! act = jsonencode (data);
69 %! assert (isequal (exp, act));
70
71 % try different dimensions for the array with one of its dimensions equals one
72 %!testif HAVE_RAPIDJSON
73 %! data = cat (8, ['a'], ['c']);
74 %! exp = '"ac"';
75 %! act = jsonencode (data);
76 %! assert (isequal (exp, act));
77
78 % try different dimensions for the array with one of its dimensions equals one
79 %!testif HAVE_RAPIDJSON
80 %! data = cat (8, ['a'; 'b'; '1'], ['c'; 'd'; '2']);
81 %! exp = '[[[[[[["a","c"]]]]]],[[[[[["b","d"]]]]]],[[[[[["1","2"]]]]]]]';
82 %! act = jsonencode (data);
83 %! assert (isequal (exp, act));
84
85 %% Test 3: encode numeric and logical arrays (with NaN and Inf)
86 % test simple vectors
87 %!testif HAVE_RAPIDJSON
88 %! assert (isequal (jsonencode ([]), '[]'));
89 %! assert (isequal (jsonencode ([1, 2, 3, 4]), '[1,2,3,4]'));
90 %! assert (isequal (jsonencode ([true; false; true]), '[true,false,true]'));
91
92 % test arrays
93 %!testif HAVE_RAPIDJSON
94 %! data = [1 NaN; 3 4];
95 %! exp = '[[1,null],[3,4]]';
96 %! act = jsonencode (data);
97 %! assert (isequal (exp, act));
98
99 %!testif HAVE_RAPIDJSON
100 %! data = cat (3, [NaN, 3; 5, Inf], [2, 4; -Inf, 8]);
101 %! exp = '[[[null,2],[3,4]],[[5,null],[null,8]]]';
102 %! act = jsonencode (data);
103 %! assert (isequal (exp, act));
104
105 % Customized encoding of Nan, Inf, -Inf
106 %!testif HAVE_RAPIDJSON
107 %! data = cat (3, [1, NaN; 5, 7], [2, Inf; 6, -Inf]);
108 %! exp = '[[[1,2],[NaN,Infinity]],[[5,6],[7,-Infinity]]]';
109 %! act = jsonencode (data, 'ConvertInfAndNaN', false);
110 %! assert (isequal (exp, act));
111
112 % try different dimensions for the array
113 %!testif HAVE_RAPIDJSON
114 %! data = cat (3, [1, 3; 5, 7], [2, 4; 6, 8], [-1, NaN; Inf, -Inf]);
115 %! exp = '[[[1,2,-1],[3,4,NaN]],[[5,6,Infinity],[7,8,-Infinity]]]';
116 %! act = jsonencode (data, 'ConvertInfAndNaN', false);
117 %! assert (isequal (exp, act));
118
119 % try different dimensions for the array with one of its dimensions equals one
120 %!testif HAVE_RAPIDJSON
121 %! data = cat (3, [1; 7; 11], [4; 8; 12]);
122 %! exp = '[[[1,4]],[[7,8]],[[11,12]]]';
123 %! act = jsonencode (data);
124 %! assert (isequal (exp, act));
125
126 % try higher dimensions for the array with one of its dimensions equals one
127 %!testif HAVE_RAPIDJSON
128 %! tmp1 = cat (3, [5, 7], [2, 4]);
129 %! tmp2 = cat (3, [-1, -3], [-2, -4]);
130 %! data = cat (4, tmp1, tmp2);
131 %! exp = '[[[[5,-1],[2,-2]],[[7,-3],[4,-4]]]]';
132 %! act = jsonencode (data);
133 %! assert (isequal (exp, act));
134
135 % try higher dimensions for the array with one of its dimensions equals one
136 %!testif HAVE_RAPIDJSON
137 %! tmp1 = cat (3, [5; 7], [2; 4]);
138 %! tmp2 = cat (3, [-1; -3], [-2; -4]);
139 %! data = cat (4, tmp1, tmp2);
140 %! exp = '[[[[5,-1],[2,-2]]],[[[7,-3],[4,-4]]]]';
141 %! act = jsonencode (data);
142 %! assert (isequal (exp, act));
143
144 % try higher dimensions for the array with one of its dimensions equals one
145 %!testif HAVE_RAPIDJSON
146 %! data = cat (4, [1, 3; 5, 7], [-1, -3; -5, -7]);
147 %! exp = '[[[[1,-1]],[[3,-3]]],[[[5,-5]],[[7,-7]]]]';
148 %! act = jsonencode (data);
149 %! assert (isequal (exp, act));
150
151 % try higher dimensions for the array with one of its dimensions equals one
152 %!testif HAVE_RAPIDJSON
153 %! data = ones ([1 1 1 1 1 6]);
154 %! exp = '[1,1,1,1,1,1]';
155 %! act = jsonencode (data);
156 %! assert (isequal (exp, act));
157
158 % try higher dimensions for the array with one of its dimensions equals one
159 %!testif HAVE_RAPIDJSON
160 %! data = ones ([1 2 2 2 2]);
161 %! exp = '[[[[[1,1],[1,1]],[[1,1],[1,1]]],[[[1,1],[1,1]],[[1,1],[1,1]]]]]';
162 %! act = jsonencode (data);
163 %! assert (isequal (exp, act));
164
165 % try higher dimensions for the array with some of its dimensions equal one
166 %!testif HAVE_RAPIDJSON
167 %! data = ones ([1 2 2 1 2]);
168 %! exp = '[[[[[1,1]],[[1,1]]],[[[1,1]],[[1,1]]]]]';
169 %! act = jsonencode (data);
170 %! assert (isequal (exp, act));
171
172 % try higher dimensions for the array with some of its dimensions equal one
173 %!testif HAVE_RAPIDJSON
174 %! data = ones ([1 2 1 2 1 2]);
175 %! exp = '[[[[[[1,1]],[[1,1]]]],[[[[1,1]],[[1,1]]]]]]';
176 %! act = jsonencode (data);
177 %! assert (isequal (exp, act));
178
179 % try higher dimensions for the array with some of its dimensions equal one
180 %!testif HAVE_RAPIDJSON
181 %! data = ones ([1 1 2 1 2 1 2]);
182 %! exp = '[[[[[[[1,1]],[[1,1]]]],[[[[1,1]],[[1,1]]]]]]]';
183 %! act = jsonencode (data);
184 %! assert (isequal (exp, act));
185
186 % try higher dimensions for the array with some of its dimensions equal one
187 %!testif HAVE_RAPIDJSON
188 %! data = ones ([1 2 2 1 1 2]);
189 %! exp = '[[[[[[1,1]]],[[[1,1]]]],[[[[1,1]]],[[[1,1]]]]]]';
190 %! act = jsonencode (data);
191 %! assert (isequal (exp, act));
192
193 % try higher dimensions for the array with some of its dimensions equal one
194 %!testif HAVE_RAPIDJSON
195 %! data = ones ([1 2 1 3 1 1 1 2]);
196 %! exp = ['[[[[[[[[1,1]]]],[[[[1,1]]]],[[[[1,1]]]]]],[[[[[[1,1]]]],', ...
197 %! '[[[[1,1]]]],[[[[1,1]]]]]]]]'];
198 %! act = jsonencode (data);
199 %! assert (isequal (exp, act));
200
201 % try higher dimensions for the array with some of its dimensions equal one
202 %!testif HAVE_RAPIDJSON
203 %! data = ones ([1 1 1 1 2 1 1 1 2]);
204 %! exp = '[[[[[[[[[1,1]]]],[[[[1,1]]]]]]]]]';
205 %! act = jsonencode (data);
206 %! assert (isequal (exp, act));
207
208 % try higher dimensions for the array with some of its dimensions equal one
209 %!testif HAVE_RAPIDJSON
210 %! data = ones ([1 3 2 1 1 2 1 2 2]);
211 %! exp = ['[[[[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]],[[[[[[1,1],', ...
212 %! '[1,1]]],[[[1,1],[1,1]]]]]]],[[[[[[[1,1],[1,1]]],[[[1,', ...
213 %! '1],[1,1]]]]]],[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]]],', ...
214 %! '[[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]],[[[[[[1,1],[1,', ...
215 %! '1]]],[[[1,1],[1,1]]]]]]]]]'];
216 %! act = jsonencode (data);
217 %! assert (isequal (exp, act));
218
219 % try higher dimensions for the array with some of its dimensions equal one
220 %!testif HAVE_RAPIDJSON
221 %! data = ones ([1 1 1 1 1 1 2 1 1 2 2 3 1 1 1 1 1 1 1 2]);
222 %! exp = ['[[[[[[[[[[[[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],', ...
223 %! '[[[[[[[[1,1]]]]]]]]],[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]', ...
224 %! ']]],[[[[[[[[1,1]]]]]]]]]],[[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]', ...
225 %! ']]]]]]],[[[[[[[[1,1]]]]]]]]],[[[[[[[[[1,1]]]]]]]],[[[[[[', ...
226 %! '[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]]]]]],[[[[[[[[[[[[[1,1]', ...
227 %! ']]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]],[[[[[[[[', ...
228 %! '[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]]],[[[', ...
229 %! '[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]],', ...
230 %! '[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]', ...
231 %! ']]]]]]]]]]]]]]]]]]'];
232 %! act = jsonencode (data);
233 %! assert (isequal (exp, act));
234
235 % try higher dimensions for the array
236 %!testif HAVE_RAPIDJSON
237 %! tmp1 = cat (3, [1, 3; 5, 7; 9, 11; 13, 15], [2, 4; 6, 8; 10, 12; 14, 16]);
238 %! tmp2 = cat (3, [-1, -3; -5, -7; -9, -11; -13, -15], ...
239 %! [-2, -4; -6, -8; -10, -12; -14, -16]);
240 %! data = cat (4, tmp1, tmp2);
241 %! exp = ['[[[[1,-1],[2,-2]],[[3,-3],[4,-4]]],[[[5,-5],[6,-6]],[[7,-7],', ...
242 %! '[8,-8]]],[[[9,-9],[10,-10]],[[11,-11],[12,-12]]],', ...
243 %! '[[[13,-13],[14,-14]],[[15,-15],[16,-16]]]]'];
244 %! act = jsonencode (data);
245 %! assert (isequal (exp, act));
246
247 %!testif HAVE_RAPIDJSON
248 %! data = [true false; true false; true false];
249 %! exp = '[[true,false],[true,false],[true,false]]';
250 %! act = jsonencode (data);
251 %! assert (isequal (exp, act));
252
253 %% Test 4: encode containers.Map
254
255 % KeyType must be char to encode objects of containers.Map
256 %!testif HAVE_RAPIDJSON
257 %! assert (isequal (jsonencode (containers.Map('1', [1, 2, 3])), '{"1":[1,2,3]}'));
258
259 %!testif HAVE_RAPIDJSON
260 %! data = containers.Map({'foo'; 'bar'; 'baz'}, [1, 2, 3]);
261 %! exp = '{"bar":2,"baz":3,"foo":1}';
262 %! act = jsonencode (data);
263 %! assert (isequal (exp, act));
264
265 %!testif HAVE_RAPIDJSON
266 %! data = containers.Map({'foo'; 'bar'; 'baz'}, {{1, 'hello', NaN}, true, [2, 3, 4]});
267 %! exp = '{"bar":true,"baz":[2,3,4],"foo":[1,"hello",NaN]}';
268 %! act = jsonencode (data, 'ConvertInfAndNaN', false);
269 %! assert (isequal (exp, act));
270
271 %% Test 5: encode scalar structs
272 % check the encoding of Boolean, Number and String values inside a struct
273 %!testif HAVE_RAPIDJSON
274 %! data = struct ('number', 3.14, 'string', 'string', 'boolean', false);
275 %! exp = '{"number":3.14,"string":"string","boolean":false}';
276 %! act = jsonencode (data);
277 %! assert (isequal (exp, act));
278
279 % check the decoding of null, Inf and -Inf values inside a struct
280 %!testif HAVE_RAPIDJSON
281 %! data = struct ('numericArray', [7, NaN, Inf, -Inf]);
282 %! exp = '{"numericArray":[7,null,null,null]}';
283 %! act = jsonencode (data);
284 %! assert (isequal (exp, act));
285
286 % Customized encoding of Nan, Inf, -Inf
287 %!testif HAVE_RAPIDJSON
288 %! data = struct ('numericArray', [7, NaN, Inf, -Inf]);
289 %! exp = '{"numericArray":[7,NaN,Infinity,-Infinity]}';
290 %! act = jsonencode (data, 'ConvertInfAndNaN', false);
291 %! assert (isequal (exp, act));
292
293 % check the encoding of structs inside a struct
294 %!testif HAVE_RAPIDJSON
295 %! data = struct ('object', struct ('field1', 1, 'field2', 2, 'field3', 3));
296 %! exp = '{"object":{"field1":1,"field2":2,"field3":3}}';
297 %! act = jsonencode (data);
298 %! assert (isequal (exp, act));
299
300 % check the encoding of empty structs, empty arrays and Inf inside a struct
301 %!testif HAVE_RAPIDJSON
302 %! data = struct ('a', Inf, 'b', [], 'c', struct ());
303 %! exp = '{"a":null,"b":[],"c":{}}';
304 %! act = jsonencode (data);
305 %! assert (isequal (exp, act));
306
307 % a big test
308 %!testif HAVE_RAPIDJSON
309 %! tmp1 = struct ('para', ['A meta-markup language, used to create markup languages', ...
310 %! ' such as DocBook.'],'GlossSeeAlso', {{'GML'; 'XML'}});
311 %! tmp2 = struct ('ID', 'SGML', 'SortAs', 'SGML', 'GlossTerm', ...
312 %! 'Standard Generalized Markup Language', 'Acronym', 'SGML', ...
313 %! 'Abbrev', 'ISO 8879:1986', 'GlossDef', tmp1, 'GlossSee', 'markup');
314 %! data = struct ('glossary', struct ('title', 'example glossary', 'GlossDiv', ...
315 %! struct ('title', 'S', 'GlossList', struct ('GlossEntry', tmp2))));
316 %! exp = ['{' , ...
317 %! '"glossary":{', ...
318 %! '"title":"example glossary",', ...
319 %! '"GlossDiv":{', ...
320 %! '"title":"S",', ...
321 %! '"GlossList":{', ...
322 %! '"GlossEntry":{', ...
323 %! '"ID":"SGML",', ...
324 %! '"SortAs":"SGML",', ...
325 %! '"GlossTerm":"Standard Generalized Markup Language",', ...
326 %! '"Acronym":"SGML",', ...
327 %! '"Abbrev":"ISO 8879:1986",', ...
328 %! '"GlossDef":{', ...
329 %! '"para":"A meta-markup language, ', ...
330 %! 'used to create markup languages such as DocBook.",', ...
331 %! '"GlossSeeAlso":["GML","XML"]', ...
332 %! '},', ...
333 %! '"GlossSee":"markup"', ...
334 %! '}', ...
335 %! '}', ...
336 %! '}', ...
337 %! '}', ...
338 %! '}'];
339 %! act = jsonencode (data);
340 %! assert (isequal (exp, act));
341
342 %% Test 6: encode struct arrays
343 %!testif HAVE_RAPIDJSON
344 %! data = struct ('structarray', struct ('a', {1; 3}, 'b', {2; 4}));
345 %! exp = '{"structarray":[{"a":1,"b":2},{"a":3,"b":4}]}';
346 %! act = jsonencode (data);
347 %! assert (isequal (exp, act));
348
349 % a big Test
350 %!testif HAVE_RAPIDJSON
351 %! tmp1 = struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'});
352 %! tmp2 = struct ('id', {0; 1; 2}, 'name', {'Osborn'; 'Mcdowell'; 'Jewel'});
353 %! tmp3 = struct ('id', {0; 1; 2}, 'name', {'Socorro'; 'Darla'; 'Leanne'});
354 %! data = struct ('x_id', {'5ee28980fc9ab3'; '5ee28980dd7250'; '5ee289802422ac'}, ...
355 %! 'index', {0; 1; 2}, 'guid', {'b229d1de-f94a'; '39cee338-01fb'; '3db8d55a-663e'}, ...
356 %! 'latitude', {-17.124067; 13.205994; -35.453456}, 'longitude', ...
357 %! {-61.161831; -37.276231; 14.080287}, 'friends', {tmp1; tmp2; tmp3});
358 %! exp = ['[', ...
359 %! '{', ...
360 %! '"x_id":"5ee28980fc9ab3",', ...
361 %! '"index":0,', ...
362 %! '"guid":"b229d1de-f94a",', ...
363 %! '"latitude":-17.124067,', ...
364 %! '"longitude":-61.161831,', ...
365 %! '"friends":[', ...
366 %! '{', ...
367 %! '"id":0,', ...
368 %! '"name":"Collins"', ...
369 %! '},', ...
370 %! '{', ...
371 %! '"id":1,', ...
372 %! '"name":"Hays"', ...
373 %! '},', ...
374 %! '{', ...
375 %! '"id":2,', ...
376 %! '"name":"Griffin"', ...
377 %! '}', ...
378 %! ']', ...
379 %! '},', ...
380 %! '{', ...
381 %! '"x_id":"5ee28980dd7250",', ...
382 %! '"index":1,', ...
383 %! '"guid":"39cee338-01fb",', ...
384 %! '"latitude":13.205994,', ...
385 %! '"longitude":-37.276231,', ...
386 %! '"friends":[', ...
387 %! '{', ...
388 %! '"id":0,', ...
389 %! '"name":"Osborn"', ...
390 %! '},', ...
391 %! '{', ...
392 %! '"id":1,', ...
393 %! '"name":"Mcdowell"', ...
394 %! '},', ...
395 %! '{', ...
396 %! '"id":2,', ...
397 %! '"name":"Jewel"', ...
398 %! '}', ...
399 %! ']', ...
400 %! '},', ...
401 %! '{', ...
402 %! '"x_id":"5ee289802422ac",', ...
403 %! '"index":2,', ...
404 %! '"guid":"3db8d55a-663e",', ...
405 %! '"latitude":-35.453456,', ...
406 %! '"longitude":14.080287,', ...
407 %! '"friends":[', ...
408 %! '{', ...
409 %! '"id":0,', ...
410 %! '"name":"Socorro"', ...
411 %! '},', ...
412 %! '{', ...
413 %! '"id":1,', ...
414 %! '"name":"Darla"', ...
415 %! '},', ...
416 %! '{', ...
417 %! '"id":2,', ...
418 %! '"name":"Leanne"', ...
419 %! '}', ...
420 %! ']', ...
421 %! '}', ...
422 %! ']'];
423 %! act = jsonencode (data);
424 %! assert (isequal (exp, act));
425
426 %% Test 7: encode cell arrays
427 %!testif HAVE_RAPIDJSON
428 %! assert (isequal ('[]', jsonencode ({})));
429 %! assert (isequal ('[5]', jsonencode ({5})));
430 %! assert (isequal ('["hello there"]', jsonencode ({'hello there'})));
431
432 %!testif HAVE_RAPIDJSON
433 %! data = {'true', 'true'; 'false', 'true'};
434 %! exp = '["true","false","true","true"]';
435 %! act = jsonencode (data);
436 %! assert (isequal (exp, act));
437
438 %!testif HAVE_RAPIDJSON
439 %! data = {'foo'; 'bar'; {'foo'; 'bar'}};
440 %! exp = '["foo","bar",["foo","bar"]]';
441 %! act = jsonencode (data);
442 %! assert (isequal (exp, act));
443
444 % cell array of structs & a big test
445 %!testif HAVE_RAPIDJSON
446 %! tmp1 = struct ('x_id', '5ee28980fc9ab3', 'index', 0, 'guid', 'b229d1de-f94a', ...
447 %! 'latitude', -17.124067, 'longitude', -61.161831, 'friends', ...
448 %! struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'}));
449 %! tmp2 = struct ('numericArray',{{'str'; 5; []}}, 'nonnumericArray', {[1; 2; NaN]});
450 %! tmp3 = struct ('firstName', 'John','lastName', 'Smith', 'age', 25, 'address', ...
451 %! struct('streetAddress', '21 2nd Street', 'city', 'New York', 'state', 'NY'), ...
452 %! 'phoneNumber', struct ('type', 'home', 'number', '212 555-1234'));
453 %! data = {tmp1; tmp2; tmp3};
454 %! exp = ['[', ...
455 %! '{', ...
456 %! '"x_id":"5ee28980fc9ab3",', ...
457 %! '"index":0,', ...
458 %! '"guid":"b229d1de-f94a",', ...
459 %! '"latitude":-17.124067,', ...
460 %! '"longitude":-61.161831,', ...
461 %! '"friends":[', ...
462 %! '{', ...
463 %! '"id":0,', ...
464 %! '"name":"Collins"', ...
465 %! '},', ...
466 %! '{', ...
467 %! '"id":1,', ...
468 %! '"name":"Hays"', ...
469 %! '},', ...
470 %! '{', ...
471 %! '"id":2,', ...
472 %! '"name":"Griffin"', ...
473 %! '}', ...
474 %! ']', ...
475 %! '},', ...
476 %! '{"numericArray":["str",5,[]],"nonnumericArray":[1,2,null]},', ...
477 %! '{', ...
478 %! '"firstName":"John",', ...
479 %! '"lastName":"Smith",', ...
480 %! '"age":25,', ...
481 %! '"address":', ...
482 %! '{', ...
483 %! '"streetAddress":"21 2nd Street",', ...
484 %! '"city":"New York",', ...
485 %! '"state":"NY"', ...
486 %! '},', ...
487 %! '"phoneNumber":', ...
488 %! '{', ...
489 %! '"type":"home",', ...
490 %! '"number":"212 555-1234"', ...
491 %! '}', ...
492 %! '}]'];
493 %! act = jsonencode (data);
494 %! assert (isequal (exp, act));
495
496 % cell array of diferrent types & Customized encoding of Nan, Inf, -Inf
497 %!testif HAVE_RAPIDJSON
498 %! tmp = struct ('x_id', '5ee28980dd7250', 'index', 1, 'guid', '39cee338-01fb', ...
499 %! 'latitude', 13.205994, 'longitude', -37.276231, 'friends', ...
500 %! struct ('id', {0; 1; 2}, 'name', {'Osborn'; 'Mcdowell'; 'Jewel'}));
501 %! data = {NaN; true; Inf; 2531.023; 'hello there'; tmp};
502 %! exp = ['[NaN,true,Infinity,2531.023,"hello there",', ...
503 %! '{', ...
504 %! '"x_id":"5ee28980dd7250",', ...
505 %! '"index":1,', ...
506 %! '"guid":"39cee338-01fb",', ...
507 %! '"latitude":13.205994,', ...
508 %! '"longitude":-37.276231,', ...
509 %! '"friends":[', ...
510 %! '{', ...
511 %! '"id":0,', ...
512 %! '"name":"Osborn"', ...
513 %! '},', ...
514 %! '{', ...
515 %! '"id":1,', ...
516 %! '"name":"Mcdowell"', ...
517 %! '},', ...
518 %! '{', ...
519 %! '"id":2,', ...
520 %! '"name":"Jewel"', ...
521 %! '}', ...
522 %! ']', ...
523 %! '}]'];
524 %! act = jsonencode (data, 'ConvertInfAndNaN', false);
525 %! assert (isequal (exp, act));
526
527 % a big example
528 %!testif HAVE_RAPIDJSON
529 %! tmp1 = struct ('x_id', '5ee28980fc9ab3', 'index', 0, 'guid', 'b229d1de-f94a', ...
530 %! 'latitude', -17.124067, 'longitude', -61.161831, 'friends', ...
531 %! struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'}));
532 %! tmp2 = struct ('numericArray',{{'str'; 5; -Inf}}, 'nonnumericArray', {[1; 2; NaN]});
533 %! tmp3 = struct ('firstName', 'John','lastName', 'Smith', 'age', 25, 'address', ...
534 %! struct('streetAddress', '21 2nd Street', 'city', 'New York', 'state', 'NY'), ...
535 %! 'phoneNumber', struct ('type', 'home', 'number', '212 555-1234'));
536 %! data = {{'str'; Inf; {}}; [1; 2; NaN]; {'foo'; 'bar'; {'foo'; 'bar'}};
537 %! cat(3, [1, 3; 5, 7], [2, 4; 6, 8]); {tmp1; tmp2 ;tmp3}};
538 %! exp = ['[["str",null,[]],[1,2,null],["foo","bar",["foo","bar"]],', ...
539 %! '[[[1,2],[3,4]],[[5,6],[7,8]]],' , ...
540 %! '[', ...
541 %! '{', ...
542 %! '"x_id":"5ee28980fc9ab3",', ...
543 %! '"index":0,', ...
544 %! '"guid":"b229d1de-f94a",', ...
545 %! '"latitude":-17.124067,', ...
546 %! '"longitude":-61.161831,', ...
547 %! '"friends":[', ...
548 %! '{', ...
549 %! '"id":0,', ...
550 %! '"name":"Collins"', ...
551 %! '},', ...
552 %! '{', ...
553 %! '"id":1,', ...
554 %! '"name":"Hays"', ...
555 %! '},', ...
556 %! '{', ...
557 %! '"id":2,', ...
558 %! '"name":"Griffin"', ...
559 %! '}', ...
560 %! ']', ...
561 %! '},', ...
562 %! '{"numericArray":["str",5,null],"nonnumericArray":[1,2,null]},', ...
563 %! '{', ...
564 %! '"firstName":"John",', ...
565 %! '"lastName":"Smith",', ...
566 %! '"age":25,', ...
567 %! '"address":', ...
568 %! '{', ...
569 %! '"streetAddress":"21 2nd Street",', ...
570 %! '"city":"New York",', ...
571 %! '"state":"NY"', ...
572 %! '},', ...
573 %! '"phoneNumber":', ...
574 %! '{', ...
575 %! '"type":"home",', ...
576 %! '"number":"212 555-1234"', ...
577 %! '}', ...
578 %! '}]]'];
579 %! act = jsonencode (data);
580 %! assert (isequal (exp, act));