diff 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
line wrap: on
line diff
--- a/test/json/jsondecode_BIST.tst	Wed Aug 18 00:25:50 2021 -0400
+++ b/test/json/jsondecode_BIST.tst	Wed Aug 18 14:56:07 2021 +0900
@@ -555,6 +555,50 @@
 %! obs  = jsondecode (json, "ReplacementStyle", "underscore", "Prefix", "x_");
 %! assert (isequal (obs, exp));
 
+%%% Test 8: More tests from https://github.com/apjanke/octave-jsonstuff (bug #60688)
+
+%!testif HAVE_RAPIDJSON
+%! assert (jsondecode ('42'), 42);
+%! assert (jsondecode ('"foobar"'), "foobar");
+%! assert (jsondecode ('null'), []);
+%! assert (jsondecode ('[]'), []);
+%! assert (jsondecode ('[[]]'), {[]});
+%! assert (jsondecode ('[[[]]]'), {{[]}});
+%! assert (jsondecode ('[1, 2, 3]'), [1; 2; 3]);
+%! assert (jsondecode ('[1, 2, null]'), [1; 2; NaN]);
+%! assert (jsondecode ('[1, 2, "foo"]'), {1; 2; "foo"});
+%! assert (jsondecode ('{"foo": 42, "bar": "hello"}'), ...
+%!         struct("foo",42, "bar","hello"));
+%! assert (jsondecode ('[{"foo": 42, "bar": "hello"}, {"foo": 1.23, "bar": "world"}]'), ...
+%!         struct("foo", {42; 1.23}, "bar", {"hello"; "world"}));
+%! assert (jsondecode ('[1, 2]'), [1; 2]);
+%! assert (jsondecode ('[[1, 2]]'), [1 2]);
+%! assert (jsondecode ('[[[1, 2]]]'), cat(3, 1, 2));
+%! assert (jsondecode ('[[1, 2], [3, 4]]'), [1 2; 3 4]);
+%! assert (jsondecode ('[[[1, 2], [3, 4]]]'), cat(3, [1 3], [2 4]));
+%! assert (jsondecode ('[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]'), ...
+%!         cat(3, [1 3; 5 7], [2 4; 6 8]));
+%! assert (jsondecode ('{}'), struct);
+%! assert (jsondecode ('[{}]'), struct);
+%! assert (jsondecode ('[[{}]]'), struct);
+%! assert (jsondecode ('[{}, {}]'), [struct; struct]);
+%! assert (jsondecode ('[[{}, {}]]'), [struct struct]);
+%! assert (jsondecode ('[[[{}, {}]]]'), cat(3, struct, struct));
+%! assert (jsonencode (42), "42");
+%! assert (jsonencode ("foo"), '"foo"');
+%! assert (jsonencode ([1 2 3]), '[1,2,3]');
+%! assert (jsonencode (NaN), 'null');
+%! assert (jsonencode ([1 2 NaN]), '[1,2,null]');
+%! assert (jsonencode ({}), "[]");
+
+%%% Test 9: And even some more tests for #60688...
+
+%!testif HAVE_RAPIDJSON
+%! assert (jsondecode ('[[{"foo": 42}, {"foo": 1.23}], [{"foo": 12}, {"foo": "bar"}]]'), ...
+%!         struct("foo", {42 1.23; 12 "bar"}));
+%! assert (jsondecode ('[[{"foo": 42}, {"foo": 1.23}], [{"bar": 12}, {"foo": "bar"}]]'), ...
+%!         {struct("foo", {42; 1.23}); {struct("bar", 12); struct("foo", "bar")}});
+
 %% Check decoding of objects inside an object without using makeValidName
 %!testif HAVE_RAPIDJSON
 %! json = ['{"object": {"  hi 1   ": 1, "%string.array": 2,' ...