Mercurial > octave
view 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 |
line wrap: on
line source
% test jsonencode % Note: This script is intended to be a script-based unit test % for MATLAB to test compatibility. Don't break that! % some tests here are just the reverse of tests in jsondecode with some modifiactions %% Test 1: encode logical and numeric scalars, NaN and Inf %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode (true), 'true')); %! assert (isequal (jsonencode (50.025), '50.025')); %! assert (isequal (jsonencode (NaN), 'null')); %! assert (isequal (jsonencode (Inf), 'null')); %! assert (isequal (jsonencode (-Inf), 'null')); % Customized encoding of Nan, Inf, -Inf %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode (NaN, 'ConvertInfAndNaN', true), 'null')); %! assert (isequal (jsonencode (Inf, 'ConvertInfAndNaN', true), 'null')); %! assert (isequal (jsonencode (-Inf, 'ConvertInfAndNaN', true), 'null')); %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode (NaN, 'ConvertInfAndNaN', false), 'NaN')); %! assert (isequal (jsonencode (Inf, 'ConvertInfAndNaN', false), 'Infinity')); %! assert (isequal (jsonencode (-Inf, 'ConvertInfAndNaN', false), '-Infinity')); %% Test 2: encode character vectors and arrays %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode (''), '""')); %! assert (isequal (jsonencode ('hello there'), '"hello there"')); %! assert (isequal (jsonencode (['foo'; 'bar']), '["foo","bar"]')); %! assert (isequal (jsonencode (['foo', 'bar'; 'foo', 'bar']), '["foobar","foobar"]')); %!testif HAVE_RAPIDJSON %! data = [[['foo'; 'bar']; ['foo'; 'bar']], [['foo'; 'bar']; ['foo'; 'bar']]]; %! exp = '["foofoo","barbar","foofoo","barbar"]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %!testif HAVE_RAPIDJSON %! data = cat (3, ['a', 'b'; 'c', 'd'], ['e', 'f'; 'g', 'h']); %! exp = '[["ab","ef"],["cd","gh"]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try different dimensions for the array %!testif HAVE_RAPIDJSON %! data = cat (3, ['a', 'b'; 'c', 'd'; '1', '2'], ['e', 'f'; 'g', 'h'; '3', '4']); %! exp = '[["ab","ef"],["cd","gh"],["12","34"]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array %!testif HAVE_RAPIDJSON %! tmp1 = cat (3, ['1', '3'; '5', '7'; '9', 'e'; 'f', 'g'], ... %! ['2', '4'; '6', '8'; 'a', 'b'; 'c', 'd']); %! tmp2 = cat (3, ['1', '3'; '5', '7'; '9', 'e'; 'f', 'g'], ... %! ['2', '4'; '6', '8'; 'a', 'b'; 'c', 'd']); %! data = cat (4, tmp1, tmp2); %! exp = ['[[["13","13"],["24","24"]],[["57","57"],["68","68"]],', ... %! '[["9e","9e"],["ab","ab"]],[["fg","fg"],["cd","cd"]]]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try different dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = cat (4, ['a'; 'b'], ['c'; 'd']); %! exp = '[[["a","c"]],[["b","d"]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try different dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = cat (8, ['a'], ['c']); %! exp = '"ac"'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try different dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = cat (8, ['a'; 'b'; '1'], ['c'; 'd'; '2']); %! exp = '[[[[[[["a","c"]]]]]],[[[[[["b","d"]]]]]],[[[[[["1","2"]]]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %% Test 3: encode numeric and logical arrays (with NaN and Inf) % test simple vectors %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode ([]), '[]')); %! assert (isequal (jsonencode ([1, 2, 3, 4]), '[1,2,3,4]')); %! assert (isequal (jsonencode ([true; false; true]), '[true,false,true]')); % test arrays %!testif HAVE_RAPIDJSON %! data = [1 NaN; 3 4]; %! exp = '[[1,null],[3,4]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %!testif HAVE_RAPIDJSON %! data = cat (3, [NaN, 3; 5, Inf], [2, 4; -Inf, 8]); %! exp = '[[[null,2],[3,4]],[[5,null],[null,8]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % Customized encoding of Nan, Inf, -Inf %!testif HAVE_RAPIDJSON %! data = cat (3, [1, NaN; 5, 7], [2, Inf; 6, -Inf]); %! exp = '[[[1,2],[NaN,Infinity]],[[5,6],[7,-Infinity]]]'; %! act = jsonencode (data, 'ConvertInfAndNaN', false); %! assert (isequal (exp, act)); % try different dimensions for the array %!testif HAVE_RAPIDJSON %! data = cat (3, [1, 3; 5, 7], [2, 4; 6, 8], [-1, NaN; Inf, -Inf]); %! exp = '[[[1,2,-1],[3,4,NaN]],[[5,6,Infinity],[7,8,-Infinity]]]'; %! act = jsonencode (data, 'ConvertInfAndNaN', false); %! assert (isequal (exp, act)); % try different dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = cat (3, [1; 7; 11], [4; 8; 12]); %! exp = '[[[1,4]],[[7,8]],[[11,12]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! tmp1 = cat (3, [5, 7], [2, 4]); %! tmp2 = cat (3, [-1, -3], [-2, -4]); %! data = cat (4, tmp1, tmp2); %! exp = '[[[[5,-1],[2,-2]],[[7,-3],[4,-4]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! tmp1 = cat (3, [5; 7], [2; 4]); %! tmp2 = cat (3, [-1; -3], [-2; -4]); %! data = cat (4, tmp1, tmp2); %! exp = '[[[[5,-1],[2,-2]]],[[[7,-3],[4,-4]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = cat (4, [1, 3; 5, 7], [-1, -3; -5, -7]); %! exp = '[[[[1,-1]],[[3,-3]]],[[[5,-5]],[[7,-7]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = ones ([1 1 1 1 1 6]); %! exp = '[1,1,1,1,1,1]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with one of its dimensions equals one %!testif HAVE_RAPIDJSON %! data = ones ([1 2 2 2 2]); %! exp = '[[[[[1,1],[1,1]],[[1,1],[1,1]]],[[[1,1],[1,1]],[[1,1],[1,1]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 2 2 1 2]); %! exp = '[[[[[1,1]],[[1,1]]],[[[1,1]],[[1,1]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 2 1 2 1 2]); %! exp = '[[[[[[1,1]],[[1,1]]]],[[[[1,1]],[[1,1]]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 1 2 1 2 1 2]); %! exp = '[[[[[[[1,1]],[[1,1]]]],[[[[1,1]],[[1,1]]]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 2 2 1 1 2]); %! exp = '[[[[[[1,1]]],[[[1,1]]]],[[[[1,1]]],[[[1,1]]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 2 1 3 1 1 1 2]); %! exp = ['[[[[[[[[1,1]]]],[[[[1,1]]]],[[[[1,1]]]]]],[[[[[[1,1]]]],', ... %! '[[[[1,1]]]],[[[[1,1]]]]]]]]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 1 1 1 2 1 1 1 2]); %! exp = '[[[[[[[[[1,1]]]],[[[[1,1]]]]]]]]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 3 2 1 1 2 1 2 2]); %! exp = ['[[[[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]],[[[[[[1,1],', ... %! '[1,1]]],[[[1,1],[1,1]]]]]]],[[[[[[[1,1],[1,1]]],[[[1,', ... %! '1],[1,1]]]]]],[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]]],', ... %! '[[[[[[[1,1],[1,1]]],[[[1,1],[1,1]]]]]],[[[[[[1,1],[1,', ... %! '1]]],[[[1,1],[1,1]]]]]]]]]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array with some of its dimensions equal one %!testif HAVE_RAPIDJSON %! data = ones ([1 1 1 1 1 1 2 1 1 2 2 3 1 1 1 1 1 1 1 2]); %! exp = ['[[[[[[[[[[[[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],', ... %! '[[[[[[[[1,1]]]]]]]]],[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]', ... %! ']]],[[[[[[[[1,1]]]]]]]]]],[[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]', ... %! ']]]]]]],[[[[[[[[1,1]]]]]]]]],[[[[[[[[[1,1]]]]]]]],[[[[[[', ... %! '[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]]]]]],[[[[[[[[[[[[[1,1]', ... %! ']]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]],[[[[[[[[', ... %! '[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]]],[[[', ... %! '[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]]],', ... %! '[[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]]]]]]],[[[[[[[[1,1]]', ... %! ']]]]]]]]]]]]]]]]]]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); % try higher dimensions for the array %!testif HAVE_RAPIDJSON %! tmp1 = cat (3, [1, 3; 5, 7; 9, 11; 13, 15], [2, 4; 6, 8; 10, 12; 14, 16]); %! tmp2 = cat (3, [-1, -3; -5, -7; -9, -11; -13, -15], ... %! [-2, -4; -6, -8; -10, -12; -14, -16]); %! data = cat (4, tmp1, tmp2); %! exp = ['[[[[1,-1],[2,-2]],[[3,-3],[4,-4]]],[[[5,-5],[6,-6]],[[7,-7],', ... %! '[8,-8]]],[[[9,-9],[10,-10]],[[11,-11],[12,-12]]],', ... %! '[[[13,-13],[14,-14]],[[15,-15],[16,-16]]]]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); %!testif HAVE_RAPIDJSON %! data = [true false; true false; true false]; %! exp = '[[true,false],[true,false],[true,false]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %% Test 4: encode containers.Map % KeyType must be char to encode objects of containers.Map %!testif HAVE_RAPIDJSON %! assert (isequal (jsonencode (containers.Map('1', [1, 2, 3])), '{"1":[1,2,3]}')); %!testif HAVE_RAPIDJSON %! data = containers.Map({'foo'; 'bar'; 'baz'}, [1, 2, 3]); %! exp = '{"bar":2,"baz":3,"foo":1}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %!testif HAVE_RAPIDJSON %! data = containers.Map({'foo'; 'bar'; 'baz'}, {{1, 'hello', NaN}, true, [2, 3, 4]}); %! exp = '{"bar":true,"baz":[2,3,4],"foo":[1,"hello",NaN]}'; %! act = jsonencode (data, 'ConvertInfAndNaN', false); %! assert (isequal (exp, act)); %% Test 5: encode scalar structs % check the encoding of Boolean, Number and String values inside a struct %!testif HAVE_RAPIDJSON %! data = struct ('number', 3.14, 'string', 'string', 'boolean', false); %! exp = '{"number":3.14,"string":"string","boolean":false}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % check the decoding of null, Inf and -Inf values inside a struct %!testif HAVE_RAPIDJSON %! data = struct ('numericArray', [7, NaN, Inf, -Inf]); %! exp = '{"numericArray":[7,null,null,null]}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % Customized encoding of Nan, Inf, -Inf %!testif HAVE_RAPIDJSON %! data = struct ('numericArray', [7, NaN, Inf, -Inf]); %! exp = '{"numericArray":[7,NaN,Infinity,-Infinity]}'; %! act = jsonencode (data, 'ConvertInfAndNaN', false); %! assert (isequal (exp, act)); % check the encoding of structs inside a struct %!testif HAVE_RAPIDJSON %! data = struct ('object', struct ('field1', 1, 'field2', 2, 'field3', 3)); %! exp = '{"object":{"field1":1,"field2":2,"field3":3}}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % check the encoding of empty structs, empty arrays and Inf inside a struct %!testif HAVE_RAPIDJSON %! data = struct ('a', Inf, 'b', [], 'c', struct ()); %! exp = '{"a":null,"b":[],"c":{}}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % a big test %!testif HAVE_RAPIDJSON %! tmp1 = struct ('para', ['A meta-markup language, used to create markup languages', ... %! ' such as DocBook.'],'GlossSeeAlso', {{'GML'; 'XML'}}); %! tmp2 = struct ('ID', 'SGML', 'SortAs', 'SGML', 'GlossTerm', ... %! 'Standard Generalized Markup Language', 'Acronym', 'SGML', ... %! 'Abbrev', 'ISO 8879:1986', 'GlossDef', tmp1, 'GlossSee', 'markup'); %! data = struct ('glossary', struct ('title', 'example glossary', 'GlossDiv', ... %! struct ('title', 'S', 'GlossList', struct ('GlossEntry', tmp2)))); %! exp = ['{' , ... %! '"glossary":{', ... %! '"title":"example glossary",', ... %! '"GlossDiv":{', ... %! '"title":"S",', ... %! '"GlossList":{', ... %! '"GlossEntry":{', ... %! '"ID":"SGML",', ... %! '"SortAs":"SGML",', ... %! '"GlossTerm":"Standard Generalized Markup Language",', ... %! '"Acronym":"SGML",', ... %! '"Abbrev":"ISO 8879:1986",', ... %! '"GlossDef":{', ... %! '"para":"A meta-markup language, ', ... %! 'used to create markup languages such as DocBook.",', ... %! '"GlossSeeAlso":["GML","XML"]', ... %! '},', ... %! '"GlossSee":"markup"', ... %! '}', ... %! '}', ... %! '}', ... %! '}', ... %! '}']; %! act = jsonencode (data); %! assert (isequal (exp, act)); %% Test 6: encode struct arrays %!testif HAVE_RAPIDJSON %! data = struct ('structarray', struct ('a', {1; 3}, 'b', {2; 4})); %! exp = '{"structarray":[{"a":1,"b":2},{"a":3,"b":4}]}'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % a big Test %!testif HAVE_RAPIDJSON %! tmp1 = struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'}); %! tmp2 = struct ('id', {0; 1; 2}, 'name', {'Osborn'; 'Mcdowell'; 'Jewel'}); %! tmp3 = struct ('id', {0; 1; 2}, 'name', {'Socorro'; 'Darla'; 'Leanne'}); %! data = struct ('x_id', {'5ee28980fc9ab3'; '5ee28980dd7250'; '5ee289802422ac'}, ... %! 'index', {0; 1; 2}, 'guid', {'b229d1de-f94a'; '39cee338-01fb'; '3db8d55a-663e'}, ... %! 'latitude', {-17.124067; 13.205994; -35.453456}, 'longitude', ... %! {-61.161831; -37.276231; 14.080287}, 'friends', {tmp1; tmp2; tmp3}); %! exp = ['[', ... %! '{', ... %! '"x_id":"5ee28980fc9ab3",', ... %! '"index":0,', ... %! '"guid":"b229d1de-f94a",', ... %! '"latitude":-17.124067,', ... %! '"longitude":-61.161831,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Collins"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Hays"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Griffin"', ... %! '}', ... %! ']', ... %! '},', ... %! '{', ... %! '"x_id":"5ee28980dd7250",', ... %! '"index":1,', ... %! '"guid":"39cee338-01fb",', ... %! '"latitude":13.205994,', ... %! '"longitude":-37.276231,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Osborn"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Mcdowell"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Jewel"', ... %! '}', ... %! ']', ... %! '},', ... %! '{', ... %! '"x_id":"5ee289802422ac",', ... %! '"index":2,', ... %! '"guid":"3db8d55a-663e",', ... %! '"latitude":-35.453456,', ... %! '"longitude":14.080287,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Socorro"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Darla"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Leanne"', ... %! '}', ... %! ']', ... %! '}', ... %! ']']; %! act = jsonencode (data); %! assert (isequal (exp, act)); %% Test 7: encode cell arrays %!testif HAVE_RAPIDJSON %! assert (isequal ('[]', jsonencode ({}))); %! assert (isequal ('[5]', jsonencode ({5}))); %! assert (isequal ('["hello there"]', jsonencode ({'hello there'}))); %!testif HAVE_RAPIDJSON %! data = {'true', 'true'; 'false', 'true'}; %! exp = '["true","false","true","true"]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); %!testif HAVE_RAPIDJSON %! data = {'foo'; 'bar'; {'foo'; 'bar'}}; %! exp = '["foo","bar",["foo","bar"]]'; %! act = jsonencode (data); %! assert (isequal (exp, act)); % cell array of structs & a big test %!testif HAVE_RAPIDJSON %! tmp1 = struct ('x_id', '5ee28980fc9ab3', 'index', 0, 'guid', 'b229d1de-f94a', ... %! 'latitude', -17.124067, 'longitude', -61.161831, 'friends', ... %! struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'})); %! tmp2 = struct ('numericArray',{{'str'; 5; []}}, 'nonnumericArray', {[1; 2; NaN]}); %! tmp3 = struct ('firstName', 'John','lastName', 'Smith', 'age', 25, 'address', ... %! struct('streetAddress', '21 2nd Street', 'city', 'New York', 'state', 'NY'), ... %! 'phoneNumber', struct ('type', 'home', 'number', '212 555-1234')); %! data = {tmp1; tmp2; tmp3}; %! exp = ['[', ... %! '{', ... %! '"x_id":"5ee28980fc9ab3",', ... %! '"index":0,', ... %! '"guid":"b229d1de-f94a",', ... %! '"latitude":-17.124067,', ... %! '"longitude":-61.161831,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Collins"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Hays"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Griffin"', ... %! '}', ... %! ']', ... %! '},', ... %! '{"numericArray":["str",5,[]],"nonnumericArray":[1,2,null]},', ... %! '{', ... %! '"firstName":"John",', ... %! '"lastName":"Smith",', ... %! '"age":25,', ... %! '"address":', ... %! '{', ... %! '"streetAddress":"21 2nd Street",', ... %! '"city":"New York",', ... %! '"state":"NY"', ... %! '},', ... %! '"phoneNumber":', ... %! '{', ... %! '"type":"home",', ... %! '"number":"212 555-1234"', ... %! '}', ... %! '}]']; %! act = jsonencode (data); %! assert (isequal (exp, act)); % cell array of diferrent types & Customized encoding of Nan, Inf, -Inf %!testif HAVE_RAPIDJSON %! tmp = struct ('x_id', '5ee28980dd7250', 'index', 1, 'guid', '39cee338-01fb', ... %! 'latitude', 13.205994, 'longitude', -37.276231, 'friends', ... %! struct ('id', {0; 1; 2}, 'name', {'Osborn'; 'Mcdowell'; 'Jewel'})); %! data = {NaN; true; Inf; 2531.023; 'hello there'; tmp}; %! exp = ['[NaN,true,Infinity,2531.023,"hello there",', ... %! '{', ... %! '"x_id":"5ee28980dd7250",', ... %! '"index":1,', ... %! '"guid":"39cee338-01fb",', ... %! '"latitude":13.205994,', ... %! '"longitude":-37.276231,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Osborn"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Mcdowell"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Jewel"', ... %! '}', ... %! ']', ... %! '}]']; %! act = jsonencode (data, 'ConvertInfAndNaN', false); %! assert (isequal (exp, act)); % a big example %!testif HAVE_RAPIDJSON %! tmp1 = struct ('x_id', '5ee28980fc9ab3', 'index', 0, 'guid', 'b229d1de-f94a', ... %! 'latitude', -17.124067, 'longitude', -61.161831, 'friends', ... %! struct ('id', {0; 1; 2}, 'name', {'Collins'; 'Hays'; 'Griffin'})); %! tmp2 = struct ('numericArray',{{'str'; 5; -Inf}}, 'nonnumericArray', {[1; 2; NaN]}); %! tmp3 = struct ('firstName', 'John','lastName', 'Smith', 'age', 25, 'address', ... %! struct('streetAddress', '21 2nd Street', 'city', 'New York', 'state', 'NY'), ... %! 'phoneNumber', struct ('type', 'home', 'number', '212 555-1234')); %! data = {{'str'; Inf; {}}; [1; 2; NaN]; {'foo'; 'bar'; {'foo'; 'bar'}}; %! cat(3, [1, 3; 5, 7], [2, 4; 6, 8]); {tmp1; tmp2 ;tmp3}}; %! exp = ['[["str",null,[]],[1,2,null],["foo","bar",["foo","bar"]],', ... %! '[[[1,2],[3,4]],[[5,6],[7,8]]],' , ... %! '[', ... %! '{', ... %! '"x_id":"5ee28980fc9ab3",', ... %! '"index":0,', ... %! '"guid":"b229d1de-f94a",', ... %! '"latitude":-17.124067,', ... %! '"longitude":-61.161831,', ... %! '"friends":[', ... %! '{', ... %! '"id":0,', ... %! '"name":"Collins"', ... %! '},', ... %! '{', ... %! '"id":1,', ... %! '"name":"Hays"', ... %! '},', ... %! '{', ... %! '"id":2,', ... %! '"name":"Griffin"', ... %! '}', ... %! ']', ... %! '},', ... %! '{"numericArray":["str",5,null],"nonnumericArray":[1,2,null]},', ... %! '{', ... %! '"firstName":"John",', ... %! '"lastName":"Smith",', ... %! '"age":25,', ... %! '"address":', ... %! '{', ... %! '"streetAddress":"21 2nd Street",', ... %! '"city":"New York",', ... %! '"state":"NY"', ... %! '},', ... %! '"phoneNumber":', ... %! '{', ... %! '"type":"home",', ... %! '"number":"212 555-1234"', ... %! '}', ... %! '}]]']; %! act = jsonencode (data); %! assert (isequal (exp, act));