123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582 |
- // __ _____ _____ _____
- // __| | __| | | | JSON for Modern C++ (supporting code)
- // | | |__ | | | | | | version 3.11.2
- // |_____|_____|_____|_|___| https://github.com/nlohmann/json
- //
- // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
- // SPDX-License-Identifier: MIT
- #include "doctest_compatibility.h"
- #define JSON_TESTS_PRIVATE
- #include <nlohmann/json.hpp>
- using nlohmann::json;
- #include <algorithm>
- #include <climits>
- #include <limits>
- #include <iostream>
- #include <fstream>
- #include <set>
- #include "make_test_data_available.hpp"
- #include "test_utils.hpp"
- namespace
- {
- class SaxCountdown
- {
- public:
- explicit SaxCountdown(const int count) : events_left(count)
- {}
- bool null()
- {
- return events_left-- > 0;
- }
- bool boolean(bool /*unused*/)
- {
- return events_left-- > 0;
- }
- bool number_integer(json::number_integer_t /*unused*/)
- {
- return events_left-- > 0;
- }
- bool number_unsigned(json::number_unsigned_t /*unused*/)
- {
- return events_left-- > 0;
- }
- bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
- {
- return events_left-- > 0;
- }
- bool string(std::string& /*unused*/)
- {
- return events_left-- > 0;
- }
- bool binary(std::vector<std::uint8_t>& /*unused*/)
- {
- return events_left-- > 0;
- }
- bool start_object(std::size_t /*unused*/)
- {
- return events_left-- > 0;
- }
- bool key(std::string& /*unused*/)
- {
- return events_left-- > 0;
- }
- bool end_object()
- {
- return events_left-- > 0;
- }
- bool start_array(std::size_t /*unused*/)
- {
- return events_left-- > 0;
- }
- bool end_array()
- {
- return events_left-- > 0;
- }
- bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
- {
- return false;
- }
- private:
- int events_left = 0;
- };
- } // namespace
- // at some point in the future, a unit test dedicated to type traits might be a good idea
- template <typename OfType, typename T, bool MinInRange, bool MaxInRange>
- struct trait_test_arg
- {
- using of_type = OfType;
- using type = T;
- static constexpr bool min_in_range = MinInRange;
- static constexpr bool max_in_range = MaxInRange;
- };
- TEST_CASE_TEMPLATE_DEFINE("value_in_range_of trait", T, value_in_range_of_test)
- {
- using nlohmann::detail::value_in_range_of;
- using of_type = typename T::of_type;
- using type = typename T::type;
- constexpr bool min_in_range = T::min_in_range;
- constexpr bool max_in_range = T::max_in_range;
- type const val_min = std::numeric_limits<type>::min();
- type const val_min2 = val_min + 1;
- type const val_max = std::numeric_limits<type>::max();
- type const val_max2 = val_max - 1;
- REQUIRE(CHAR_BIT == 8);
- std::string of_type_str;
- if (std::is_unsigned<of_type>::value)
- {
- of_type_str += "u";
- }
- of_type_str += "int";
- of_type_str += std::to_string(sizeof(of_type) * 8);
- INFO("of_type := ", of_type_str);
- std::string type_str;
- if (std::is_unsigned<type>::value)
- {
- type_str += "u";
- }
- type_str += "int";
- type_str += std::to_string(sizeof(type) * 8);
- INFO("type := ", type_str);
- CAPTURE(val_min);
- CAPTURE(min_in_range);
- CAPTURE(val_max);
- CAPTURE(max_in_range);
- if (min_in_range)
- {
- CHECK(value_in_range_of<of_type>(val_min));
- CHECK(value_in_range_of<of_type>(val_min2));
- }
- else
- {
- CHECK_FALSE(value_in_range_of<of_type>(val_min));
- CHECK_FALSE(value_in_range_of<of_type>(val_min2));
- }
- if (max_in_range)
- {
- CHECK(value_in_range_of<of_type>(val_max));
- CHECK(value_in_range_of<of_type>(val_max2));
- }
- else
- {
- CHECK_FALSE(value_in_range_of<of_type>(val_max));
- CHECK_FALSE(value_in_range_of<of_type>(val_max2));
- }
- }
- TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
- trait_test_arg<std::int32_t, std::int32_t, true, true>, \
- trait_test_arg<std::int32_t, std::uint32_t, true, false>, \
- trait_test_arg<std::uint32_t, std::int32_t, false, true>, \
- trait_test_arg<std::uint32_t, std::uint32_t, true, true>, \
- trait_test_arg<std::int32_t, std::int64_t, false, false>, \
- trait_test_arg<std::int32_t, std::uint64_t, true, false>, \
- trait_test_arg<std::uint32_t, std::int64_t, false, false>, \
- trait_test_arg<std::uint32_t, std::uint64_t, true, false>, \
- trait_test_arg<std::int64_t, std::int32_t, true, true>, \
- trait_test_arg<std::int64_t, std::uint32_t, true, true>, \
- trait_test_arg<std::uint64_t, std::int32_t, false, true>, \
- trait_test_arg<std::uint64_t, std::uint32_t, true, true>, \
- trait_test_arg<std::int64_t, std::int64_t, true, true>, \
- trait_test_arg<std::int64_t, std::uint64_t, true, false>, \
- trait_test_arg<std::uint64_t, std::int64_t, false, true>, \
- trait_test_arg<std::uint64_t, std::uint64_t, true, true>);
- #if SIZE_MAX == 0xffffffff
- TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
- trait_test_arg<std::size_t, std::int32_t, false, true>, \
- trait_test_arg<std::size_t, std::uint32_t, true, true>, \
- trait_test_arg<std::size_t, std::int64_t, false, false>, \
- trait_test_arg<std::size_t, std::uint64_t, true, false>);
- #else
- TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
- trait_test_arg<std::size_t, std::int32_t, false, true>, \
- trait_test_arg<std::size_t, std::uint32_t, true, true>, \
- trait_test_arg<std::size_t, std::int64_t, false, true>, \
- trait_test_arg<std::size_t, std::uint64_t, true, true>);
- #endif
- TEST_CASE("BJData")
- {
- SECTION("binary_reader BJData LUT arrays are sorted")
- {
- std::vector<std::uint8_t> const data;
- auto ia = nlohmann::detail::input_adapter(data);
- // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
- nlohmann::detail::binary_reader<json, decltype(ia)> const br{std::move(ia), json::input_format_t::bjdata};
- CHECK(std::is_sorted(br.bjd_optimized_type_markers.begin(), br.bjd_optimized_type_markers.end()));
- CHECK(std::is_sorted(br.bjd_types_map.begin(), br.bjd_types_map.end()));
- }
- SECTION("individual values")
- {
- SECTION("discarded")
- {
- // discarded values are not serialized
- json const j = json::value_t::discarded;
- const auto result = json::to_bjdata(j);
- CHECK(result.empty());
- }
- SECTION("null")
- {
- json const j = nullptr;
- std::vector<uint8_t> const expected = {'Z'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("boolean")
- {
- SECTION("true")
- {
- json const j = true;
- std::vector<uint8_t> const expected = {'T'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("false")
- {
- json const j = false;
- std::vector<uint8_t> const expected = {'F'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("number")
- {
- SECTION("signed")
- {
- SECTION("-9223372036854775808..-2147483649 (int64)")
- {
- std::vector<int64_t> const numbers
- {
- (std::numeric_limits<int64_t>::min)(),
- -1000000000000000000LL,
- -100000000000000000LL,
- -10000000000000000LL,
- -1000000000000000LL,
- -100000000000000LL,
- -10000000000000LL,
- -1000000000000LL,
- -100000000000LL,
- -10000000000LL,
- -2147483649LL,
- };
- for (const auto i : numbers)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('L'),
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- static_cast<uint8_t>((i >> 32) & 0xff),
- static_cast<uint8_t>((i >> 40) & 0xff),
- static_cast<uint8_t>((i >> 48) & 0xff),
- static_cast<uint8_t>((i >> 56) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 9);
- // check individual bytes
- CHECK(result[0] == 'L');
- int64_t const restored = (static_cast<int64_t>(result[8]) << 070) +
- (static_cast<int64_t>(result[7]) << 060) +
- (static_cast<int64_t>(result[6]) << 050) +
- (static_cast<int64_t>(result[5]) << 040) +
- (static_cast<int64_t>(result[4]) << 030) +
- (static_cast<int64_t>(result[3]) << 020) +
- (static_cast<int64_t>(result[2]) << 010) +
- static_cast<int64_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("-2147483648..-32769 (int32)")
- {
- std::vector<int32_t> const numbers
- {
- -32769,
- -100000,
- -1000000,
- -10000000,
- -100000000,
- -1000000000,
- -2147483647 - 1, // https://stackoverflow.com/a/29356002/266378
- };
- for (const auto i : numbers)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('l'),
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 5);
- // check individual bytes
- CHECK(result[0] == 'l');
- int32_t const restored = (static_cast<int32_t>(result[4]) << 030) +
- (static_cast<int32_t>(result[3]) << 020) +
- (static_cast<int32_t>(result[2]) << 010) +
- static_cast<int32_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("-32768..-129 (int16)")
- {
- for (int32_t i = -32768; i <= -129; ++i)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('I'),
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'I');
- auto const restored = static_cast<int16_t>(((result[2] << 8) + result[1]));
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("-9263 (int16)")
- {
- json const j = -9263;
- std::vector<uint8_t> const expected = {'I', 0xd1, 0xdb};
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'I');
- auto const restored = static_cast<int16_t>(((result[2] << 8) + result[1]));
- CHECK(restored == -9263);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("-128..-1 (int8)")
- {
- for (auto i = -128; i <= -1; ++i)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'i',
- static_cast<uint8_t>(i),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 2);
- // check individual bytes
- CHECK(result[0] == 'i');
- CHECK(static_cast<int8_t>(result[1]) == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("0..127 (int8)")
- {
- for (size_t i = 0; i <= 127; ++i)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('i'),
- static_cast<uint8_t>(i),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 2);
- // check individual bytes
- CHECK(result[0] == 'i');
- CHECK(result[1] == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("128..255 (uint8)")
- {
- for (size_t i = 128; i <= 255; ++i)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('U'),
- static_cast<uint8_t>(i),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 2);
- // check individual bytes
- CHECK(result[0] == 'U');
- CHECK(result[1] == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("256..32767 (int16)")
- {
- for (size_t i = 256; i <= 32767; ++i)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('I'),
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'I');
- auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("32768..65535 (uint16)")
- {
- for (const uint32_t i :
- {
- 32768u, 55555u, 65535u
- })
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- static_cast<uint8_t>('u'),
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'u');
- auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("65536..2147483647 (int32)")
- {
- for (const uint32_t i :
- {
- 65536u, 77777u, 2147483647u
- })
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'l',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 5);
- // check individual bytes
- CHECK(result[0] == 'l');
- uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
- (static_cast<uint32_t>(result[3]) << 020) +
- (static_cast<uint32_t>(result[2]) << 010) +
- static_cast<uint32_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("2147483648..4294967295 (uint32)")
- {
- for (const uint32_t i :
- {
- 2147483648u, 3333333333u, 4294967295u
- })
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'm',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 5);
- // check individual bytes
- CHECK(result[0] == 'm');
- uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
- (static_cast<uint32_t>(result[3]) << 020) +
- (static_cast<uint32_t>(result[2]) << 010) +
- static_cast<uint32_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("4294967296..9223372036854775807 (int64)")
- {
- std::vector<uint64_t> const v = {4294967296LU, 9223372036854775807LU};
- for (const uint64_t i : v)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json j = -1;
- j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
- // check type
- CHECK(j.is_number_integer());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'L',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 010) & 0xff),
- static_cast<uint8_t>((i >> 020) & 0xff),
- static_cast<uint8_t>((i >> 030) & 0xff),
- static_cast<uint8_t>((i >> 040) & 0xff),
- static_cast<uint8_t>((i >> 050) & 0xff),
- static_cast<uint8_t>((i >> 060) & 0xff),
- static_cast<uint8_t>((i >> 070) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 9);
- // check individual bytes
- CHECK(result[0] == 'L');
- uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
- (static_cast<uint64_t>(result[7]) << 060) +
- (static_cast<uint64_t>(result[6]) << 050) +
- (static_cast<uint64_t>(result[5]) << 040) +
- (static_cast<uint64_t>(result[4]) << 030) +
- (static_cast<uint64_t>(result[3]) << 020) +
- (static_cast<uint64_t>(result[2]) << 010) +
- static_cast<uint64_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("9223372036854775808..18446744073709551615 (uint64)")
- {
- std::vector<uint64_t> const v = {9223372036854775808ull, 18446744073709551615ull};
- for (const uint64_t i : v)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'M',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 010) & 0xff),
- static_cast<uint8_t>((i >> 020) & 0xff),
- static_cast<uint8_t>((i >> 030) & 0xff),
- static_cast<uint8_t>((i >> 040) & 0xff),
- static_cast<uint8_t>((i >> 050) & 0xff),
- static_cast<uint8_t>((i >> 060) & 0xff),
- static_cast<uint8_t>((i >> 070) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 9);
- // check individual bytes
- CHECK(result[0] == 'M');
- uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
- (static_cast<uint64_t>(result[7]) << 060) +
- (static_cast<uint64_t>(result[6]) << 050) +
- (static_cast<uint64_t>(result[5]) << 040) +
- (static_cast<uint64_t>(result[4]) << 030) +
- (static_cast<uint64_t>(result[3]) << 020) +
- (static_cast<uint64_t>(result[2]) << 010) +
- static_cast<uint64_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- }
- SECTION("unsigned")
- {
- SECTION("0..127 (int8)")
- {
- for (size_t i = 0; i <= 127; ++i)
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected{'i', static_cast<uint8_t>(i)};
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 2);
- // check individual bytes
- CHECK(result[0] == 'i');
- auto const restored = static_cast<uint8_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("128..255 (uint8)")
- {
- for (size_t i = 128; i <= 255; ++i)
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected{'U', static_cast<uint8_t>(i)};
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 2);
- // check individual bytes
- CHECK(result[0] == 'U');
- auto const restored = static_cast<uint8_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("256..32767 (int16)")
- {
- for (size_t i = 256; i <= 32767; ++i)
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'I',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'I');
- auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("32768..65535 (uint16)")
- {
- for (const uint32_t i :
- {
- 32768u, 55555u, 65535u
- })
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'u',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 3);
- // check individual bytes
- CHECK(result[0] == 'u');
- auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("65536..2147483647 (int32)")
- {
- for (const uint32_t i :
- {
- 65536u, 77777u, 2147483647u
- })
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'l',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 5);
- // check individual bytes
- CHECK(result[0] == 'l');
- uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
- (static_cast<uint32_t>(result[3]) << 020) +
- (static_cast<uint32_t>(result[2]) << 010) +
- static_cast<uint32_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("2147483648..4294967295 (uint32)")
- {
- for (const uint32_t i :
- {
- 2147483648u, 3333333333u, 4294967295u
- })
- {
- CAPTURE(i)
- // create JSON value with unsigned integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'm',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 8) & 0xff),
- static_cast<uint8_t>((i >> 16) & 0xff),
- static_cast<uint8_t>((i >> 24) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 5);
- // check individual bytes
- CHECK(result[0] == 'm');
- uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
- (static_cast<uint32_t>(result[3]) << 020) +
- (static_cast<uint32_t>(result[2]) << 010) +
- static_cast<uint32_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("4294967296..9223372036854775807 (int64)")
- {
- std::vector<uint64_t> const v = {4294967296ul, 9223372036854775807ul};
- for (const uint64_t i : v)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'L',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 010) & 0xff),
- static_cast<uint8_t>((i >> 020) & 0xff),
- static_cast<uint8_t>((i >> 030) & 0xff),
- static_cast<uint8_t>((i >> 040) & 0xff),
- static_cast<uint8_t>((i >> 050) & 0xff),
- static_cast<uint8_t>((i >> 060) & 0xff),
- static_cast<uint8_t>((i >> 070) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 9);
- // check individual bytes
- CHECK(result[0] == 'L');
- uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
- (static_cast<uint64_t>(result[7]) << 060) +
- (static_cast<uint64_t>(result[6]) << 050) +
- (static_cast<uint64_t>(result[5]) << 040) +
- (static_cast<uint64_t>(result[4]) << 030) +
- (static_cast<uint64_t>(result[3]) << 020) +
- (static_cast<uint64_t>(result[2]) << 010) +
- static_cast<uint64_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("9223372036854775808..18446744073709551615 (uint64)")
- {
- std::vector<uint64_t> const v = {9223372036854775808ull, 18446744073709551615ull};
- for (const uint64_t i : v)
- {
- CAPTURE(i)
- // create JSON value with integer number
- json const j = i;
- // check type
- CHECK(j.is_number_unsigned());
- // create expected byte vector
- std::vector<uint8_t> const expected
- {
- 'M',
- static_cast<uint8_t>(i & 0xff),
- static_cast<uint8_t>((i >> 010) & 0xff),
- static_cast<uint8_t>((i >> 020) & 0xff),
- static_cast<uint8_t>((i >> 030) & 0xff),
- static_cast<uint8_t>((i >> 040) & 0xff),
- static_cast<uint8_t>((i >> 050) & 0xff),
- static_cast<uint8_t>((i >> 060) & 0xff),
- static_cast<uint8_t>((i >> 070) & 0xff),
- };
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == 9);
- // check individual bytes
- CHECK(result[0] == 'M');
- uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
- (static_cast<uint64_t>(result[7]) << 060) +
- (static_cast<uint64_t>(result[6]) << 050) +
- (static_cast<uint64_t>(result[5]) << 040) +
- (static_cast<uint64_t>(result[4]) << 030) +
- (static_cast<uint64_t>(result[3]) << 020) +
- (static_cast<uint64_t>(result[2]) << 010) +
- static_cast<uint64_t>(result[1]);
- CHECK(restored == i);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- }
- SECTION("float64")
- {
- SECTION("3.1415925")
- {
- double v = 3.1415925;
- json const j = v;
- std::vector<uint8_t> const expected =
- {
- 'D', 0xfc, 0xde, 0xa6, 0x3f, 0xfb, 0x21, 0x09, 0x40
- };
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result) == v);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("half-precision float")
- {
- SECTION("simple half floats")
- {
- CHECK(json::parse("0.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x00})));
- CHECK(json::parse("-0.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x80})));
- CHECK(json::parse("1.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3c})));
- CHECK(json::parse("1.5") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3e})));
- CHECK(json::parse("65504.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0xff, 0x7b})));
- }
- SECTION("errors")
- {
- SECTION("no byte follows")
- {
- json _;
- std::vector<uint8_t> const vec0 = {'h'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec0), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vec0, true, false).is_discarded());
- }
- SECTION("only one byte follows")
- {
- json _;
- std::vector<uint8_t> const vec1 = {'h', 0x00};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec1), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vec1, true, false).is_discarded());
- }
- }
- }
- SECTION("half-precision float (edge cases)")
- {
- SECTION("exp = 0b00000")
- {
- SECTION("0 (0 00000 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x00}));
- json::number_float_t d{j};
- CHECK(d == 0.0);
- }
- SECTION("-0 (1 00000 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x80}));
- json::number_float_t d{j};
- CHECK(d == -0.0);
- }
- SECTION("2**-24 (0 00000 0000000001)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x01, 0x00}));
- json::number_float_t d{j};
- CHECK(d == std::pow(2.0, -24.0));
- }
- }
- SECTION("exp = 0b11111")
- {
- SECTION("infinity (0 11111 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7c}));
- json::number_float_t d{j};
- CHECK(d == std::numeric_limits<json::number_float_t>::infinity());
- CHECK(j.dump() == "null");
- }
- SECTION("-infinity (1 11111 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0xfc}));
- json::number_float_t d{j};
- CHECK(d == -std::numeric_limits<json::number_float_t>::infinity());
- CHECK(j.dump() == "null");
- }
- }
- SECTION("other values from https://en.wikipedia.org/wiki/Half-precision_floating-point_format")
- {
- SECTION("1 (0 01111 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3c}));
- json::number_float_t d{j};
- CHECK(d == 1);
- }
- SECTION("-2 (1 10000 0000000000)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0xc0}));
- json::number_float_t d{j};
- CHECK(d == -2);
- }
- SECTION("65504 (0 11110 1111111111)")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0xff, 0x7b}));
- json::number_float_t d{j};
- CHECK(d == 65504);
- }
- }
- SECTION("infinity")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7c}));
- json::number_float_t const d{j};
- CHECK_FALSE(std::isfinite(d));
- CHECK(j.dump() == "null");
- }
- SECTION("NaN")
- {
- json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7e }));
- json::number_float_t const d{j};
- CHECK(std::isnan(d));
- CHECK(j.dump() == "null");
- }
- }
- SECTION("high-precision number")
- {
- SECTION("unsigned integer number")
- {
- std::vector<uint8_t> const vec = {'H', 'i', 0x14, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
- const auto j = json::from_bjdata(vec);
- CHECK(j.is_number_unsigned());
- CHECK(j.dump() == "12345678901234567890");
- }
- SECTION("signed integer number")
- {
- std::vector<uint8_t> const vec = {'H', 'i', 0x13, '-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8'};
- const auto j = json::from_bjdata(vec);
- CHECK(j.is_number_integer());
- CHECK(j.dump() == "-123456789012345678");
- }
- SECTION("floating-point number")
- {
- std::vector<uint8_t> const vec = {'H', 'i', 0x16, '3', '.', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6'};
- const auto j = json::from_bjdata(vec);
- CHECK(j.is_number_float());
- CHECK(j.dump() == "3.141592653589793");
- }
- SECTION("errors")
- {
- // error while parsing length
- std::vector<uint8_t> const vec0 = {'H', 'i'};
- CHECK(json::from_bjdata(vec0, true, false).is_discarded());
- // error while parsing string
- std::vector<uint8_t> const vec1 = {'H', 'i', '1'};
- CHECK(json::from_bjdata(vec1, true, false).is_discarded());
- json _;
- std::vector<uint8_t> const vec2 = {'H', 'i', 2, '1', 'A', '3'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1A", json::parse_error);
- std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error);
- std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x02", json::parse_error);
- }
- }
- }
- SECTION("string")
- {
- SECTION("N = 0..127")
- {
- for (size_t N = 0; N <= 127; ++N)
- {
- CAPTURE(N)
- // create JSON value with string containing of N * 'x'
- const auto s = std::string(N, 'x');
- json const j = s;
- // create expected byte vector
- std::vector<uint8_t> expected;
- expected.push_back('S');
- expected.push_back('i');
- expected.push_back(static_cast<uint8_t>(N));
- for (size_t i = 0; i < N; ++i)
- {
- expected.push_back('x');
- }
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == N + 3);
- // check that no null byte is appended
- if (N > 0)
- {
- CHECK(result.back() != '\x00');
- }
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("N = 128..255")
- {
- for (size_t N = 128; N <= 255; ++N)
- {
- CAPTURE(N)
- // create JSON value with string containing of N * 'x'
- const auto s = std::string(N, 'x');
- json const j = s;
- // create expected byte vector
- std::vector<uint8_t> expected;
- expected.push_back('S');
- expected.push_back('U');
- expected.push_back(static_cast<uint8_t>(N));
- for (size_t i = 0; i < N; ++i)
- {
- expected.push_back('x');
- }
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == N + 3);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("N = 256..32767")
- {
- for (const size_t N :
- {
- 256u, 999u, 1025u, 3333u, 2048u, 32767u
- })
- {
- CAPTURE(N)
- // create JSON value with string containing of N * 'x'
- const auto s = std::string(N, 'x');
- json const j = s;
- // create expected byte vector (hack: create string first)
- std::vector<uint8_t> expected(N, 'x');
- // reverse order of commands, because we insert at begin()
- expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
- expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
- expected.insert(expected.begin(), 'I');
- expected.insert(expected.begin(), 'S');
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == N + 4);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("N = 32768..65535")
- {
- for (const size_t N :
- {
- 32768u, 55555u, 65535u
- })
- {
- CAPTURE(N)
- // create JSON value with string containing of N * 'x'
- const auto s = std::string(N, 'x');
- json const j = s;
- // create expected byte vector (hack: create string first)
- std::vector<uint8_t> expected(N, 'x');
- // reverse order of commands, because we insert at begin()
- expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
- expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
- expected.insert(expected.begin(), 'u');
- expected.insert(expected.begin(), 'S');
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == N + 4);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("N = 65536..2147483647")
- {
- for (const size_t N :
- {
- 65536u, 77777u, 1048576u
- })
- {
- CAPTURE(N)
- // create JSON value with string containing of N * 'x'
- const auto s = std::string(N, 'x');
- json const j = s;
- // create expected byte vector (hack: create string first)
- std::vector<uint8_t> expected(N, 'x');
- // reverse order of commands, because we insert at begin()
- expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
- expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
- expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
- expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
- expected.insert(expected.begin(), 'l');
- expected.insert(expected.begin(), 'S');
- // compare result + size
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- CHECK(result.size() == N + 6);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- }
- SECTION("binary")
- {
- SECTION("N = 0..127")
- {
- for (std::size_t N = 0; N <= 127; ++N)
- {
- CAPTURE(N)
- // create JSON value with byte array containing of N * 'x'
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- // create expected byte vector
- std::vector<std::uint8_t> expected;
- expected.push_back(static_cast<std::uint8_t>('['));
- if (N != 0)
- {
- expected.push_back(static_cast<std::uint8_t>('$'));
- expected.push_back(static_cast<std::uint8_t>('U'));
- }
- expected.push_back(static_cast<std::uint8_t>('#'));
- expected.push_back(static_cast<std::uint8_t>('i'));
- expected.push_back(static_cast<std::uint8_t>(N));
- for (size_t i = 0; i < N; ++i)
- {
- expected.push_back(0x78);
- }
- // compare result + size
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- if (N == 0)
- {
- CHECK(result.size() == N + 4);
- }
- else
- {
- CHECK(result.size() == N + 6);
- }
- // check that no null byte is appended
- if (N > 0)
- {
- CHECK(result.back() != '\x00');
- }
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- SECTION("N = 128..255")
- {
- for (std::size_t N = 128; N <= 255; ++N)
- {
- CAPTURE(N)
- // create JSON value with byte array containing of N * 'x'
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- // create expected byte vector
- std::vector<uint8_t> expected;
- expected.push_back(static_cast<std::uint8_t>('['));
- expected.push_back(static_cast<std::uint8_t>('$'));
- expected.push_back(static_cast<std::uint8_t>('U'));
- expected.push_back(static_cast<std::uint8_t>('#'));
- expected.push_back(static_cast<std::uint8_t>('U'));
- expected.push_back(static_cast<std::uint8_t>(N));
- for (size_t i = 0; i < N; ++i)
- {
- expected.push_back(0x78);
- }
- // compare result + size
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- CHECK(result.size() == N + 6);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- SECTION("N = 256..32767")
- {
- for (const std::size_t N :
- {
- 256u, 999u, 1025u, 3333u, 2048u, 32767u
- })
- {
- CAPTURE(N)
- // create JSON value with byte array containing of N * 'x'
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- // create expected byte vector
- std::vector<std::uint8_t> expected(N + 7, 'x');
- expected[0] = '[';
- expected[1] = '$';
- expected[2] = 'U';
- expected[3] = '#';
- expected[4] = 'I';
- expected[5] = static_cast<std::uint8_t>(N & 0xFF);
- expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
- // compare result + size
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- CHECK(result.size() == N + 7);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- SECTION("N = 32768..65535")
- {
- for (const std::size_t N :
- {
- 32768u, 55555u, 65535u
- })
- {
- CAPTURE(N)
- // create JSON value with byte array containing of N * 'x'
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- // create expected byte vector
- std::vector<std::uint8_t> expected(N + 7, 'x');
- expected[0] = '[';
- expected[1] = '$';
- expected[2] = 'U';
- expected[3] = '#';
- expected[4] = 'u';
- expected[5] = static_cast<std::uint8_t>(N & 0xFF);
- expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
- // compare result + size
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- CHECK(result.size() == N + 7);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- SECTION("N = 65536..2147483647")
- {
- for (const std::size_t N :
- {
- 65536u, 77777u, 1048576u
- })
- {
- CAPTURE(N)
- // create JSON value with byte array containing of N * 'x'
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- // create expected byte vector
- std::vector<std::uint8_t> expected(N + 9, 'x');
- expected[0] = '[';
- expected[1] = '$';
- expected[2] = 'U';
- expected[3] = '#';
- expected[4] = 'l';
- expected[5] = static_cast<std::uint8_t>(N & 0xFF);
- expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
- expected[7] = static_cast<std::uint8_t>((N >> 16) & 0xFF);
- expected[8] = static_cast<std::uint8_t>((N >> 24) & 0xFF);
- // compare result + size
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- CHECK(result.size() == N + 9);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- SECTION("Other Serializations")
- {
- const std::size_t N = 10;
- const auto s = std::vector<std::uint8_t>(N, 'x');
- json const j = json::binary(s);
- SECTION("No Count No Type")
- {
- std::vector<uint8_t> expected;
- expected.push_back(static_cast<std::uint8_t>('['));
- for (std::size_t i = 0; i < N; ++i)
- {
- expected.push_back(static_cast<std::uint8_t>('U'));
- expected.push_back(static_cast<std::uint8_t>(0x78));
- }
- expected.push_back(static_cast<std::uint8_t>(']'));
- // compare result + size
- const auto result = json::to_bjdata(j, false, false);
- CHECK(result == expected);
- CHECK(result.size() == N + 12);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- SECTION("Yes Count No Type")
- {
- std::vector<std::uint8_t> expected;
- expected.push_back(static_cast<std::uint8_t>('['));
- expected.push_back(static_cast<std::uint8_t>('#'));
- expected.push_back(static_cast<std::uint8_t>('i'));
- expected.push_back(static_cast<std::uint8_t>(N));
- for (size_t i = 0; i < N; ++i)
- {
- expected.push_back(static_cast<std::uint8_t>('U'));
- expected.push_back(static_cast<std::uint8_t>(0x78));
- }
- // compare result + size
- const auto result = json::to_bjdata(j, true, false);
- CHECK(result == expected);
- CHECK(result.size() == N + 14);
- // check that no null byte is appended
- CHECK(result.back() != '\x00');
- // roundtrip only works to an array of numbers
- json j_out = s;
- CHECK(json::from_bjdata(result) == j_out);
- CHECK(json::from_bjdata(result, true, false) == j_out);
- }
- }
- }
- SECTION("array")
- {
- SECTION("empty")
- {
- SECTION("size=false type=false")
- {
- json const j = json::array();
- std::vector<uint8_t> const expected = {'[', ']'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = json::array();
- std::vector<uint8_t> const expected = {'[', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true")
- {
- json const j = json::array();
- std::vector<uint8_t> const expected = {'[', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("[null]")
- {
- SECTION("size=false type=false")
- {
- json const j = {nullptr};
- std::vector<uint8_t> const expected = {'[', 'Z', ']'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = {nullptr};
- std::vector<uint8_t> const expected = {'[', '#', 'i', 1, 'Z'};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true")
- {
- json const j = {nullptr};
- std::vector<uint8_t> const expected = {'[', '#', 'i', 1, 'Z'};
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("[1,2,3,4,5]")
- {
- SECTION("size=false type=false")
- {
- json const j = json::parse("[1,2,3,4,5]");
- std::vector<uint8_t> const expected = {'[', 'i', 1, 'i', 2, 'i', 3, 'i', 4, 'i', 5, ']'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = json::parse("[1,2,3,4,5]");
- std::vector<uint8_t> const expected = {'[', '#', 'i', 5, 'i', 1, 'i', 2, 'i', 3, 'i', 4, 'i', 5};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true")
- {
- json const j = json::parse("[1,2,3,4,5]");
- std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 5, 1, 2, 3, 4, 5};
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("[[[[]]]]")
- {
- SECTION("size=false type=false")
- {
- json const j = json::parse("[[[[]]]]");
- std::vector<uint8_t> const expected = {'[', '[', '[', '[', ']', ']', ']', ']'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = json::parse("[[[[]]]]");
- std::vector<uint8_t> const expected = {'[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true")
- {
- json const j = json::parse("[[[[]]]]");
- std::vector<uint8_t> const expected = {'[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("array with int16_t elements")
- {
- SECTION("size=false type=false")
- {
- json j(257, nullptr);
- std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[258] = ']'; // closing array
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json j(257, nullptr);
- std::vector<uint8_t> expected(j.size() + 5, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[1] = '#'; // array size
- expected[2] = 'I'; // int16
- expected[3] = 0x01; // 0x0101, first byte
- expected[4] = 0x01; // 0x0101, second byte
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("array with uint16_t elements")
- {
- SECTION("size=false type=false")
- {
- json j(32768, nullptr);
- std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[32769] = ']'; // closing array
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json j(32768, nullptr);
- std::vector<uint8_t> expected(j.size() + 5, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[1] = '#'; // array size
- expected[2] = 'u'; // int16
- expected[3] = 0x00; // 0x0101, first byte
- expected[4] = 0x80; // 0x0101, second byte
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("array with int32_t elements")
- {
- SECTION("size=false type=false")
- {
- json j(65793, nullptr);
- std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[65794] = ']'; // closing array
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json j(65793, nullptr);
- std::vector<uint8_t> expected(j.size() + 7, 'Z'); // all null
- expected[0] = '['; // opening array
- expected[1] = '#'; // array size
- expected[2] = 'l'; // int32
- expected[3] = 0x01; // 0x00010101, fourth byte
- expected[4] = 0x01; // 0x00010101, third byte
- expected[5] = 0x01; // 0x00010101, second byte
- expected[6] = 0x00; // 0x00010101, first byte
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- }
- SECTION("object")
- {
- SECTION("empty")
- {
- SECTION("size=false type=false")
- {
- json const j = json::object();
- std::vector<uint8_t> const expected = {'{', '}'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = json::object();
- std::vector<uint8_t> const expected = {'{', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true")
- {
- json const j = json::object();
- std::vector<uint8_t> const expected = {'{', '#', 'i', 0};
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("{\"\":null}")
- {
- SECTION("size=false type=false")
- {
- json const j = {{"", nullptr}};
- std::vector<uint8_t> const expected = {'{', 'i', 0, 'Z', '}'};
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = {{"", nullptr}};
- std::vector<uint8_t> const expected = {'{', '#', 'i', 1, 'i', 0, 'Z'};
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
- {
- SECTION("size=false type=false")
- {
- json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
- std::vector<uint8_t> const expected =
- {
- '{', 'i', 1, 'a', '{', 'i', 1, 'b', '{', 'i', 1, 'c', '{', '}', '}', '}', '}'
- };
- const auto result = json::to_bjdata(j);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=false")
- {
- json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
- std::vector<uint8_t> const expected =
- {
- '{', '#', 'i', 1, 'i', 1, 'a', '{', '#', 'i', 1, 'i', 1, 'b', '{', '#', 'i', 1, 'i', 1, 'c', '{', '#', 'i', 0
- };
- const auto result = json::to_bjdata(j, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- SECTION("size=true type=true ignore object type marker")
- {
- json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
- std::vector<uint8_t> const expected =
- {
- '{', '#', 'i', 1, 'i', 1, 'a', '{', '#', 'i', 1, 'i', 1, 'b', '{', '#', 'i', 1, 'i', 1, 'c', '{', '#', 'i', 0
- };
- const auto result = json::to_bjdata(j, true, true);
- CHECK(result == expected);
- // roundtrip
- CHECK(json::from_bjdata(result) == j);
- CHECK(json::from_bjdata(result, true, false) == j);
- }
- }
- }
- }
- SECTION("errors")
- {
- SECTION("strict mode")
- {
- std::vector<uint8_t> const vec = {'Z', 'Z'};
- SECTION("non-strict mode")
- {
- const auto result = json::from_bjdata(vec, false);
- CHECK(result == json());
- }
- SECTION("strict mode")
- {
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec),
- "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData value: expected end of input; last byte: 0x5A", json::parse_error&);
- }
- }
- }
- SECTION("SAX aborts")
- {
- SECTION("start_array()")
- {
- std::vector<uint8_t> const v = {'[', 'T', 'F', ']'};
- SaxCountdown scp(0);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("start_object()")
- {
- std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
- SaxCountdown scp(0);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("key() in object")
- {
- std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
- SaxCountdown scp(1);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("start_array(len)")
- {
- std::vector<uint8_t> const v = {'[', '#', 'i', '2', 'T', 'F'};
- SaxCountdown scp(0);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("start_object(len)")
- {
- std::vector<uint8_t> const v = {'{', '#', 'i', '1', 3, 'f', 'o', 'o', 'F'};
- SaxCountdown scp(0);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("key() in object with length")
- {
- std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
- SaxCountdown scp(1);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("start_array() in ndarray _ArraySize_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 1, 2};
- SaxCountdown scp(2);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("number_integer() in ndarray _ArraySize_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 1, 2};
- SaxCountdown scp(3);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("key() in ndarray _ArrayType_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
- SaxCountdown scp(6);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("string() in ndarray _ArrayType_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
- SaxCountdown scp(7);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("key() in ndarray _ArrayData_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
- SaxCountdown scp(8);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("string() in ndarray _ArrayData_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
- SaxCountdown scp(9);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("string() in ndarray _ArrayType_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 3, 2, 6, 5, 4, 3, 2, 1};
- SaxCountdown scp(11);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- SECTION("start_array() in ndarray _ArrayData_")
- {
- std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', 'i', 2, 'i', 3, ']', 6, 5, 4, 3, 2, 1};
- SaxCountdown scp(13);
- CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
- }
- }
- SECTION("parsing values")
- {
- SECTION("strings")
- {
- // create a single-character string for all number types
- std::vector<uint8_t> s_i = {'S', 'i', 1, 'a'};
- std::vector<uint8_t> const s_U = {'S', 'U', 1, 'a'};
- std::vector<uint8_t> const s_I = {'S', 'I', 1, 0, 'a'};
- std::vector<uint8_t> const s_u = {'S', 'u', 1, 0, 'a'};
- std::vector<uint8_t> const s_l = {'S', 'l', 1, 0, 0, 0, 'a'};
- std::vector<uint8_t> const s_m = {'S', 'm', 1, 0, 0, 0, 'a'};
- std::vector<uint8_t> const s_L = {'S', 'L', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
- std::vector<uint8_t> const s_M = {'S', 'M', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
- // check if string is parsed correctly to "a"
- CHECK(json::from_bjdata(s_i) == "a");
- CHECK(json::from_bjdata(s_U) == "a");
- CHECK(json::from_bjdata(s_I) == "a");
- CHECK(json::from_bjdata(s_u) == "a");
- CHECK(json::from_bjdata(s_l) == "a");
- CHECK(json::from_bjdata(s_m) == "a");
- CHECK(json::from_bjdata(s_L) == "a");
- CHECK(json::from_bjdata(s_M) == "a");
- // roundtrip: output should be optimized
- CHECK(json::to_bjdata(json::from_bjdata(s_i)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_U)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_I)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_u)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_l)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_m)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_L)) == s_i);
- CHECK(json::to_bjdata(json::from_bjdata(s_M)) == s_i);
- }
- SECTION("number")
- {
- SECTION("float")
- {
- // float32
- std::vector<uint8_t> const v_d = {'d', 0xd0, 0x0f, 0x49, 0x40};
- CHECK(json::from_bjdata(v_d) == 3.14159f);
- // float64
- std::vector<uint8_t> const v_D = {'D', 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40};
- CHECK(json::from_bjdata(v_D) == 3.14159);
- // float32 is serialized as float64 as the library does not support float32
- CHECK(json::to_bjdata(json::from_bjdata(v_d)) == json::to_bjdata(3.14159f));
- }
- }
- SECTION("array")
- {
- SECTION("optimized version (length only)")
- {
- // create vector with two elements of the same type
- std::vector<uint8_t> const v_TU = {'[', '#', 'U', 2, 'T', 'T'};
- std::vector<uint8_t> const v_T = {'[', '#', 'i', 2, 'T', 'T'};
- std::vector<uint8_t> const v_F = {'[', '#', 'i', 2, 'F', 'F'};
- std::vector<uint8_t> const v_Z = {'[', '#', 'i', 2, 'Z', 'Z'};
- std::vector<uint8_t> const v_i = {'[', '#', 'i', 2, 'i', 0x7F, 'i', 0x7F};
- std::vector<uint8_t> const v_U = {'[', '#', 'i', 2, 'U', 0xFF, 'U', 0xFF};
- std::vector<uint8_t> const v_I = {'[', '#', 'i', 2, 'I', 0xFF, 0x7F, 'I', 0xFF, 0x7F};
- std::vector<uint8_t> const v_u = {'[', '#', 'i', 2, 'u', 0x0F, 0xA7, 'u', 0x0F, 0xA7};
- std::vector<uint8_t> const v_l = {'[', '#', 'i', 2, 'l', 0xFF, 0xFF, 0xFF, 0x7F, 'l', 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_m = {'[', '#', 'i', 2, 'm', 0xFF, 0xC9, 0x9A, 0xBB, 'm', 0xFF, 0xC9, 0x9A, 0xBB};
- std::vector<uint8_t> const v_L = {'[', '#', 'i', 2, 'L', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 'L', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_M = {'[', '#', 'i', 2, 'M', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 'M', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const v_D = {'[', '#', 'i', 2, 'D', 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40, 'D', 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40};
- std::vector<uint8_t> const v_S = {'[', '#', 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
- std::vector<uint8_t> const v_C = {'[', '#', 'i', 2, 'C', 'a', 'C', 'a'};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_TU) == json({true, true}));
- CHECK(json::from_bjdata(v_T) == json({true, true}));
- CHECK(json::from_bjdata(v_F) == json({false, false}));
- CHECK(json::from_bjdata(v_Z) == json({nullptr, nullptr}));
- CHECK(json::from_bjdata(v_i) == json({127, 127}));
- CHECK(json::from_bjdata(v_U) == json({255, 255}));
- CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
- CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
- CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
- CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
- CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
- CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
- CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
- CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
- // roundtrip: output should be optimized
- CHECK(json::to_bjdata(json::from_bjdata(v_T), true) == v_T);
- CHECK(json::to_bjdata(json::from_bjdata(v_F), true) == v_F);
- CHECK(json::to_bjdata(json::from_bjdata(v_Z), true) == v_Z);
- CHECK(json::to_bjdata(json::from_bjdata(v_i), true) == v_i);
- CHECK(json::to_bjdata(json::from_bjdata(v_U), true) == v_U);
- CHECK(json::to_bjdata(json::from_bjdata(v_I), true) == v_I);
- CHECK(json::to_bjdata(json::from_bjdata(v_u), true) == v_u);
- CHECK(json::to_bjdata(json::from_bjdata(v_l), true) == v_l);
- CHECK(json::to_bjdata(json::from_bjdata(v_m), true) == v_m);
- CHECK(json::to_bjdata(json::from_bjdata(v_L), true) == v_L);
- CHECK(json::to_bjdata(json::from_bjdata(v_M), true) == v_M);
- CHECK(json::to_bjdata(json::from_bjdata(v_D), true) == v_D);
- CHECK(json::to_bjdata(json::from_bjdata(v_S), true) == v_S);
- CHECK(json::to_bjdata(json::from_bjdata(v_C), true) == v_S); // char is serialized to string
- }
- SECTION("optimized version (type and length)")
- {
- // create vector with two elements of the same type
- std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', 'i', 2, 0x7F, 0x7F};
- std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', 'i', 2, 0xFF, 0xFF};
- std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', 'i', 2, 0xFF, 0x7F, 0xFF, 0x7F};
- std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', 'i', 2, 0x0F, 0xA7, 0x0F, 0xA7};
- std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', 'i', 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
- std::vector<uint8_t> const v_L = {'[', '$', 'L', '#', 'i', 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_M = {'[', '$', 'M', '#', 'i', 2, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const v_D = {'[', '$', 'D', '#', 'i', 2, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40};
- std::vector<uint8_t> const v_S = {'[', '#', 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
- std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', 'i', 2, 'a', 'a'};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_i) == json({127, 127}));
- CHECK(json::from_bjdata(v_U) == json({255, 255}));
- CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
- CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
- CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
- CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
- CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
- CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
- CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
- CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
- // roundtrip: output should be optimized
- std::vector<uint8_t> const v_empty = {'[', '#', 'i', 0};
- CHECK(json::to_bjdata(json::from_bjdata(v_i), true, true) == v_i);
- CHECK(json::to_bjdata(json::from_bjdata(v_U), true, true) == v_U);
- CHECK(json::to_bjdata(json::from_bjdata(v_I), true, true) == v_I);
- CHECK(json::to_bjdata(json::from_bjdata(v_u), true, true) == v_u);
- CHECK(json::to_bjdata(json::from_bjdata(v_l), true, true) == v_l);
- CHECK(json::to_bjdata(json::from_bjdata(v_m), true, true) == v_m);
- CHECK(json::to_bjdata(json::from_bjdata(v_L), true, true) == v_L);
- CHECK(json::to_bjdata(json::from_bjdata(v_M), true, true) == v_M);
- CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
- CHECK(json::to_bjdata(json::from_bjdata(v_S), true, true) == v_S);
- CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_S); // char is serialized to string
- }
- SECTION("optimized ndarray (type and vector-size as optimized 1D array)")
- {
- // create vector with two elements of the same type
- std::vector<uint8_t> const v_0 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 1, 0};
- std::vector<uint8_t> const v_1 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 1, 2, 0x7F, 0x7F};
- std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0x7F, 0x7F};
- std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF};
- std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0x7F, 0xFF, 0x7F};
- std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0x0F, 0xA7, 0x0F, 0xA7};
- std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
- std::vector<uint8_t> const v_L = {'[', '$', 'L', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_M = {'[', '$', 'M', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const v_D = {'[', '$', 'D', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40};
- std::vector<uint8_t> const v_S = {'[', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
- std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 'a', 'a'};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_0) == json::array());
- CHECK(json::from_bjdata(v_1) == json({127, 127}));
- CHECK(json::from_bjdata(v_i) == json({127, 127}));
- CHECK(json::from_bjdata(v_U) == json({255, 255}));
- CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
- CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
- CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
- CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
- CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
- CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
- CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
- CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
- }
- SECTION("optimized ndarray (type and vector-size ndarray with JData annotations)")
- {
- // create vector with 0, 1, 2 elements of the same type
- std::vector<uint8_t> const v_e = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 0xFE, 0xFF};
- std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
- std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
- std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00};
- std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00};
- std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00};
- std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00};
- std::vector<uint8_t> const v_M = {'[', '$', 'M', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- std::vector<uint8_t> const v_L = {'[', '$', 'L', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- std::vector<uint8_t> const v_d = {'[', '$', 'd', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0xA0, 0x40, 0x00, 0x00, 0xC0, 0x40};
- std::vector<uint8_t> const v_D = {'[', '$', 'D', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x40};
- std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 'a', 'b', 'c', 'd', 'e', 'f'};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_e) == json({{"_ArrayData_", {254, 255}}, {"_ArraySize_", {2, 1}}, {"_ArrayType_", "uint8"}}));
- CHECK(json::from_bjdata(v_U) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint8"}}));
- CHECK(json::from_bjdata(v_i) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int8"}}));
- CHECK(json::from_bjdata(v_i) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int8"}}));
- CHECK(json::from_bjdata(v_u) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint16"}}));
- CHECK(json::from_bjdata(v_I) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int16"}}));
- CHECK(json::from_bjdata(v_m) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint32"}}));
- CHECK(json::from_bjdata(v_l) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int32"}}));
- CHECK(json::from_bjdata(v_M) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint64"}}));
- CHECK(json::from_bjdata(v_L) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int64"}}));
- CHECK(json::from_bjdata(v_d) == json({{"_ArrayData_", {1.f, 2.f, 3.f, 4.f, 5.f, 6.f}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "single"}}));
- CHECK(json::from_bjdata(v_D) == json({{"_ArrayData_", {1., 2., 3., 4., 5., 6.}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "double"}}));
- CHECK(json::from_bjdata(v_C) == json({{"_ArrayData_", {'a', 'b', 'c', 'd', 'e', 'f'}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "char"}}));
- // roundtrip: output should be optimized
- CHECK(json::to_bjdata(json::from_bjdata(v_e), true, true) == v_e);
- CHECK(json::to_bjdata(json::from_bjdata(v_U), true, true) == v_U);
- CHECK(json::to_bjdata(json::from_bjdata(v_i), true, true) == v_i);
- CHECK(json::to_bjdata(json::from_bjdata(v_u), true, true) == v_u);
- CHECK(json::to_bjdata(json::from_bjdata(v_I), true, true) == v_I);
- CHECK(json::to_bjdata(json::from_bjdata(v_m), true, true) == v_m);
- CHECK(json::to_bjdata(json::from_bjdata(v_l), true, true) == v_l);
- CHECK(json::to_bjdata(json::from_bjdata(v_M), true, true) == v_M);
- CHECK(json::to_bjdata(json::from_bjdata(v_L), true, true) == v_L);
- CHECK(json::to_bjdata(json::from_bjdata(v_d), true, true) == v_d);
- CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
- CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_C);
- }
- SECTION("optimized ndarray (type and vector-size as 1D array)")
- {
- // create vector with two elements of the same type
- std::vector<uint8_t> const v_0 = {'[', '$', 'i', '#', '[', ']'};
- std::vector<uint8_t> const v_E = {'[', '$', 'i', '#', '[', 'i', 2, 'i', 0, ']'};
- std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', 'i', 1, 'i', 2, ']', 0x7F, 0x7F};
- std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF};
- std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0x7F, 0xFF, 0x7F};
- std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', 'i', 1, 'i', 2, ']', 0x0F, 0xA7, 0x0F, 0xA7};
- std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
- std::vector<uint8_t> const v_L = {'[', '$', 'L', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_M = {'[', '$', 'M', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const v_D = {'[', '$', 'D', '#', '[', 'i', 1, 'i', 2, ']', 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40};
- std::vector<uint8_t> const v_S = {'[', '#', '[', 'i', 1, 'i', 2, ']', 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
- std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', 'i', 1, 'i', 2, ']', 'a', 'a'};
- std::vector<uint8_t> const v_R = {'[', '#', '[', 'i', 2, ']', 'i', 6, 'U', 7};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_0) == json::array());
- CHECK(json::from_bjdata(v_E) == json::array());
- CHECK(json::from_bjdata(v_i) == json({127, 127}));
- CHECK(json::from_bjdata(v_U) == json({255, 255}));
- CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
- CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
- CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
- CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
- CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
- CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
- CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
- CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_R) == json({6, 7}));
- }
- SECTION("optimized ndarray (type and vector-size as size-optimized array)")
- {
- // create vector with two elements of the same type
- std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0x7F, 0x7F};
- std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF};
- std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0x7F, 0xFF, 0x7F};
- std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0x0F, 0xA7, 0x0F, 0xA7};
- std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
- std::vector<uint8_t> const v_L = {'[', '$', 'L', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
- std::vector<uint8_t> const v_M = {'[', '$', 'M', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const v_D = {'[', '$', 'D', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40, 0x4a, 0xd8, 0x12, 0x4d, 0xfb, 0x21, 0x09, 0x40};
- std::vector<uint8_t> const v_S = {'[', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
- std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 'a', 'a'};
- // check if vector is parsed correctly
- CHECK(json::from_bjdata(v_i) == json({127, 127}));
- CHECK(json::from_bjdata(v_U) == json({255, 255}));
- CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
- CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
- CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
- CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
- CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
- CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
- CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
- CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
- CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
- }
- SECTION("invalid ndarray annotations remains as object")
- {
- // check if invalid ND array annotations stay as object
- json j_type = json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "invalidtype"}});
- json j_size = json({{"_ArrayData_", {1, 2, 3, 4, 5}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint8"}});
- // roundtrip: output should stay as object
- CHECK(json::from_bjdata(json::to_bjdata(j_type), true, true) == j_type);
- CHECK(json::from_bjdata(json::to_bjdata(j_size), true, true) == j_size);
- }
- }
- }
- SECTION("parse errors")
- {
- SECTION("empty byte vector")
- {
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(std::vector<uint8_t>()),
- "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- }
- SECTION("char")
- {
- SECTION("eof after C byte")
- {
- std::vector<uint8_t> const v = {'C'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData char: unexpected end of input", json::parse_error&);
- }
- SECTION("byte out of range")
- {
- std::vector<uint8_t> const v = {'C', 130};
- json _;
- CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82");
- }
- }
- SECTION("strings")
- {
- SECTION("eof after S byte")
- {
- std::vector<uint8_t> const v = {'S'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- }
- SECTION("invalid byte")
- {
- std::vector<uint8_t> const v = {'S', '1', 'a'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData string: expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x31", json::parse_error&);
- }
- SECTION("parse bjdata markers in ubjson")
- {
- // create a single-character string for all number types
- std::vector<uint8_t> const s_u = {'S', 'u', 1, 0, 'a'};
- std::vector<uint8_t> const s_m = {'S', 'm', 1, 0, 0, 0, 'a'};
- std::vector<uint8_t> const s_M = {'S', 'M', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
- json _;
- // check if string is parsed correctly to "a"
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(s_u), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x75", json::parse_error&);
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(s_m), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x6D", json::parse_error&);
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(s_M), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x4D", json::parse_error&);
- }
- }
- SECTION("array")
- {
- SECTION("optimized array: no size following type")
- {
- std::vector<uint8_t> const v = {'[', '$', 'i', 2};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x02", json::parse_error&);
- }
- SECTION("optimized array: negative size")
- {
- std::vector<uint8_t> const v1 = {'[', '#', 'i', 0xF1};
- std::vector<uint8_t> const v2 = {'[', '$', 'I', '#', 'i', 0xF2};
- std::vector<uint8_t> const v3 = {'[', '$', 'I', '#', '[', 'i', 0xF4, 'i', 0x02, ']'};
- std::vector<uint8_t> const v4 = {'[', '$', 0xF6, '#', 'i', 0xF7};
- std::vector<uint8_t> const v5 = {'[', '$', 'I', '#', '[', 'i', 0xF5, 'i', 0xF1, ']'};
- std::vector<uint8_t> const v6 = {'[', '#', '[', 'i', 0xF3, 'i', 0x02, ']'};
- std::vector<uint8_t> const vI = {'[', '#', 'I', 0x00, 0xF1};
- std::vector<uint8_t> const vl = {'[', '#', 'l', 0x00, 0x00, 0x00, 0xF2};
- std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3};
- std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
- std::vector<uint8_t> const vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v1), "[json.exception.parse_error.113] parse error at byte 4: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v1, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v2), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v2, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v3), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v3, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v4), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v4, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v5), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v5, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v6), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(v6, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vI), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(vI, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vl), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(vl, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.parse_error.113] parse error at byte 11: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
- CHECK(json::from_bjdata(vL, true, false).is_discarded());
- #if SIZE_MAX != 0xffffffff
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: excessive ndarray size caused overflow", json::out_of_range&);
- #else
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
- #endif
- CHECK(json::from_bjdata(vM, true, false).is_discarded());
- #if SIZE_MAX != 0xffffffff
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: excessive ndarray size caused overflow", json::out_of_range&);
- #else
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
- #endif
- CHECK(json::from_bjdata(vMX, true, false).is_discarded());
- }
- SECTION("optimized array: integer value overflow")
- {
- #if SIZE_MAX == 0xffffffff
- std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
- std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
- CHECK(json::from_bjdata(vL, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
- CHECK(json::from_bjdata(vM, true, false).is_discarded());
- #endif
- }
- SECTION("do not accept NTFZ markers in ndarray optimized type (with count)")
- {
- json _;
- std::vector<uint8_t> const v_N = {'[', '$', 'N', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
- std::vector<uint8_t> const v_T = {'[', '$', 'T', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
- std::vector<uint8_t> const v_F = {'[', '$', 'F', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
- std::vector<uint8_t> const v_Z = {'[', '$', 'Z', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_N), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x4E is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_N, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_T), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x54 is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_T, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_F), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x46 is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_F, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_Z), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x5A is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_Z, true, false).is_discarded());
- }
- SECTION("do not accept NTFZ markers in ndarray optimized type (without count)")
- {
- json _;
- std::vector<uint8_t> const v_N = {'[', '$', 'N', '#', '[', 'i', 1, 'i', 2, ']'};
- std::vector<uint8_t> const v_T = {'[', '$', 'T', '#', '[', 'i', 1, 'i', 2, ']'};
- std::vector<uint8_t> const v_F = {'[', '$', 'F', '#', '[', 'i', 1, 'i', 2, ']'};
- std::vector<uint8_t> const v_Z = {'[', '$', 'Z', '#', '[', 'i', 1, 'i', 2, ']'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_N), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x4E is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_N, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_T), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x54 is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_T, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_F), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x46 is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_F, true, false).is_discarded());
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v_Z), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x5A is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v_Z, true, false).is_discarded());
- }
- }
- SECTION("strings")
- {
- std::vector<uint8_t> const vS = {'S'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vS), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vS, true, false).is_discarded());
- std::vector<uint8_t> const v = {'S', 'i', '2', 'a'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing BJData string: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- std::vector<uint8_t> const vC = {'C'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vC), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing BJData char: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vC, true, false).is_discarded());
- }
- SECTION("sizes")
- {
- std::vector<uint8_t> const vU = {'[', '#', 'U'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vU), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vU, true, false).is_discarded());
- std::vector<uint8_t> const vi = {'[', '#', 'i'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vi), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vi, true, false).is_discarded());
- std::vector<uint8_t> const vI = {'[', '#', 'I'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vI), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vI, true, false).is_discarded());
- std::vector<uint8_t> const vu = {'[', '#', 'u'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vu), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vu, true, false).is_discarded());
- std::vector<uint8_t> const vl = {'[', '#', 'l'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vl), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vl, true, false).is_discarded());
- std::vector<uint8_t> const vm = {'[', '#', 'm'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vm), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vm, true, false).is_discarded());
- std::vector<uint8_t> const vL = {'[', '#', 'L'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vL, true, false).is_discarded());
- std::vector<uint8_t> const vM = {'[', '#', 'M'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vM, true, false).is_discarded());
- std::vector<uint8_t> const v0 = {'[', '#', 'T', ']'};
- CHECK_THROWS_WITH(_ = json::from_bjdata(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x54");
- CHECK(json::from_bjdata(v0, true, false).is_discarded());
- }
- SECTION("parse bjdata markers as array size in ubjson")
- {
- json _;
- std::vector<uint8_t> const vu = {'[', '#', 'u'};
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vu), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x75", json::parse_error&);
- CHECK(json::from_ubjson(vu, true, false).is_discarded());
- std::vector<uint8_t> const vm = {'[', '#', 'm'};
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vm), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x6D", json::parse_error&);
- CHECK(json::from_ubjson(vm, true, false).is_discarded());
- std::vector<uint8_t> const vM = {'[', '#', 'M'};
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vM), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x4D", json::parse_error&);
- CHECK(json::from_ubjson(vM, true, false).is_discarded());
- std::vector<uint8_t> const v0 = {'[', '#', '['};
- CHECK_THROWS_WITH_AS(_ = json::from_ubjson(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x5B", json::parse_error&);
- CHECK(json::from_ubjson(v0, true, false).is_discarded());
- }
- SECTION("types")
- {
- std::vector<uint8_t> const v0 = {'[', '$'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v0), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BJData type: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v0, true, false).is_discarded());
- std::vector<uint8_t> const vi = {'[', '$', '#'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vi), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vi, true, false).is_discarded());
- std::vector<uint8_t> const vU = {'[', '$', 'U'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vU), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vU, true, false).is_discarded());
- std::vector<uint8_t> const v1 = {'[', '$', '['};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v1), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x5B is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v1, true, false).is_discarded());
- }
- SECTION("arrays")
- {
- std::vector<uint8_t> const vST = {'[', '$', 'i', '#', 'i', 2, 1};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vST), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vST, true, false).is_discarded());
- std::vector<uint8_t> const vS = {'[', '#', 'i', 2, 'i', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vS), "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vS, true, false).is_discarded());
- std::vector<uint8_t> const v = {'[', 'i', 2, 'i', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- }
- SECTION("ndarrays")
- {
- std::vector<uint8_t> const vST = {'[', '$', 'i', '#', '[', '$', 'i', '#'};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vST), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0xFF", json::parse_error&);
- CHECK(json::from_bjdata(vST, true, false).is_discarded());
- std::vector<uint8_t> const v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 13: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- std::vector<uint8_t> const vS0 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vS0), "[json.exception.parse_error.110] parse error at byte 12: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vS0, true, false).is_discarded());
- std::vector<uint8_t> const vS = {'[', '$', 'i', '#', '[', '#', 'i', 2, 1, 2, 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vS), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x01", json::parse_error&);
- CHECK(json::from_bjdata(vS, true, false).is_discarded());
- std::vector<uint8_t> const vT = {'[', '$', 'i', '#', '[', 'i', 2, 'i'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vT), "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vT, true, false).is_discarded());
- std::vector<uint8_t> const vT0 = {'[', '$', 'i', '#', '[', 'i'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vT0), "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vT0, true, false).is_discarded());
- std::vector<uint8_t> const vu = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'u', 1, 0};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vu), "[json.exception.parse_error.110] parse error at byte 12: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vu, true, false).is_discarded());
- std::vector<uint8_t> const vm = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'm', 1, 0, 0, 0};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vm), "[json.exception.parse_error.110] parse error at byte 14: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vm, true, false).is_discarded());
- std::vector<uint8_t> const vM = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'M', 1, 0, 0, 0, 0, 0, 0, 0};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.parse_error.110] parse error at byte 18: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vM, true, false).is_discarded());
- std::vector<uint8_t> const vU = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 1, 2, 3, 4, 5};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vU), "[json.exception.parse_error.110] parse error at byte 18: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vU, true, false).is_discarded());
- std::vector<uint8_t> const vT1 = {'[', '$', 'T', '#', '[', '$', 'i', '#', 'i', 2, 2, 3};
- CHECK(json::from_bjdata(vT1, true, false).is_discarded());
- std::vector<uint8_t> const vh = {'[', '$', 'h', '#', '[', '$', 'i', '#', 'i', 2, 2, 3};
- CHECK(json::from_bjdata(vh, true, false).is_discarded());
- std::vector<uint8_t> const vR = {'[', '$', 'i', '#', '[', 'i', 1, '[', ']', ']', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR), "[json.exception.parse_error.113] parse error at byte 8: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
- CHECK(json::from_bjdata(vR, true, false).is_discarded());
- std::vector<uint8_t> const vRo = {'[', '$', 'i', '#', '[', 'i', 0, '{', '}', ']', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vRo), "[json.exception.parse_error.113] parse error at byte 8: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x7B", json::parse_error&);
- CHECK(json::from_bjdata(vRo, true, false).is_discarded());
- std::vector<uint8_t> const vR1 = {'[', '$', 'i', '#', '[', '[', 'i', 1, ']', ']', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
- CHECK(json::from_bjdata(vR1, true, false).is_discarded());
- std::vector<uint8_t> const vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR2), "[json.exception.parse_error.113] parse error at byte 11: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x5D", json::parse_error&);
- CHECK(json::from_bjdata(vR2, true, false).is_discarded());
- std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR3), "[json.exception.parse_error.112] parse error at byte 8: syntax error while parsing BJData size: ndarray requires both type and size", json::parse_error&);
- CHECK(json::from_bjdata(vR3, true, false).is_discarded());
- std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR4), "[json.exception.parse_error.110] parse error at byte 14: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vR4, true, false).is_discarded());
- std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR5), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
- CHECK(json::from_bjdata(vR5, true, false).is_discarded());
- std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR6), "[json.exception.parse_error.112] parse error at byte 14: syntax error while parsing BJData size: ndarray can not be recursive", json::parse_error&);
- CHECK(json::from_bjdata(vR6, true, false).is_discarded());
- std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vH), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
- CHECK(json::from_bjdata(vH, true, false).is_discarded());
- }
- SECTION("objects")
- {
- std::vector<uint8_t> const vST = {'{', '$', 'i', '#', 'i', 2, 'i', 1, 'a', 1};
- json _;
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vST), "[json.exception.parse_error.110] parse error at byte 11: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vST, true, false).is_discarded());
- std::vector<uint8_t> const vT = {'{', '$', 'i', 'i', 1, 'a', 1};
- CHECK_THROWS_WITH(_ = json::from_bjdata(vT), "[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x69");
- CHECK(json::from_bjdata(vT, true, false).is_discarded());
- std::vector<uint8_t> const vS = {'{', '#', 'i', 2, 'i', 1, 'a', 'i', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vS), "[json.exception.parse_error.110] parse error at byte 10: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vS, true, false).is_discarded());
- std::vector<uint8_t> const v = {'{', 'i', 1, 'a', 'i', 1};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- std::vector<uint8_t> const v2 = {'{', 'i', 1, 'a', 'i', 1, 'i'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v2), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v2, true, false).is_discarded());
- std::vector<uint8_t> const v3 = {'{', 'i', 1, 'a'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v3), "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(v3, true, false).is_discarded());
- std::vector<uint8_t> const vST1 = {'{', '$', 'd', '#', 'i', 2, 'i', 1, 'a'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vST1), "[json.exception.parse_error.110] parse error at byte 10: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vST1, true, false).is_discarded());
- std::vector<uint8_t> const vST2 = {'{', '#', 'i', 2, 'i', 1, 'a'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vST2), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
- CHECK(json::from_bjdata(vST2, true, false).is_discarded());
- std::vector<uint8_t> const vO = {'{', '#', '[', 'i', 2, 'i', 1, ']', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'i', 2};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vO), "[json.exception.parse_error.112] parse error at byte 8: syntax error while parsing BJData size: ndarray requires both type and size", json::parse_error&);
- CHECK(json::from_bjdata(vO, true, false).is_discarded());
- std::vector<uint8_t> const vO2 = {'{', '$', 'i', '#', '[', 'i', 2, 'i', 1, ']', 'i', 1, 'a', 1, 'i', 1, 'b', 2};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vO2), "[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BJData object: BJData object does not support ND-array size in optimized format", json::parse_error&);
- CHECK(json::from_bjdata(vO2, true, false).is_discarded());
- }
- }
- SECTION("writing optimized values")
- {
- SECTION("integer")
- {
- SECTION("array of i")
- {
- json const j = {1, -1};
- std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 2, 1, 0xff};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of U")
- {
- json const j = {200, 201};
- std::vector<uint8_t> const expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of I")
- {
- json const j = {30000, -30000};
- std::vector<uint8_t> const expected = {'[', '$', 'I', '#', 'i', 2, 0x30, 0x75, 0xd0, 0x8a};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of u")
- {
- json const j = {50000, 50001};
- std::vector<uint8_t> const expected = {'[', '$', 'u', '#', 'i', 2, 0x50, 0xC3, 0x51, 0xC3};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of l")
- {
- json const j = {70000, -70000};
- std::vector<uint8_t> const expected = {'[', '$', 'l', '#', 'i', 2, 0x70, 0x11, 0x01, 0x00, 0x90, 0xEE, 0xFE, 0xFF};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of m")
- {
- json const j = {3147483647, 3147483648};
- std::vector<uint8_t> const expected = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0x00, 0xCA, 0x9A, 0xBB};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- SECTION("array of L")
- {
- json const j = {5000000000, -5000000000};
- std::vector<uint8_t> const expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xFA, 0xD5, 0xFE, 0xFF, 0xFF, 0xFF};
- CHECK(json::to_bjdata(j, true, true) == expected);
- }
- }
- SECTION("unsigned integer")
- {
- SECTION("array of i")
- {
- json const j = {1u, 2u};
- std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 2, 1, 2};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'i', 1, 'i', 2};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of U")
- {
- json const j = {200u, 201u};
- std::vector<uint8_t> const expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'U', 0xC8, 'U', 0xC9};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of I")
- {
- json const j = {30000u, 30001u};
- std::vector<uint8_t> const expected = {'[', '$', 'I', '#', 'i', 2, 0x30, 0x75, 0x31, 0x75};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'I', 0x30, 0x75, 'I', 0x31, 0x75};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of u")
- {
- json const j = {50000u, 50001u};
- std::vector<uint8_t> const expected = {'[', '$', 'u', '#', 'i', 2, 0x50, 0xC3, 0x51, 0xC3};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'u', 0x50, 0xC3, 'u', 0x51, 0xC3};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of l")
- {
- json const j = {70000u, 70001u};
- std::vector<uint8_t> const expected = {'[', '$', 'l', '#', 'i', 2, 0x70, 0x11, 0x01, 0x00, 0x71, 0x11, 0x01, 0x00};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'l', 0x70, 0x11, 0x01, 0x00, 'l', 0x71, 0x11, 0x01, 0x00};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of m")
- {
- json const j = {3147483647u, 3147483648u};
- std::vector<uint8_t> const expected = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0x00, 0xCA, 0x9A, 0xBB};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'm', 0xFF, 0xC9, 0x9A, 0xBB, 'm', 0x00, 0xCA, 0x9A, 0xBB};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of L")
- {
- json const j = {5000000000u, 5000000001u};
- std::vector<uint8_t> const expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x01, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'L', 0x00, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00, 'L', 0x01, 0xF2, 0x05, 0x2A, 0x01, 0x00, 0x00, 0x00};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- SECTION("array of M")
- {
- json const j = {10223372036854775807ull, 10223372036854775808ull};
- std::vector<uint8_t> const expected = {'[', '$', 'M', '#', 'i', 2, 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 0x00, 0x00, 0x64, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'M', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D, 'M', 0x00, 0x00, 0x64, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D};
- CHECK(json::to_bjdata(j, true, true) == expected);
- CHECK(json::to_bjdata(j, true) == expected_size);
- }
- }
- }
- }
- TEST_CASE("Universal Binary JSON Specification Examples 1")
- {
- SECTION("Null Value")
- {
- json const j = {{"passcode", nullptr}};
- std::vector<uint8_t> v = {'{', 'i', 8, 'p', 'a', 's', 's', 'c', 'o', 'd', 'e', 'Z', '}'};
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("No-Op Value")
- {
- json const j = {"foo", "bar", "baz"};
- std::vector<uint8_t> v = {'[', 'S', 'i', 3, 'f', 'o', 'o',
- 'S', 'i', 3, 'b', 'a', 'r',
- 'S', 'i', 3, 'b', 'a', 'z', ']'
- };
- std::vector<uint8_t> const v2 = {'[', 'S', 'i', 3, 'f', 'o', 'o', 'N',
- 'S', 'i', 3, 'b', 'a', 'r', 'N', 'N', 'N',
- 'S', 'i', 3, 'b', 'a', 'z', 'N', 'N', ']'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- CHECK(json::from_bjdata(v2) == j);
- }
- SECTION("Boolean Types")
- {
- json const j = {{"authorized", true}, {"verified", false}};
- std::vector<uint8_t> v = {'{', 'i', 10, 'a', 'u', 't', 'h', 'o', 'r', 'i', 'z', 'e', 'd', 'T',
- 'i', 8, 'v', 'e', 'r', 'i', 'f', 'i', 'e', 'd', 'F', '}'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Numeric Types")
- {
- json j =
- {
- {"int8", 16},
- {"uint8", 255},
- {"int16", 32767},
- {"uint16", 42767},
- {"int32", 2147483647},
- {"uint32", 3147483647},
- {"int64", 9223372036854775807},
- {"uint64", 10223372036854775807ull},
- {"float64", 113243.7863123}
- };
- std::vector<uint8_t> v = {'{',
- 'i', 7, 'f', 'l', 'o', 'a', 't', '6', '4', 'D', 0xcf, 0x34, 0xbc, 0x94, 0xbc, 0xa5, 0xfb, 0x40,
- 'i', 5, 'i', 'n', 't', '1', '6', 'I', 0xff, 0x7f,
- 'i', 5, 'i', 'n', 't', '3', '2', 'l', 0xff, 0xff, 0xff, 0x7f,
- 'i', 5, 'i', 'n', 't', '6', '4', 'L', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
- 'i', 4, 'i', 'n', 't', '8', 'i', 16,
- 'i', 6, 'u', 'i', 'n', 't', '1', '6', 'u', 0x0F, 0xA7,
- 'i', 6, 'u', 'i', 'n', 't', '3', '2', 'm', 0xFF, 0xC9, 0x9A, 0xBB,
- 'i', 6, 'u', 'i', 'n', 't', '6', '4', 'M', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D,
- 'i', 5, 'u', 'i', 'n', 't', '8', 'U', 0xff,
- '}'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Char Type")
- {
- json const j = {{"rolecode", "a"}, {"delim", ";"}};
- std::vector<uint8_t> const v = {'{', 'i', 5, 'd', 'e', 'l', 'i', 'm', 'C', ';', 'i', 8, 'r', 'o', 'l', 'e', 'c', 'o', 'd', 'e', 'C', 'a', '}'};
- //CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("String Type")
- {
- SECTION("English")
- {
- json const j = "hello";
- std::vector<uint8_t> v = {'S', 'i', 5, 'h', 'e', 'l', 'l', 'o'};
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Russian")
- {
- json const j = "привет";
- std::vector<uint8_t> v = {'S', 'i', 12, 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82};
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Russian")
- {
- json const j = "مرحبا";
- std::vector<uint8_t> v = {'S', 'i', 10, 0xD9, 0x85, 0xD8, 0xB1, 0xD8, 0xAD, 0xD8, 0xA8, 0xD8, 0xA7};
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- }
- SECTION("Array Type")
- {
- SECTION("size=false type=false")
- {
- // note the float has been replaced by a double
- json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
- std::vector<uint8_t> v = {'[', 'Z', 'T', 'F', 'L', 0xE9, 0xCB, 0x0C, 0x1D, 0x01, 0x00, 0x00, 0x00, 'D', 0x4e, 0x62, 0x10, 0x58, 0x39, 0x24, 0x63, 0x40, 'S', 'i', 3, 'h', 'a', 'm', ']'};
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("size=true type=false")
- {
- // note the float has been replaced by a double
- json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
- std::vector<uint8_t> v = {'[', '#', 'i', 6, 'Z', 'T', 'F', 'L', 0xE9, 0xCB, 0x0C, 0x1D, 0x01, 0x00, 0x00, 0x00, 'D', 0x4e, 0x62, 0x10, 0x58, 0x39, 0x24, 0x63, 0x40, 'S', 'i', 3, 'h', 'a', 'm'};
- CHECK(json::to_bjdata(j, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("size=true type=true")
- {
- // note the float has been replaced by a double
- json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
- std::vector<uint8_t> v = {'[', '#', 'i', 6, 'Z', 'T', 'F', 'L', 0xE9, 0xCB, 0x0C, 0x1D, 0x01, 0x00, 0x00, 0x00, 'D', 0x4e, 0x62, 0x10, 0x58, 0x39, 0x24, 0x63, 0x40, 'S', 'i', 3, 'h', 'a', 'm'};
- CHECK(json::to_bjdata(j, true, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- }
- SECTION("Object Type")
- {
- SECTION("size=false type=false")
- {
- json j =
- {
- {
- "post", {
- {"id", 1137},
- {"author", "rkalla"},
- {"timestamp", 1364482090592},
- {"body", "I totally agree!"}
- }
- }
- };
- std::vector<uint8_t> v = {'{', 'i', 4, 'p', 'o', 's', 't', '{',
- 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
- 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
- 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
- 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
- '}', '}'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("size=true type=false")
- {
- json j =
- {
- {
- "post", {
- {"id", 1137},
- {"author", "rkalla"},
- {"timestamp", 1364482090592},
- {"body", "I totally agree!"}
- }
- }
- };
- std::vector<uint8_t> v = {'{', '#', 'i', 1, 'i', 4, 'p', 'o', 's', 't', '{', '#', 'i', 4,
- 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
- 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
- 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
- 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
- };
- CHECK(json::to_bjdata(j, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("size=true type=true")
- {
- json j =
- {
- {
- "post", {
- {"id", 1137},
- {"author", "rkalla"},
- {"timestamp", 1364482090592},
- {"body", "I totally agree!"}
- }
- }
- };
- std::vector<uint8_t> v = {'{', '#', 'i', 1, 'i', 4, 'p', 'o', 's', 't', '{', '#', 'i', 4,
- 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
- 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
- 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
- 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
- };
- CHECK(json::to_bjdata(j, true, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- }
- SECTION("Optimized Format")
- {
- SECTION("Array Example")
- {
- SECTION("No Optimization")
- {
- // note the floats have been replaced by doubles
- json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
- std::vector<uint8_t> v = {'[',
- 'D', 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
- 'D', 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
- 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 'D', 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
- 'D', 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
- ']'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Optimized with count")
- {
- // note the floats have been replaced by doubles
- json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
- std::vector<uint8_t> v = {'[', '#', 'i', 5,
- 'D', 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
- 'D', 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
- 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 'D', 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
- 'D', 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
- };
- CHECK(json::to_bjdata(j, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Optimized with type & count")
- {
- // note the floats have been replaced by doubles
- json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
- std::vector<uint8_t> v = {'[', '$', 'D', '#', 'i', 5,
- 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
- 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
- 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
- };
- CHECK(json::to_bjdata(j, true, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- }
- SECTION("Object Example")
- {
- SECTION("No Optimization")
- {
- // note the floats have been replaced by doubles
- json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
- std::vector<uint8_t> v = {'{',
- 'i', 3, 'a', 'l', 't', 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 'i', 3, 'l', 'a', 't', 'D', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
- 'i', 4, 'l', 'o', 'n', 'g', 'D', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
- '}'
- };
- CHECK(json::to_bjdata(j) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Optimized with count")
- {
- // note the floats have been replaced by doubles
- json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
- std::vector<uint8_t> v = {'{', '#', 'i', 3,
- 'i', 3, 'a', 'l', 't', 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 'i', 3, 'l', 'a', 't', 'D', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
- 'i', 4, 'l', 'o', 'n', 'g', 'D', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
- };
- CHECK(json::to_bjdata(j, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- SECTION("Optimized with type & count")
- {
- // note the floats have been replaced by doubles
- json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
- std::vector<uint8_t> v = {'{', '$', 'D', '#', 'i', 3,
- 'i', 3, 'a', 'l', 't', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
- 'i', 3, 'l', 'a', 't', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
- 'i', 4, 'l', 'o', 'n', 'g', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
- };
- CHECK(json::to_bjdata(j, true, true) == v);
- CHECK(json::from_bjdata(v) == j);
- }
- }
- SECTION("Special Cases (Null, No-Op and Boolean)")
- {
- SECTION("Array")
- {
- json _;
- std::vector<uint8_t> const v = {'[', '$', 'N', '#', 'I', 0x00, 0x02};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x4E is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- }
- SECTION("Object")
- {
- json _;
- std::vector<uint8_t> const v = {'{', '$', 'Z', '#', 'i', 3, 'i', 4, 'n', 'a', 'm', 'e', 'i', 8, 'p', 'a', 's', 's', 'w', 'o', 'r', 'd', 'i', 5, 'e', 'm', 'a', 'i', 'l'};
- CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x5A is not a permitted optimized array type", json::parse_error&);
- CHECK(json::from_bjdata(v, true, false).is_discarded());
- }
- }
- }
- }
- #if !defined(JSON_NOEXCEPTION)
- TEST_CASE("all BJData first bytes")
- {
- // these bytes will fail immediately with exception parse_error.112
- std::set<uint8_t> supported =
- {
- 'T', 'F', 'Z', 'U', 'i', 'I', 'l', 'L', 'd', 'D', 'C', 'S', '[', '{', 'N', 'H', 'u', 'm', 'M', 'h'
- };
- for (auto i = 0; i < 256; ++i)
- {
- const auto byte = static_cast<uint8_t>(i);
- CAPTURE(byte)
- try
- {
- auto res = json::from_bjdata(std::vector<uint8_t>(1, byte));
- }
- catch (const json::parse_error& e)
- {
- // check that parse_error.112 is only thrown if the
- // first byte is not in the supported set
- INFO_WITH_TEMP(e.what());
- if (supported.find(byte) == supported.end())
- {
- CHECK(e.id == 112);
- }
- else
- {
- CHECK(e.id != 112);
- }
- }
- }
- }
- #endif
- TEST_CASE("BJData roundtrips" * doctest::skip())
- {
- SECTION("input from self-generated BJData files")
- {
- for (const std::string filename :
- {
- TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json",
- TEST_DATA_DIRECTORY "/json.org/1.json",
- TEST_DATA_DIRECTORY "/json.org/2.json",
- TEST_DATA_DIRECTORY "/json.org/3.json",
- TEST_DATA_DIRECTORY "/json.org/4.json",
- TEST_DATA_DIRECTORY "/json.org/5.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip03.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip04.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip05.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip06.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip07.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip08.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip09.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip10.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip11.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip12.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip13.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip14.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip15.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip16.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip17.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip18.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip19.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip20.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip21.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip22.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip23.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip24.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip25.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip26.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip27.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip28.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip29.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip30.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip31.json",
- TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip32.json",
- TEST_DATA_DIRECTORY "/json_testsuite/sample.json",
- TEST_DATA_DIRECTORY "/json_tests/pass1.json",
- TEST_DATA_DIRECTORY "/json_tests/pass2.json",
- TEST_DATA_DIRECTORY "/json_tests/pass3.json"
- })
- {
- CAPTURE(filename)
- {
- INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
- // parse JSON file
- std::ifstream f_json(filename);
- json j1 = json::parse(f_json);
- // parse BJData file
- auto packed = utils::read_binary_file(filename + ".bjdata");
- json j2;
- CHECK_NOTHROW(j2 = json::from_bjdata(packed));
- // compare parsed JSON values
- CHECK(j1 == j2);
- }
- {
- INFO_WITH_TEMP(filename + ": std::ifstream");
- // parse JSON file
- std::ifstream f_json(filename);
- json j1 = json::parse(f_json);
- // parse BJData file
- std::ifstream f_bjdata(filename + ".bjdata", std::ios::binary);
- json j2;
- CHECK_NOTHROW(j2 = json::from_bjdata(f_bjdata));
- // compare parsed JSON values
- CHECK(j1 == j2);
- }
- {
- INFO_WITH_TEMP(filename + ": output to output adapters");
- // parse JSON file
- std::ifstream f_json(filename);
- json const j1 = json::parse(f_json);
- // parse BJData file
- auto packed = utils::read_binary_file(filename + ".bjdata");
- {
- INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
- std::vector<uint8_t> vec;
- json::to_bjdata(j1, vec);
- CHECK(vec == packed);
- }
- }
- }
- }
- }
|