comparison test/json/jsondecode_BIST.tst @ 29994:eb768fc5e6b7

jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688) * libinterp/corefcn/jsondecode.cc (decode_object_array): Resize struct array if it has no fields. * libinterp/corefcn/jsondecode.cc (decode_array_of_arrays): Handle struct arrays correctly. * test/json/jsondecode_BIST.tst: New tests. Many thanks to an anonymous contributor.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 18 Aug 2021 14:56:07 +0900
parents 0d67814eb038
children 1575b324613d
comparison
equal deleted inserted replaced
29993:b5fae48ad807 29994:eb768fc5e6b7
553 %! exp = struct ('cell_array', {{struct('x_1a', 1, 'b_1', 2); ... 553 %! exp = struct ('cell_array', {{struct('x_1a', 1, 'b_1', 2); ...
554 %! struct('x_1a', 3, 'b_2', 4)}}); 554 %! struct('x_1a', 3, 'b_2', 4)}});
555 %! obs = jsondecode (json, "ReplacementStyle", "underscore", "Prefix", "x_"); 555 %! obs = jsondecode (json, "ReplacementStyle", "underscore", "Prefix", "x_");
556 %! assert (isequal (obs, exp)); 556 %! assert (isequal (obs, exp));
557 557
558 %%% Test 8: More tests from https://github.com/apjanke/octave-jsonstuff (bug #60688)
559
560 %!testif HAVE_RAPIDJSON
561 %! assert (jsondecode ('42'), 42);
562 %! assert (jsondecode ('"foobar"'), "foobar");
563 %! assert (jsondecode ('null'), []);
564 %! assert (jsondecode ('[]'), []);
565 %! assert (jsondecode ('[[]]'), {[]});
566 %! assert (jsondecode ('[[[]]]'), {{[]}});
567 %! assert (jsondecode ('[1, 2, 3]'), [1; 2; 3]);
568 %! assert (jsondecode ('[1, 2, null]'), [1; 2; NaN]);
569 %! assert (jsondecode ('[1, 2, "foo"]'), {1; 2; "foo"});
570 %! assert (jsondecode ('{"foo": 42, "bar": "hello"}'), ...
571 %! struct("foo",42, "bar","hello"));
572 %! assert (jsondecode ('[{"foo": 42, "bar": "hello"}, {"foo": 1.23, "bar": "world"}]'), ...
573 %! struct("foo", {42; 1.23}, "bar", {"hello"; "world"}));
574 %! assert (jsondecode ('[1, 2]'), [1; 2]);
575 %! assert (jsondecode ('[[1, 2]]'), [1 2]);
576 %! assert (jsondecode ('[[[1, 2]]]'), cat(3, 1, 2));
577 %! assert (jsondecode ('[[1, 2], [3, 4]]'), [1 2; 3 4]);
578 %! assert (jsondecode ('[[[1, 2], [3, 4]]]'), cat(3, [1 3], [2 4]));
579 %! assert (jsondecode ('[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]'), ...
580 %! cat(3, [1 3; 5 7], [2 4; 6 8]));
581 %! assert (jsondecode ('{}'), struct);
582 %! assert (jsondecode ('[{}]'), struct);
583 %! assert (jsondecode ('[[{}]]'), struct);
584 %! assert (jsondecode ('[{}, {}]'), [struct; struct]);
585 %! assert (jsondecode ('[[{}, {}]]'), [struct struct]);
586 %! assert (jsondecode ('[[[{}, {}]]]'), cat(3, struct, struct));
587 %! assert (jsonencode (42), "42");
588 %! assert (jsonencode ("foo"), '"foo"');
589 %! assert (jsonencode ([1 2 3]), '[1,2,3]');
590 %! assert (jsonencode (NaN), 'null');
591 %! assert (jsonencode ([1 2 NaN]), '[1,2,null]');
592 %! assert (jsonencode ({}), "[]");
593
594 %%% Test 9: And even some more tests for #60688...
595
596 %!testif HAVE_RAPIDJSON
597 %! assert (jsondecode ('[[{"foo": 42}, {"foo": 1.23}], [{"foo": 12}, {"foo": "bar"}]]'), ...
598 %! struct("foo", {42 1.23; 12 "bar"}));
599 %! assert (jsondecode ('[[{"foo": 42}, {"foo": 1.23}], [{"bar": 12}, {"foo": "bar"}]]'), ...
600 %! {struct("foo", {42; 1.23}); {struct("bar", 12); struct("foo", "bar")}});
601
558 %% Check decoding of objects inside an object without using makeValidName 602 %% Check decoding of objects inside an object without using makeValidName
559 %!testif HAVE_RAPIDJSON 603 %!testif HAVE_RAPIDJSON
560 %! json = ['{"object": {" hi 1 ": 1, "%string.array": 2,' ... 604 %! json = ['{"object": {" hi 1 ": 1, "%string.array": 2,' ...
561 %! '"img/svg+xml": 3, "": 1}}']; 605 %! '"img/svg+xml": 3, "": 1}}'];
562 %! exp = struct ('object', ... 606 %! exp = struct ('object', ...