unit-bjdata.cpp 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. #define JSON_TESTS_PRIVATE
  10. #include <nlohmann/json.hpp>
  11. using nlohmann::json;
  12. #include <algorithm>
  13. #include <climits>
  14. #include <limits>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <set>
  18. #include "make_test_data_available.hpp"
  19. #include "test_utils.hpp"
  20. namespace
  21. {
  22. class SaxCountdown
  23. {
  24. public:
  25. explicit SaxCountdown(const int count) : events_left(count)
  26. {}
  27. bool null()
  28. {
  29. return events_left-- > 0;
  30. }
  31. bool boolean(bool /*unused*/)
  32. {
  33. return events_left-- > 0;
  34. }
  35. bool number_integer(json::number_integer_t /*unused*/)
  36. {
  37. return events_left-- > 0;
  38. }
  39. bool number_unsigned(json::number_unsigned_t /*unused*/)
  40. {
  41. return events_left-- > 0;
  42. }
  43. bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
  44. {
  45. return events_left-- > 0;
  46. }
  47. bool string(std::string& /*unused*/)
  48. {
  49. return events_left-- > 0;
  50. }
  51. bool binary(std::vector<std::uint8_t>& /*unused*/)
  52. {
  53. return events_left-- > 0;
  54. }
  55. bool start_object(std::size_t /*unused*/)
  56. {
  57. return events_left-- > 0;
  58. }
  59. bool key(std::string& /*unused*/)
  60. {
  61. return events_left-- > 0;
  62. }
  63. bool end_object()
  64. {
  65. return events_left-- > 0;
  66. }
  67. bool start_array(std::size_t /*unused*/)
  68. {
  69. return events_left-- > 0;
  70. }
  71. bool end_array()
  72. {
  73. return events_left-- > 0;
  74. }
  75. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
  76. {
  77. return false;
  78. }
  79. private:
  80. int events_left = 0;
  81. };
  82. } // namespace
  83. // at some point in the future, a unit test dedicated to type traits might be a good idea
  84. template <typename OfType, typename T, bool MinInRange, bool MaxInRange>
  85. struct trait_test_arg
  86. {
  87. using of_type = OfType;
  88. using type = T;
  89. static constexpr bool min_in_range = MinInRange;
  90. static constexpr bool max_in_range = MaxInRange;
  91. };
  92. TEST_CASE_TEMPLATE_DEFINE("value_in_range_of trait", T, value_in_range_of_test)
  93. {
  94. using nlohmann::detail::value_in_range_of;
  95. using of_type = typename T::of_type;
  96. using type = typename T::type;
  97. constexpr bool min_in_range = T::min_in_range;
  98. constexpr bool max_in_range = T::max_in_range;
  99. type const val_min = std::numeric_limits<type>::min();
  100. type const val_min2 = val_min + 1;
  101. type const val_max = std::numeric_limits<type>::max();
  102. type const val_max2 = val_max - 1;
  103. REQUIRE(CHAR_BIT == 8);
  104. std::string of_type_str;
  105. if (std::is_unsigned<of_type>::value)
  106. {
  107. of_type_str += "u";
  108. }
  109. of_type_str += "int";
  110. of_type_str += std::to_string(sizeof(of_type) * 8);
  111. INFO("of_type := ", of_type_str);
  112. std::string type_str;
  113. if (std::is_unsigned<type>::value)
  114. {
  115. type_str += "u";
  116. }
  117. type_str += "int";
  118. type_str += std::to_string(sizeof(type) * 8);
  119. INFO("type := ", type_str);
  120. CAPTURE(val_min);
  121. CAPTURE(min_in_range);
  122. CAPTURE(val_max);
  123. CAPTURE(max_in_range);
  124. if (min_in_range)
  125. {
  126. CHECK(value_in_range_of<of_type>(val_min));
  127. CHECK(value_in_range_of<of_type>(val_min2));
  128. }
  129. else
  130. {
  131. CHECK_FALSE(value_in_range_of<of_type>(val_min));
  132. CHECK_FALSE(value_in_range_of<of_type>(val_min2));
  133. }
  134. if (max_in_range)
  135. {
  136. CHECK(value_in_range_of<of_type>(val_max));
  137. CHECK(value_in_range_of<of_type>(val_max2));
  138. }
  139. else
  140. {
  141. CHECK_FALSE(value_in_range_of<of_type>(val_max));
  142. CHECK_FALSE(value_in_range_of<of_type>(val_max2));
  143. }
  144. }
  145. TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
  146. trait_test_arg<std::int32_t, std::int32_t, true, true>, \
  147. trait_test_arg<std::int32_t, std::uint32_t, true, false>, \
  148. trait_test_arg<std::uint32_t, std::int32_t, false, true>, \
  149. trait_test_arg<std::uint32_t, std::uint32_t, true, true>, \
  150. trait_test_arg<std::int32_t, std::int64_t, false, false>, \
  151. trait_test_arg<std::int32_t, std::uint64_t, true, false>, \
  152. trait_test_arg<std::uint32_t, std::int64_t, false, false>, \
  153. trait_test_arg<std::uint32_t, std::uint64_t, true, false>, \
  154. trait_test_arg<std::int64_t, std::int32_t, true, true>, \
  155. trait_test_arg<std::int64_t, std::uint32_t, true, true>, \
  156. trait_test_arg<std::uint64_t, std::int32_t, false, true>, \
  157. trait_test_arg<std::uint64_t, std::uint32_t, true, true>, \
  158. trait_test_arg<std::int64_t, std::int64_t, true, true>, \
  159. trait_test_arg<std::int64_t, std::uint64_t, true, false>, \
  160. trait_test_arg<std::uint64_t, std::int64_t, false, true>, \
  161. trait_test_arg<std::uint64_t, std::uint64_t, true, true>);
  162. #if SIZE_MAX == 0xffffffff
  163. TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
  164. trait_test_arg<std::size_t, std::int32_t, false, true>, \
  165. trait_test_arg<std::size_t, std::uint32_t, true, true>, \
  166. trait_test_arg<std::size_t, std::int64_t, false, false>, \
  167. trait_test_arg<std::size_t, std::uint64_t, true, false>);
  168. #else
  169. TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
  170. trait_test_arg<std::size_t, std::int32_t, false, true>, \
  171. trait_test_arg<std::size_t, std::uint32_t, true, true>, \
  172. trait_test_arg<std::size_t, std::int64_t, false, true>, \
  173. trait_test_arg<std::size_t, std::uint64_t, true, true>);
  174. #endif
  175. TEST_CASE("BJData")
  176. {
  177. SECTION("binary_reader BJData LUT arrays are sorted")
  178. {
  179. std::vector<std::uint8_t> const data;
  180. auto ia = nlohmann::detail::input_adapter(data);
  181. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  182. nlohmann::detail::binary_reader<json, decltype(ia)> const br{std::move(ia), json::input_format_t::bjdata};
  183. CHECK(std::is_sorted(br.bjd_optimized_type_markers.begin(), br.bjd_optimized_type_markers.end()));
  184. CHECK(std::is_sorted(br.bjd_types_map.begin(), br.bjd_types_map.end()));
  185. }
  186. SECTION("individual values")
  187. {
  188. SECTION("discarded")
  189. {
  190. // discarded values are not serialized
  191. json const j = json::value_t::discarded;
  192. const auto result = json::to_bjdata(j);
  193. CHECK(result.empty());
  194. }
  195. SECTION("null")
  196. {
  197. json const j = nullptr;
  198. std::vector<uint8_t> const expected = {'Z'};
  199. const auto result = json::to_bjdata(j);
  200. CHECK(result == expected);
  201. // roundtrip
  202. CHECK(json::from_bjdata(result) == j);
  203. CHECK(json::from_bjdata(result, true, false) == j);
  204. }
  205. SECTION("boolean")
  206. {
  207. SECTION("true")
  208. {
  209. json const j = true;
  210. std::vector<uint8_t> const expected = {'T'};
  211. const auto result = json::to_bjdata(j);
  212. CHECK(result == expected);
  213. // roundtrip
  214. CHECK(json::from_bjdata(result) == j);
  215. CHECK(json::from_bjdata(result, true, false) == j);
  216. }
  217. SECTION("false")
  218. {
  219. json const j = false;
  220. std::vector<uint8_t> const expected = {'F'};
  221. const auto result = json::to_bjdata(j);
  222. CHECK(result == expected);
  223. // roundtrip
  224. CHECK(json::from_bjdata(result) == j);
  225. CHECK(json::from_bjdata(result, true, false) == j);
  226. }
  227. }
  228. SECTION("number")
  229. {
  230. SECTION("signed")
  231. {
  232. SECTION("-9223372036854775808..-2147483649 (int64)")
  233. {
  234. std::vector<int64_t> const numbers
  235. {
  236. (std::numeric_limits<int64_t>::min)(),
  237. -1000000000000000000LL,
  238. -100000000000000000LL,
  239. -10000000000000000LL,
  240. -1000000000000000LL,
  241. -100000000000000LL,
  242. -10000000000000LL,
  243. -1000000000000LL,
  244. -100000000000LL,
  245. -10000000000LL,
  246. -2147483649LL,
  247. };
  248. for (const auto i : numbers)
  249. {
  250. CAPTURE(i)
  251. // create JSON value with integer number
  252. json const j = i;
  253. // check type
  254. CHECK(j.is_number_integer());
  255. // create expected byte vector
  256. std::vector<uint8_t> const expected
  257. {
  258. static_cast<uint8_t>('L'),
  259. static_cast<uint8_t>(i & 0xff),
  260. static_cast<uint8_t>((i >> 8) & 0xff),
  261. static_cast<uint8_t>((i >> 16) & 0xff),
  262. static_cast<uint8_t>((i >> 24) & 0xff),
  263. static_cast<uint8_t>((i >> 32) & 0xff),
  264. static_cast<uint8_t>((i >> 40) & 0xff),
  265. static_cast<uint8_t>((i >> 48) & 0xff),
  266. static_cast<uint8_t>((i >> 56) & 0xff),
  267. };
  268. // compare result + size
  269. const auto result = json::to_bjdata(j);
  270. CHECK(result == expected);
  271. CHECK(result.size() == 9);
  272. // check individual bytes
  273. CHECK(result[0] == 'L');
  274. int64_t const restored = (static_cast<int64_t>(result[8]) << 070) +
  275. (static_cast<int64_t>(result[7]) << 060) +
  276. (static_cast<int64_t>(result[6]) << 050) +
  277. (static_cast<int64_t>(result[5]) << 040) +
  278. (static_cast<int64_t>(result[4]) << 030) +
  279. (static_cast<int64_t>(result[3]) << 020) +
  280. (static_cast<int64_t>(result[2]) << 010) +
  281. static_cast<int64_t>(result[1]);
  282. CHECK(restored == i);
  283. // roundtrip
  284. CHECK(json::from_bjdata(result) == j);
  285. CHECK(json::from_bjdata(result, true, false) == j);
  286. }
  287. }
  288. SECTION("-2147483648..-32769 (int32)")
  289. {
  290. std::vector<int32_t> const numbers
  291. {
  292. -32769,
  293. -100000,
  294. -1000000,
  295. -10000000,
  296. -100000000,
  297. -1000000000,
  298. -2147483647 - 1, // https://stackoverflow.com/a/29356002/266378
  299. };
  300. for (const auto i : numbers)
  301. {
  302. CAPTURE(i)
  303. // create JSON value with integer number
  304. json const j = i;
  305. // check type
  306. CHECK(j.is_number_integer());
  307. // create expected byte vector
  308. std::vector<uint8_t> const expected
  309. {
  310. static_cast<uint8_t>('l'),
  311. static_cast<uint8_t>(i & 0xff),
  312. static_cast<uint8_t>((i >> 8) & 0xff),
  313. static_cast<uint8_t>((i >> 16) & 0xff),
  314. static_cast<uint8_t>((i >> 24) & 0xff),
  315. };
  316. // compare result + size
  317. const auto result = json::to_bjdata(j);
  318. CHECK(result == expected);
  319. CHECK(result.size() == 5);
  320. // check individual bytes
  321. CHECK(result[0] == 'l');
  322. int32_t const restored = (static_cast<int32_t>(result[4]) << 030) +
  323. (static_cast<int32_t>(result[3]) << 020) +
  324. (static_cast<int32_t>(result[2]) << 010) +
  325. static_cast<int32_t>(result[1]);
  326. CHECK(restored == i);
  327. // roundtrip
  328. CHECK(json::from_bjdata(result) == j);
  329. CHECK(json::from_bjdata(result, true, false) == j);
  330. }
  331. }
  332. SECTION("-32768..-129 (int16)")
  333. {
  334. for (int32_t i = -32768; i <= -129; ++i)
  335. {
  336. CAPTURE(i)
  337. // create JSON value with integer number
  338. json const j = i;
  339. // check type
  340. CHECK(j.is_number_integer());
  341. // create expected byte vector
  342. std::vector<uint8_t> const expected
  343. {
  344. static_cast<uint8_t>('I'),
  345. static_cast<uint8_t>(i & 0xff),
  346. static_cast<uint8_t>((i >> 8) & 0xff),
  347. };
  348. // compare result + size
  349. const auto result = json::to_bjdata(j);
  350. CHECK(result == expected);
  351. CHECK(result.size() == 3);
  352. // check individual bytes
  353. CHECK(result[0] == 'I');
  354. auto const restored = static_cast<int16_t>(((result[2] << 8) + result[1]));
  355. CHECK(restored == i);
  356. // roundtrip
  357. CHECK(json::from_bjdata(result) == j);
  358. CHECK(json::from_bjdata(result, true, false) == j);
  359. }
  360. }
  361. SECTION("-9263 (int16)")
  362. {
  363. json const j = -9263;
  364. std::vector<uint8_t> const expected = {'I', 0xd1, 0xdb};
  365. // compare result + size
  366. const auto result = json::to_bjdata(j);
  367. CHECK(result == expected);
  368. CHECK(result.size() == 3);
  369. // check individual bytes
  370. CHECK(result[0] == 'I');
  371. auto const restored = static_cast<int16_t>(((result[2] << 8) + result[1]));
  372. CHECK(restored == -9263);
  373. // roundtrip
  374. CHECK(json::from_bjdata(result) == j);
  375. CHECK(json::from_bjdata(result, true, false) == j);
  376. }
  377. SECTION("-128..-1 (int8)")
  378. {
  379. for (auto i = -128; i <= -1; ++i)
  380. {
  381. CAPTURE(i)
  382. // create JSON value with integer number
  383. json const j = i;
  384. // check type
  385. CHECK(j.is_number_integer());
  386. // create expected byte vector
  387. std::vector<uint8_t> const expected
  388. {
  389. 'i',
  390. static_cast<uint8_t>(i),
  391. };
  392. // compare result + size
  393. const auto result = json::to_bjdata(j);
  394. CHECK(result == expected);
  395. CHECK(result.size() == 2);
  396. // check individual bytes
  397. CHECK(result[0] == 'i');
  398. CHECK(static_cast<int8_t>(result[1]) == i);
  399. // roundtrip
  400. CHECK(json::from_bjdata(result) == j);
  401. CHECK(json::from_bjdata(result, true, false) == j);
  402. }
  403. }
  404. SECTION("0..127 (int8)")
  405. {
  406. for (size_t i = 0; i <= 127; ++i)
  407. {
  408. CAPTURE(i)
  409. // create JSON value with integer number
  410. json j = -1;
  411. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  412. // check type
  413. CHECK(j.is_number_integer());
  414. // create expected byte vector
  415. std::vector<uint8_t> const expected
  416. {
  417. static_cast<uint8_t>('i'),
  418. static_cast<uint8_t>(i),
  419. };
  420. // compare result + size
  421. const auto result = json::to_bjdata(j);
  422. CHECK(result == expected);
  423. CHECK(result.size() == 2);
  424. // check individual bytes
  425. CHECK(result[0] == 'i');
  426. CHECK(result[1] == i);
  427. // roundtrip
  428. CHECK(json::from_bjdata(result) == j);
  429. CHECK(json::from_bjdata(result, true, false) == j);
  430. }
  431. }
  432. SECTION("128..255 (uint8)")
  433. {
  434. for (size_t i = 128; i <= 255; ++i)
  435. {
  436. CAPTURE(i)
  437. // create JSON value with integer number
  438. json j = -1;
  439. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  440. // check type
  441. CHECK(j.is_number_integer());
  442. // create expected byte vector
  443. std::vector<uint8_t> const expected
  444. {
  445. static_cast<uint8_t>('U'),
  446. static_cast<uint8_t>(i),
  447. };
  448. // compare result + size
  449. const auto result = json::to_bjdata(j);
  450. CHECK(result == expected);
  451. CHECK(result.size() == 2);
  452. // check individual bytes
  453. CHECK(result[0] == 'U');
  454. CHECK(result[1] == i);
  455. // roundtrip
  456. CHECK(json::from_bjdata(result) == j);
  457. CHECK(json::from_bjdata(result, true, false) == j);
  458. }
  459. }
  460. SECTION("256..32767 (int16)")
  461. {
  462. for (size_t i = 256; i <= 32767; ++i)
  463. {
  464. CAPTURE(i)
  465. // create JSON value with integer number
  466. json j = -1;
  467. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  468. // check type
  469. CHECK(j.is_number_integer());
  470. // create expected byte vector
  471. std::vector<uint8_t> const expected
  472. {
  473. static_cast<uint8_t>('I'),
  474. static_cast<uint8_t>(i & 0xff),
  475. static_cast<uint8_t>((i >> 8) & 0xff),
  476. };
  477. // compare result + size
  478. const auto result = json::to_bjdata(j);
  479. CHECK(result == expected);
  480. CHECK(result.size() == 3);
  481. // check individual bytes
  482. CHECK(result[0] == 'I');
  483. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
  484. CHECK(restored == i);
  485. // roundtrip
  486. CHECK(json::from_bjdata(result) == j);
  487. CHECK(json::from_bjdata(result, true, false) == j);
  488. }
  489. }
  490. SECTION("32768..65535 (uint16)")
  491. {
  492. for (const uint32_t i :
  493. {
  494. 32768u, 55555u, 65535u
  495. })
  496. {
  497. CAPTURE(i)
  498. // create JSON value with integer number
  499. json j = -1;
  500. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  501. // check type
  502. CHECK(j.is_number_integer());
  503. // create expected byte vector
  504. std::vector<uint8_t> const expected
  505. {
  506. static_cast<uint8_t>('u'),
  507. static_cast<uint8_t>(i & 0xff),
  508. static_cast<uint8_t>((i >> 8) & 0xff),
  509. };
  510. // compare result + size
  511. const auto result = json::to_bjdata(j);
  512. CHECK(result == expected);
  513. CHECK(result.size() == 3);
  514. // check individual bytes
  515. CHECK(result[0] == 'u');
  516. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
  517. CHECK(restored == i);
  518. // roundtrip
  519. CHECK(json::from_bjdata(result) == j);
  520. CHECK(json::from_bjdata(result, true, false) == j);
  521. }
  522. }
  523. SECTION("65536..2147483647 (int32)")
  524. {
  525. for (const uint32_t i :
  526. {
  527. 65536u, 77777u, 2147483647u
  528. })
  529. {
  530. CAPTURE(i)
  531. // create JSON value with integer number
  532. json j = -1;
  533. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  534. // check type
  535. CHECK(j.is_number_integer());
  536. // create expected byte vector
  537. std::vector<uint8_t> const expected
  538. {
  539. 'l',
  540. static_cast<uint8_t>(i & 0xff),
  541. static_cast<uint8_t>((i >> 8) & 0xff),
  542. static_cast<uint8_t>((i >> 16) & 0xff),
  543. static_cast<uint8_t>((i >> 24) & 0xff),
  544. };
  545. // compare result + size
  546. const auto result = json::to_bjdata(j);
  547. CHECK(result == expected);
  548. CHECK(result.size() == 5);
  549. // check individual bytes
  550. CHECK(result[0] == 'l');
  551. uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
  552. (static_cast<uint32_t>(result[3]) << 020) +
  553. (static_cast<uint32_t>(result[2]) << 010) +
  554. static_cast<uint32_t>(result[1]);
  555. CHECK(restored == i);
  556. // roundtrip
  557. CHECK(json::from_bjdata(result) == j);
  558. CHECK(json::from_bjdata(result, true, false) == j);
  559. }
  560. }
  561. SECTION("2147483648..4294967295 (uint32)")
  562. {
  563. for (const uint32_t i :
  564. {
  565. 2147483648u, 3333333333u, 4294967295u
  566. })
  567. {
  568. CAPTURE(i)
  569. // create JSON value with integer number
  570. json j = -1;
  571. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  572. // check type
  573. CHECK(j.is_number_integer());
  574. // create expected byte vector
  575. std::vector<uint8_t> const expected
  576. {
  577. 'm',
  578. static_cast<uint8_t>(i & 0xff),
  579. static_cast<uint8_t>((i >> 8) & 0xff),
  580. static_cast<uint8_t>((i >> 16) & 0xff),
  581. static_cast<uint8_t>((i >> 24) & 0xff),
  582. };
  583. // compare result + size
  584. const auto result = json::to_bjdata(j);
  585. CHECK(result == expected);
  586. CHECK(result.size() == 5);
  587. // check individual bytes
  588. CHECK(result[0] == 'm');
  589. uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
  590. (static_cast<uint32_t>(result[3]) << 020) +
  591. (static_cast<uint32_t>(result[2]) << 010) +
  592. static_cast<uint32_t>(result[1]);
  593. CHECK(restored == i);
  594. // roundtrip
  595. CHECK(json::from_bjdata(result) == j);
  596. CHECK(json::from_bjdata(result, true, false) == j);
  597. }
  598. }
  599. SECTION("4294967296..9223372036854775807 (int64)")
  600. {
  601. std::vector<uint64_t> const v = {4294967296LU, 9223372036854775807LU};
  602. for (const uint64_t i : v)
  603. {
  604. CAPTURE(i)
  605. // create JSON value with integer number
  606. json j = -1;
  607. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  608. // check type
  609. CHECK(j.is_number_integer());
  610. // create expected byte vector
  611. std::vector<uint8_t> const expected
  612. {
  613. 'L',
  614. static_cast<uint8_t>(i & 0xff),
  615. static_cast<uint8_t>((i >> 010) & 0xff),
  616. static_cast<uint8_t>((i >> 020) & 0xff),
  617. static_cast<uint8_t>((i >> 030) & 0xff),
  618. static_cast<uint8_t>((i >> 040) & 0xff),
  619. static_cast<uint8_t>((i >> 050) & 0xff),
  620. static_cast<uint8_t>((i >> 060) & 0xff),
  621. static_cast<uint8_t>((i >> 070) & 0xff),
  622. };
  623. // compare result + size
  624. const auto result = json::to_bjdata(j);
  625. CHECK(result == expected);
  626. CHECK(result.size() == 9);
  627. // check individual bytes
  628. CHECK(result[0] == 'L');
  629. uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
  630. (static_cast<uint64_t>(result[7]) << 060) +
  631. (static_cast<uint64_t>(result[6]) << 050) +
  632. (static_cast<uint64_t>(result[5]) << 040) +
  633. (static_cast<uint64_t>(result[4]) << 030) +
  634. (static_cast<uint64_t>(result[3]) << 020) +
  635. (static_cast<uint64_t>(result[2]) << 010) +
  636. static_cast<uint64_t>(result[1]);
  637. CHECK(restored == i);
  638. // roundtrip
  639. CHECK(json::from_bjdata(result) == j);
  640. CHECK(json::from_bjdata(result, true, false) == j);
  641. }
  642. }
  643. SECTION("9223372036854775808..18446744073709551615 (uint64)")
  644. {
  645. std::vector<uint64_t> const v = {9223372036854775808ull, 18446744073709551615ull};
  646. for (const uint64_t i : v)
  647. {
  648. CAPTURE(i)
  649. // create JSON value with integer number
  650. json const j = i;
  651. // check type
  652. CHECK(j.is_number_unsigned());
  653. // create expected byte vector
  654. std::vector<uint8_t> const expected
  655. {
  656. 'M',
  657. static_cast<uint8_t>(i & 0xff),
  658. static_cast<uint8_t>((i >> 010) & 0xff),
  659. static_cast<uint8_t>((i >> 020) & 0xff),
  660. static_cast<uint8_t>((i >> 030) & 0xff),
  661. static_cast<uint8_t>((i >> 040) & 0xff),
  662. static_cast<uint8_t>((i >> 050) & 0xff),
  663. static_cast<uint8_t>((i >> 060) & 0xff),
  664. static_cast<uint8_t>((i >> 070) & 0xff),
  665. };
  666. // compare result + size
  667. const auto result = json::to_bjdata(j);
  668. CHECK(result == expected);
  669. CHECK(result.size() == 9);
  670. // check individual bytes
  671. CHECK(result[0] == 'M');
  672. uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
  673. (static_cast<uint64_t>(result[7]) << 060) +
  674. (static_cast<uint64_t>(result[6]) << 050) +
  675. (static_cast<uint64_t>(result[5]) << 040) +
  676. (static_cast<uint64_t>(result[4]) << 030) +
  677. (static_cast<uint64_t>(result[3]) << 020) +
  678. (static_cast<uint64_t>(result[2]) << 010) +
  679. static_cast<uint64_t>(result[1]);
  680. CHECK(restored == i);
  681. // roundtrip
  682. CHECK(json::from_bjdata(result) == j);
  683. CHECK(json::from_bjdata(result, true, false) == j);
  684. }
  685. }
  686. }
  687. SECTION("unsigned")
  688. {
  689. SECTION("0..127 (int8)")
  690. {
  691. for (size_t i = 0; i <= 127; ++i)
  692. {
  693. CAPTURE(i)
  694. // create JSON value with unsigned integer number
  695. json const j = i;
  696. // check type
  697. CHECK(j.is_number_unsigned());
  698. // create expected byte vector
  699. std::vector<uint8_t> const expected{'i', static_cast<uint8_t>(i)};
  700. // compare result + size
  701. const auto result = json::to_bjdata(j);
  702. CHECK(result == expected);
  703. CHECK(result.size() == 2);
  704. // check individual bytes
  705. CHECK(result[0] == 'i');
  706. auto const restored = static_cast<uint8_t>(result[1]);
  707. CHECK(restored == i);
  708. // roundtrip
  709. CHECK(json::from_bjdata(result) == j);
  710. CHECK(json::from_bjdata(result, true, false) == j);
  711. }
  712. }
  713. SECTION("128..255 (uint8)")
  714. {
  715. for (size_t i = 128; i <= 255; ++i)
  716. {
  717. CAPTURE(i)
  718. // create JSON value with unsigned integer number
  719. json const j = i;
  720. // check type
  721. CHECK(j.is_number_unsigned());
  722. // create expected byte vector
  723. std::vector<uint8_t> const expected{'U', static_cast<uint8_t>(i)};
  724. // compare result + size
  725. const auto result = json::to_bjdata(j);
  726. CHECK(result == expected);
  727. CHECK(result.size() == 2);
  728. // check individual bytes
  729. CHECK(result[0] == 'U');
  730. auto const restored = static_cast<uint8_t>(result[1]);
  731. CHECK(restored == i);
  732. // roundtrip
  733. CHECK(json::from_bjdata(result) == j);
  734. CHECK(json::from_bjdata(result, true, false) == j);
  735. }
  736. }
  737. SECTION("256..32767 (int16)")
  738. {
  739. for (size_t i = 256; i <= 32767; ++i)
  740. {
  741. CAPTURE(i)
  742. // create JSON value with unsigned integer number
  743. json const j = i;
  744. // check type
  745. CHECK(j.is_number_unsigned());
  746. // create expected byte vector
  747. std::vector<uint8_t> const expected
  748. {
  749. 'I',
  750. static_cast<uint8_t>(i & 0xff),
  751. static_cast<uint8_t>((i >> 8) & 0xff),
  752. };
  753. // compare result + size
  754. const auto result = json::to_bjdata(j);
  755. CHECK(result == expected);
  756. CHECK(result.size() == 3);
  757. // check individual bytes
  758. CHECK(result[0] == 'I');
  759. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
  760. CHECK(restored == i);
  761. // roundtrip
  762. CHECK(json::from_bjdata(result) == j);
  763. CHECK(json::from_bjdata(result, true, false) == j);
  764. }
  765. }
  766. SECTION("32768..65535 (uint16)")
  767. {
  768. for (const uint32_t i :
  769. {
  770. 32768u, 55555u, 65535u
  771. })
  772. {
  773. CAPTURE(i)
  774. // create JSON value with unsigned integer number
  775. json const j = i;
  776. // check type
  777. CHECK(j.is_number_unsigned());
  778. // create expected byte vector
  779. std::vector<uint8_t> const expected
  780. {
  781. 'u',
  782. static_cast<uint8_t>(i & 0xff),
  783. static_cast<uint8_t>((i >> 8) & 0xff),
  784. };
  785. // compare result + size
  786. const auto result = json::to_bjdata(j);
  787. CHECK(result == expected);
  788. CHECK(result.size() == 3);
  789. // check individual bytes
  790. CHECK(result[0] == 'u');
  791. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[2]) * 256 + static_cast<uint8_t>(result[1]));
  792. CHECK(restored == i);
  793. // roundtrip
  794. CHECK(json::from_bjdata(result) == j);
  795. CHECK(json::from_bjdata(result, true, false) == j);
  796. }
  797. }
  798. SECTION("65536..2147483647 (int32)")
  799. {
  800. for (const uint32_t i :
  801. {
  802. 65536u, 77777u, 2147483647u
  803. })
  804. {
  805. CAPTURE(i)
  806. // create JSON value with unsigned integer number
  807. json const j = i;
  808. // check type
  809. CHECK(j.is_number_unsigned());
  810. // create expected byte vector
  811. std::vector<uint8_t> const expected
  812. {
  813. 'l',
  814. static_cast<uint8_t>(i & 0xff),
  815. static_cast<uint8_t>((i >> 8) & 0xff),
  816. static_cast<uint8_t>((i >> 16) & 0xff),
  817. static_cast<uint8_t>((i >> 24) & 0xff),
  818. };
  819. // compare result + size
  820. const auto result = json::to_bjdata(j);
  821. CHECK(result == expected);
  822. CHECK(result.size() == 5);
  823. // check individual bytes
  824. CHECK(result[0] == 'l');
  825. uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
  826. (static_cast<uint32_t>(result[3]) << 020) +
  827. (static_cast<uint32_t>(result[2]) << 010) +
  828. static_cast<uint32_t>(result[1]);
  829. CHECK(restored == i);
  830. // roundtrip
  831. CHECK(json::from_bjdata(result) == j);
  832. CHECK(json::from_bjdata(result, true, false) == j);
  833. }
  834. }
  835. SECTION("2147483648..4294967295 (uint32)")
  836. {
  837. for (const uint32_t i :
  838. {
  839. 2147483648u, 3333333333u, 4294967295u
  840. })
  841. {
  842. CAPTURE(i)
  843. // create JSON value with unsigned integer number
  844. json const j = i;
  845. // check type
  846. CHECK(j.is_number_unsigned());
  847. // create expected byte vector
  848. std::vector<uint8_t> const expected
  849. {
  850. 'm',
  851. static_cast<uint8_t>(i & 0xff),
  852. static_cast<uint8_t>((i >> 8) & 0xff),
  853. static_cast<uint8_t>((i >> 16) & 0xff),
  854. static_cast<uint8_t>((i >> 24) & 0xff),
  855. };
  856. // compare result + size
  857. const auto result = json::to_bjdata(j);
  858. CHECK(result == expected);
  859. CHECK(result.size() == 5);
  860. // check individual bytes
  861. CHECK(result[0] == 'm');
  862. uint32_t const restored = (static_cast<uint32_t>(result[4]) << 030) +
  863. (static_cast<uint32_t>(result[3]) << 020) +
  864. (static_cast<uint32_t>(result[2]) << 010) +
  865. static_cast<uint32_t>(result[1]);
  866. CHECK(restored == i);
  867. // roundtrip
  868. CHECK(json::from_bjdata(result) == j);
  869. CHECK(json::from_bjdata(result, true, false) == j);
  870. }
  871. }
  872. SECTION("4294967296..9223372036854775807 (int64)")
  873. {
  874. std::vector<uint64_t> const v = {4294967296ul, 9223372036854775807ul};
  875. for (const uint64_t i : v)
  876. {
  877. CAPTURE(i)
  878. // create JSON value with integer number
  879. json const j = i;
  880. // check type
  881. CHECK(j.is_number_unsigned());
  882. // create expected byte vector
  883. std::vector<uint8_t> const expected
  884. {
  885. 'L',
  886. static_cast<uint8_t>(i & 0xff),
  887. static_cast<uint8_t>((i >> 010) & 0xff),
  888. static_cast<uint8_t>((i >> 020) & 0xff),
  889. static_cast<uint8_t>((i >> 030) & 0xff),
  890. static_cast<uint8_t>((i >> 040) & 0xff),
  891. static_cast<uint8_t>((i >> 050) & 0xff),
  892. static_cast<uint8_t>((i >> 060) & 0xff),
  893. static_cast<uint8_t>((i >> 070) & 0xff),
  894. };
  895. // compare result + size
  896. const auto result = json::to_bjdata(j);
  897. CHECK(result == expected);
  898. CHECK(result.size() == 9);
  899. // check individual bytes
  900. CHECK(result[0] == 'L');
  901. uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
  902. (static_cast<uint64_t>(result[7]) << 060) +
  903. (static_cast<uint64_t>(result[6]) << 050) +
  904. (static_cast<uint64_t>(result[5]) << 040) +
  905. (static_cast<uint64_t>(result[4]) << 030) +
  906. (static_cast<uint64_t>(result[3]) << 020) +
  907. (static_cast<uint64_t>(result[2]) << 010) +
  908. static_cast<uint64_t>(result[1]);
  909. CHECK(restored == i);
  910. // roundtrip
  911. CHECK(json::from_bjdata(result) == j);
  912. CHECK(json::from_bjdata(result, true, false) == j);
  913. }
  914. }
  915. SECTION("9223372036854775808..18446744073709551615 (uint64)")
  916. {
  917. std::vector<uint64_t> const v = {9223372036854775808ull, 18446744073709551615ull};
  918. for (const uint64_t i : v)
  919. {
  920. CAPTURE(i)
  921. // create JSON value with integer number
  922. json const j = i;
  923. // check type
  924. CHECK(j.is_number_unsigned());
  925. // create expected byte vector
  926. std::vector<uint8_t> const expected
  927. {
  928. 'M',
  929. static_cast<uint8_t>(i & 0xff),
  930. static_cast<uint8_t>((i >> 010) & 0xff),
  931. static_cast<uint8_t>((i >> 020) & 0xff),
  932. static_cast<uint8_t>((i >> 030) & 0xff),
  933. static_cast<uint8_t>((i >> 040) & 0xff),
  934. static_cast<uint8_t>((i >> 050) & 0xff),
  935. static_cast<uint8_t>((i >> 060) & 0xff),
  936. static_cast<uint8_t>((i >> 070) & 0xff),
  937. };
  938. // compare result + size
  939. const auto result = json::to_bjdata(j);
  940. CHECK(result == expected);
  941. CHECK(result.size() == 9);
  942. // check individual bytes
  943. CHECK(result[0] == 'M');
  944. uint64_t const restored = (static_cast<uint64_t>(result[8]) << 070) +
  945. (static_cast<uint64_t>(result[7]) << 060) +
  946. (static_cast<uint64_t>(result[6]) << 050) +
  947. (static_cast<uint64_t>(result[5]) << 040) +
  948. (static_cast<uint64_t>(result[4]) << 030) +
  949. (static_cast<uint64_t>(result[3]) << 020) +
  950. (static_cast<uint64_t>(result[2]) << 010) +
  951. static_cast<uint64_t>(result[1]);
  952. CHECK(restored == i);
  953. // roundtrip
  954. CHECK(json::from_bjdata(result) == j);
  955. CHECK(json::from_bjdata(result, true, false) == j);
  956. }
  957. }
  958. }
  959. SECTION("float64")
  960. {
  961. SECTION("3.1415925")
  962. {
  963. double v = 3.1415925;
  964. json const j = v;
  965. std::vector<uint8_t> const expected =
  966. {
  967. 'D', 0xfc, 0xde, 0xa6, 0x3f, 0xfb, 0x21, 0x09, 0x40
  968. };
  969. const auto result = json::to_bjdata(j);
  970. CHECK(result == expected);
  971. // roundtrip
  972. CHECK(json::from_bjdata(result) == j);
  973. CHECK(json::from_bjdata(result) == v);
  974. CHECK(json::from_bjdata(result, true, false) == j);
  975. }
  976. }
  977. SECTION("half-precision float")
  978. {
  979. SECTION("simple half floats")
  980. {
  981. CHECK(json::parse("0.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x00})));
  982. CHECK(json::parse("-0.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x80})));
  983. CHECK(json::parse("1.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3c})));
  984. CHECK(json::parse("1.5") == json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3e})));
  985. CHECK(json::parse("65504.0") == json::from_bjdata(std::vector<uint8_t>({'h', 0xff, 0x7b})));
  986. }
  987. SECTION("errors")
  988. {
  989. SECTION("no byte follows")
  990. {
  991. json _;
  992. std::vector<uint8_t> const vec0 = {'h'};
  993. 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&);
  994. CHECK(json::from_bjdata(vec0, true, false).is_discarded());
  995. }
  996. SECTION("only one byte follows")
  997. {
  998. json _;
  999. std::vector<uint8_t> const vec1 = {'h', 0x00};
  1000. 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&);
  1001. CHECK(json::from_bjdata(vec1, true, false).is_discarded());
  1002. }
  1003. }
  1004. }
  1005. SECTION("half-precision float (edge cases)")
  1006. {
  1007. SECTION("exp = 0b00000")
  1008. {
  1009. SECTION("0 (0 00000 0000000000)")
  1010. {
  1011. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x00}));
  1012. json::number_float_t d{j};
  1013. CHECK(d == 0.0);
  1014. }
  1015. SECTION("-0 (1 00000 0000000000)")
  1016. {
  1017. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x80}));
  1018. json::number_float_t d{j};
  1019. CHECK(d == -0.0);
  1020. }
  1021. SECTION("2**-24 (0 00000 0000000001)")
  1022. {
  1023. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x01, 0x00}));
  1024. json::number_float_t d{j};
  1025. CHECK(d == std::pow(2.0, -24.0));
  1026. }
  1027. }
  1028. SECTION("exp = 0b11111")
  1029. {
  1030. SECTION("infinity (0 11111 0000000000)")
  1031. {
  1032. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7c}));
  1033. json::number_float_t d{j};
  1034. CHECK(d == std::numeric_limits<json::number_float_t>::infinity());
  1035. CHECK(j.dump() == "null");
  1036. }
  1037. SECTION("-infinity (1 11111 0000000000)")
  1038. {
  1039. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0xfc}));
  1040. json::number_float_t d{j};
  1041. CHECK(d == -std::numeric_limits<json::number_float_t>::infinity());
  1042. CHECK(j.dump() == "null");
  1043. }
  1044. }
  1045. SECTION("other values from https://en.wikipedia.org/wiki/Half-precision_floating-point_format")
  1046. {
  1047. SECTION("1 (0 01111 0000000000)")
  1048. {
  1049. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x3c}));
  1050. json::number_float_t d{j};
  1051. CHECK(d == 1);
  1052. }
  1053. SECTION("-2 (1 10000 0000000000)")
  1054. {
  1055. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0xc0}));
  1056. json::number_float_t d{j};
  1057. CHECK(d == -2);
  1058. }
  1059. SECTION("65504 (0 11110 1111111111)")
  1060. {
  1061. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0xff, 0x7b}));
  1062. json::number_float_t d{j};
  1063. CHECK(d == 65504);
  1064. }
  1065. }
  1066. SECTION("infinity")
  1067. {
  1068. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7c}));
  1069. json::number_float_t const d{j};
  1070. CHECK_FALSE(std::isfinite(d));
  1071. CHECK(j.dump() == "null");
  1072. }
  1073. SECTION("NaN")
  1074. {
  1075. json const j = json::from_bjdata(std::vector<uint8_t>({'h', 0x00, 0x7e }));
  1076. json::number_float_t const d{j};
  1077. CHECK(std::isnan(d));
  1078. CHECK(j.dump() == "null");
  1079. }
  1080. }
  1081. SECTION("high-precision number")
  1082. {
  1083. SECTION("unsigned integer number")
  1084. {
  1085. 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'};
  1086. const auto j = json::from_bjdata(vec);
  1087. CHECK(j.is_number_unsigned());
  1088. CHECK(j.dump() == "12345678901234567890");
  1089. }
  1090. SECTION("signed integer number")
  1091. {
  1092. 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'};
  1093. const auto j = json::from_bjdata(vec);
  1094. CHECK(j.is_number_integer());
  1095. CHECK(j.dump() == "-123456789012345678");
  1096. }
  1097. SECTION("floating-point number")
  1098. {
  1099. 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'};
  1100. const auto j = json::from_bjdata(vec);
  1101. CHECK(j.is_number_float());
  1102. CHECK(j.dump() == "3.141592653589793");
  1103. }
  1104. SECTION("errors")
  1105. {
  1106. // error while parsing length
  1107. std::vector<uint8_t> const vec0 = {'H', 'i'};
  1108. CHECK(json::from_bjdata(vec0, true, false).is_discarded());
  1109. // error while parsing string
  1110. std::vector<uint8_t> const vec1 = {'H', 'i', '1'};
  1111. CHECK(json::from_bjdata(vec1, true, false).is_discarded());
  1112. json _;
  1113. std::vector<uint8_t> const vec2 = {'H', 'i', 2, '1', 'A', '3'};
  1114. 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);
  1115. std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
  1116. 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);
  1117. std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
  1118. 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);
  1119. }
  1120. }
  1121. }
  1122. SECTION("string")
  1123. {
  1124. SECTION("N = 0..127")
  1125. {
  1126. for (size_t N = 0; N <= 127; ++N)
  1127. {
  1128. CAPTURE(N)
  1129. // create JSON value with string containing of N * 'x'
  1130. const auto s = std::string(N, 'x');
  1131. json const j = s;
  1132. // create expected byte vector
  1133. std::vector<uint8_t> expected;
  1134. expected.push_back('S');
  1135. expected.push_back('i');
  1136. expected.push_back(static_cast<uint8_t>(N));
  1137. for (size_t i = 0; i < N; ++i)
  1138. {
  1139. expected.push_back('x');
  1140. }
  1141. // compare result + size
  1142. const auto result = json::to_bjdata(j);
  1143. CHECK(result == expected);
  1144. CHECK(result.size() == N + 3);
  1145. // check that no null byte is appended
  1146. if (N > 0)
  1147. {
  1148. CHECK(result.back() != '\x00');
  1149. }
  1150. // roundtrip
  1151. CHECK(json::from_bjdata(result) == j);
  1152. CHECK(json::from_bjdata(result, true, false) == j);
  1153. }
  1154. }
  1155. SECTION("N = 128..255")
  1156. {
  1157. for (size_t N = 128; N <= 255; ++N)
  1158. {
  1159. CAPTURE(N)
  1160. // create JSON value with string containing of N * 'x'
  1161. const auto s = std::string(N, 'x');
  1162. json const j = s;
  1163. // create expected byte vector
  1164. std::vector<uint8_t> expected;
  1165. expected.push_back('S');
  1166. expected.push_back('U');
  1167. expected.push_back(static_cast<uint8_t>(N));
  1168. for (size_t i = 0; i < N; ++i)
  1169. {
  1170. expected.push_back('x');
  1171. }
  1172. // compare result + size
  1173. const auto result = json::to_bjdata(j);
  1174. CHECK(result == expected);
  1175. CHECK(result.size() == N + 3);
  1176. // check that no null byte is appended
  1177. CHECK(result.back() != '\x00');
  1178. // roundtrip
  1179. CHECK(json::from_bjdata(result) == j);
  1180. CHECK(json::from_bjdata(result, true, false) == j);
  1181. }
  1182. }
  1183. SECTION("N = 256..32767")
  1184. {
  1185. for (const size_t N :
  1186. {
  1187. 256u, 999u, 1025u, 3333u, 2048u, 32767u
  1188. })
  1189. {
  1190. CAPTURE(N)
  1191. // create JSON value with string containing of N * 'x'
  1192. const auto s = std::string(N, 'x');
  1193. json const j = s;
  1194. // create expected byte vector (hack: create string first)
  1195. std::vector<uint8_t> expected(N, 'x');
  1196. // reverse order of commands, because we insert at begin()
  1197. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1198. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1199. expected.insert(expected.begin(), 'I');
  1200. expected.insert(expected.begin(), 'S');
  1201. // compare result + size
  1202. const auto result = json::to_bjdata(j);
  1203. CHECK(result == expected);
  1204. CHECK(result.size() == N + 4);
  1205. // check that no null byte is appended
  1206. CHECK(result.back() != '\x00');
  1207. // roundtrip
  1208. CHECK(json::from_bjdata(result) == j);
  1209. CHECK(json::from_bjdata(result, true, false) == j);
  1210. }
  1211. }
  1212. SECTION("N = 32768..65535")
  1213. {
  1214. for (const size_t N :
  1215. {
  1216. 32768u, 55555u, 65535u
  1217. })
  1218. {
  1219. CAPTURE(N)
  1220. // create JSON value with string containing of N * 'x'
  1221. const auto s = std::string(N, 'x');
  1222. json const j = s;
  1223. // create expected byte vector (hack: create string first)
  1224. std::vector<uint8_t> expected(N, 'x');
  1225. // reverse order of commands, because we insert at begin()
  1226. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1227. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1228. expected.insert(expected.begin(), 'u');
  1229. expected.insert(expected.begin(), 'S');
  1230. // compare result + size
  1231. const auto result = json::to_bjdata(j);
  1232. CHECK(result == expected);
  1233. CHECK(result.size() == N + 4);
  1234. // check that no null byte is appended
  1235. CHECK(result.back() != '\x00');
  1236. // roundtrip
  1237. CHECK(json::from_bjdata(result) == j);
  1238. CHECK(json::from_bjdata(result, true, false) == j);
  1239. }
  1240. }
  1241. SECTION("N = 65536..2147483647")
  1242. {
  1243. for (const size_t N :
  1244. {
  1245. 65536u, 77777u, 1048576u
  1246. })
  1247. {
  1248. CAPTURE(N)
  1249. // create JSON value with string containing of N * 'x'
  1250. const auto s = std::string(N, 'x');
  1251. json const j = s;
  1252. // create expected byte vector (hack: create string first)
  1253. std::vector<uint8_t> expected(N, 'x');
  1254. // reverse order of commands, because we insert at begin()
  1255. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
  1256. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
  1257. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1258. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1259. expected.insert(expected.begin(), 'l');
  1260. expected.insert(expected.begin(), 'S');
  1261. // compare result + size
  1262. const auto result = json::to_bjdata(j);
  1263. CHECK(result == expected);
  1264. CHECK(result.size() == N + 6);
  1265. // check that no null byte is appended
  1266. CHECK(result.back() != '\x00');
  1267. // roundtrip
  1268. CHECK(json::from_bjdata(result) == j);
  1269. CHECK(json::from_bjdata(result, true, false) == j);
  1270. }
  1271. }
  1272. }
  1273. SECTION("binary")
  1274. {
  1275. SECTION("N = 0..127")
  1276. {
  1277. for (std::size_t N = 0; N <= 127; ++N)
  1278. {
  1279. CAPTURE(N)
  1280. // create JSON value with byte array containing of N * 'x'
  1281. const auto s = std::vector<std::uint8_t>(N, 'x');
  1282. json const j = json::binary(s);
  1283. // create expected byte vector
  1284. std::vector<std::uint8_t> expected;
  1285. expected.push_back(static_cast<std::uint8_t>('['));
  1286. if (N != 0)
  1287. {
  1288. expected.push_back(static_cast<std::uint8_t>('$'));
  1289. expected.push_back(static_cast<std::uint8_t>('U'));
  1290. }
  1291. expected.push_back(static_cast<std::uint8_t>('#'));
  1292. expected.push_back(static_cast<std::uint8_t>('i'));
  1293. expected.push_back(static_cast<std::uint8_t>(N));
  1294. for (size_t i = 0; i < N; ++i)
  1295. {
  1296. expected.push_back(0x78);
  1297. }
  1298. // compare result + size
  1299. const auto result = json::to_bjdata(j, true, true);
  1300. CHECK(result == expected);
  1301. if (N == 0)
  1302. {
  1303. CHECK(result.size() == N + 4);
  1304. }
  1305. else
  1306. {
  1307. CHECK(result.size() == N + 6);
  1308. }
  1309. // check that no null byte is appended
  1310. if (N > 0)
  1311. {
  1312. CHECK(result.back() != '\x00');
  1313. }
  1314. // roundtrip only works to an array of numbers
  1315. json j_out = s;
  1316. CHECK(json::from_bjdata(result) == j_out);
  1317. CHECK(json::from_bjdata(result, true, false) == j_out);
  1318. }
  1319. }
  1320. SECTION("N = 128..255")
  1321. {
  1322. for (std::size_t N = 128; N <= 255; ++N)
  1323. {
  1324. CAPTURE(N)
  1325. // create JSON value with byte array containing of N * 'x'
  1326. const auto s = std::vector<std::uint8_t>(N, 'x');
  1327. json const j = json::binary(s);
  1328. // create expected byte vector
  1329. std::vector<uint8_t> expected;
  1330. expected.push_back(static_cast<std::uint8_t>('['));
  1331. expected.push_back(static_cast<std::uint8_t>('$'));
  1332. expected.push_back(static_cast<std::uint8_t>('U'));
  1333. expected.push_back(static_cast<std::uint8_t>('#'));
  1334. expected.push_back(static_cast<std::uint8_t>('U'));
  1335. expected.push_back(static_cast<std::uint8_t>(N));
  1336. for (size_t i = 0; i < N; ++i)
  1337. {
  1338. expected.push_back(0x78);
  1339. }
  1340. // compare result + size
  1341. const auto result = json::to_bjdata(j, true, true);
  1342. CHECK(result == expected);
  1343. CHECK(result.size() == N + 6);
  1344. // check that no null byte is appended
  1345. CHECK(result.back() != '\x00');
  1346. // roundtrip only works to an array of numbers
  1347. json j_out = s;
  1348. CHECK(json::from_bjdata(result) == j_out);
  1349. CHECK(json::from_bjdata(result, true, false) == j_out);
  1350. }
  1351. }
  1352. SECTION("N = 256..32767")
  1353. {
  1354. for (const std::size_t N :
  1355. {
  1356. 256u, 999u, 1025u, 3333u, 2048u, 32767u
  1357. })
  1358. {
  1359. CAPTURE(N)
  1360. // create JSON value with byte array containing of N * 'x'
  1361. const auto s = std::vector<std::uint8_t>(N, 'x');
  1362. json const j = json::binary(s);
  1363. // create expected byte vector
  1364. std::vector<std::uint8_t> expected(N + 7, 'x');
  1365. expected[0] = '[';
  1366. expected[1] = '$';
  1367. expected[2] = 'U';
  1368. expected[3] = '#';
  1369. expected[4] = 'I';
  1370. expected[5] = static_cast<std::uint8_t>(N & 0xFF);
  1371. expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
  1372. // compare result + size
  1373. const auto result = json::to_bjdata(j, true, true);
  1374. CHECK(result == expected);
  1375. CHECK(result.size() == N + 7);
  1376. // check that no null byte is appended
  1377. CHECK(result.back() != '\x00');
  1378. // roundtrip only works to an array of numbers
  1379. json j_out = s;
  1380. CHECK(json::from_bjdata(result) == j_out);
  1381. CHECK(json::from_bjdata(result, true, false) == j_out);
  1382. }
  1383. }
  1384. SECTION("N = 32768..65535")
  1385. {
  1386. for (const std::size_t N :
  1387. {
  1388. 32768u, 55555u, 65535u
  1389. })
  1390. {
  1391. CAPTURE(N)
  1392. // create JSON value with byte array containing of N * 'x'
  1393. const auto s = std::vector<std::uint8_t>(N, 'x');
  1394. json const j = json::binary(s);
  1395. // create expected byte vector
  1396. std::vector<std::uint8_t> expected(N + 7, 'x');
  1397. expected[0] = '[';
  1398. expected[1] = '$';
  1399. expected[2] = 'U';
  1400. expected[3] = '#';
  1401. expected[4] = 'u';
  1402. expected[5] = static_cast<std::uint8_t>(N & 0xFF);
  1403. expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
  1404. // compare result + size
  1405. const auto result = json::to_bjdata(j, true, true);
  1406. CHECK(result == expected);
  1407. CHECK(result.size() == N + 7);
  1408. // check that no null byte is appended
  1409. CHECK(result.back() != '\x00');
  1410. // roundtrip only works to an array of numbers
  1411. json j_out = s;
  1412. CHECK(json::from_bjdata(result) == j_out);
  1413. CHECK(json::from_bjdata(result, true, false) == j_out);
  1414. }
  1415. }
  1416. SECTION("N = 65536..2147483647")
  1417. {
  1418. for (const std::size_t N :
  1419. {
  1420. 65536u, 77777u, 1048576u
  1421. })
  1422. {
  1423. CAPTURE(N)
  1424. // create JSON value with byte array containing of N * 'x'
  1425. const auto s = std::vector<std::uint8_t>(N, 'x');
  1426. json const j = json::binary(s);
  1427. // create expected byte vector
  1428. std::vector<std::uint8_t> expected(N + 9, 'x');
  1429. expected[0] = '[';
  1430. expected[1] = '$';
  1431. expected[2] = 'U';
  1432. expected[3] = '#';
  1433. expected[4] = 'l';
  1434. expected[5] = static_cast<std::uint8_t>(N & 0xFF);
  1435. expected[6] = static_cast<std::uint8_t>((N >> 8) & 0xFF);
  1436. expected[7] = static_cast<std::uint8_t>((N >> 16) & 0xFF);
  1437. expected[8] = static_cast<std::uint8_t>((N >> 24) & 0xFF);
  1438. // compare result + size
  1439. const auto result = json::to_bjdata(j, true, true);
  1440. CHECK(result == expected);
  1441. CHECK(result.size() == N + 9);
  1442. // check that no null byte is appended
  1443. CHECK(result.back() != '\x00');
  1444. // roundtrip only works to an array of numbers
  1445. json j_out = s;
  1446. CHECK(json::from_bjdata(result) == j_out);
  1447. CHECK(json::from_bjdata(result, true, false) == j_out);
  1448. }
  1449. }
  1450. SECTION("Other Serializations")
  1451. {
  1452. const std::size_t N = 10;
  1453. const auto s = std::vector<std::uint8_t>(N, 'x');
  1454. json const j = json::binary(s);
  1455. SECTION("No Count No Type")
  1456. {
  1457. std::vector<uint8_t> expected;
  1458. expected.push_back(static_cast<std::uint8_t>('['));
  1459. for (std::size_t i = 0; i < N; ++i)
  1460. {
  1461. expected.push_back(static_cast<std::uint8_t>('U'));
  1462. expected.push_back(static_cast<std::uint8_t>(0x78));
  1463. }
  1464. expected.push_back(static_cast<std::uint8_t>(']'));
  1465. // compare result + size
  1466. const auto result = json::to_bjdata(j, false, false);
  1467. CHECK(result == expected);
  1468. CHECK(result.size() == N + 12);
  1469. // check that no null byte is appended
  1470. CHECK(result.back() != '\x00');
  1471. // roundtrip only works to an array of numbers
  1472. json j_out = s;
  1473. CHECK(json::from_bjdata(result) == j_out);
  1474. CHECK(json::from_bjdata(result, true, false) == j_out);
  1475. }
  1476. SECTION("Yes Count No Type")
  1477. {
  1478. std::vector<std::uint8_t> expected;
  1479. expected.push_back(static_cast<std::uint8_t>('['));
  1480. expected.push_back(static_cast<std::uint8_t>('#'));
  1481. expected.push_back(static_cast<std::uint8_t>('i'));
  1482. expected.push_back(static_cast<std::uint8_t>(N));
  1483. for (size_t i = 0; i < N; ++i)
  1484. {
  1485. expected.push_back(static_cast<std::uint8_t>('U'));
  1486. expected.push_back(static_cast<std::uint8_t>(0x78));
  1487. }
  1488. // compare result + size
  1489. const auto result = json::to_bjdata(j, true, false);
  1490. CHECK(result == expected);
  1491. CHECK(result.size() == N + 14);
  1492. // check that no null byte is appended
  1493. CHECK(result.back() != '\x00');
  1494. // roundtrip only works to an array of numbers
  1495. json j_out = s;
  1496. CHECK(json::from_bjdata(result) == j_out);
  1497. CHECK(json::from_bjdata(result, true, false) == j_out);
  1498. }
  1499. }
  1500. }
  1501. SECTION("array")
  1502. {
  1503. SECTION("empty")
  1504. {
  1505. SECTION("size=false type=false")
  1506. {
  1507. json const j = json::array();
  1508. std::vector<uint8_t> const expected = {'[', ']'};
  1509. const auto result = json::to_bjdata(j);
  1510. CHECK(result == expected);
  1511. // roundtrip
  1512. CHECK(json::from_bjdata(result) == j);
  1513. CHECK(json::from_bjdata(result, true, false) == j);
  1514. }
  1515. SECTION("size=true type=false")
  1516. {
  1517. json const j = json::array();
  1518. std::vector<uint8_t> const expected = {'[', '#', 'i', 0};
  1519. const auto result = json::to_bjdata(j, true);
  1520. CHECK(result == expected);
  1521. // roundtrip
  1522. CHECK(json::from_bjdata(result) == j);
  1523. CHECK(json::from_bjdata(result, true, false) == j);
  1524. }
  1525. SECTION("size=true type=true")
  1526. {
  1527. json const j = json::array();
  1528. std::vector<uint8_t> const expected = {'[', '#', 'i', 0};
  1529. const auto result = json::to_bjdata(j, true, true);
  1530. CHECK(result == expected);
  1531. // roundtrip
  1532. CHECK(json::from_bjdata(result) == j);
  1533. CHECK(json::from_bjdata(result, true, false) == j);
  1534. }
  1535. }
  1536. SECTION("[null]")
  1537. {
  1538. SECTION("size=false type=false")
  1539. {
  1540. json const j = {nullptr};
  1541. std::vector<uint8_t> const expected = {'[', 'Z', ']'};
  1542. const auto result = json::to_bjdata(j);
  1543. CHECK(result == expected);
  1544. // roundtrip
  1545. CHECK(json::from_bjdata(result) == j);
  1546. CHECK(json::from_bjdata(result, true, false) == j);
  1547. }
  1548. SECTION("size=true type=false")
  1549. {
  1550. json const j = {nullptr};
  1551. std::vector<uint8_t> const expected = {'[', '#', 'i', 1, 'Z'};
  1552. const auto result = json::to_bjdata(j, true);
  1553. CHECK(result == expected);
  1554. // roundtrip
  1555. CHECK(json::from_bjdata(result) == j);
  1556. CHECK(json::from_bjdata(result, true, false) == j);
  1557. }
  1558. SECTION("size=true type=true")
  1559. {
  1560. json const j = {nullptr};
  1561. std::vector<uint8_t> const expected = {'[', '#', 'i', 1, 'Z'};
  1562. const auto result = json::to_bjdata(j, true, true);
  1563. CHECK(result == expected);
  1564. // roundtrip
  1565. CHECK(json::from_bjdata(result) == j);
  1566. CHECK(json::from_bjdata(result, true, false) == j);
  1567. }
  1568. }
  1569. SECTION("[1,2,3,4,5]")
  1570. {
  1571. SECTION("size=false type=false")
  1572. {
  1573. json const j = json::parse("[1,2,3,4,5]");
  1574. std::vector<uint8_t> const expected = {'[', 'i', 1, 'i', 2, 'i', 3, 'i', 4, 'i', 5, ']'};
  1575. const auto result = json::to_bjdata(j);
  1576. CHECK(result == expected);
  1577. // roundtrip
  1578. CHECK(json::from_bjdata(result) == j);
  1579. CHECK(json::from_bjdata(result, true, false) == j);
  1580. }
  1581. SECTION("size=true type=false")
  1582. {
  1583. json const j = json::parse("[1,2,3,4,5]");
  1584. std::vector<uint8_t> const expected = {'[', '#', 'i', 5, 'i', 1, 'i', 2, 'i', 3, 'i', 4, 'i', 5};
  1585. const auto result = json::to_bjdata(j, true);
  1586. CHECK(result == expected);
  1587. // roundtrip
  1588. CHECK(json::from_bjdata(result) == j);
  1589. CHECK(json::from_bjdata(result, true, false) == j);
  1590. }
  1591. SECTION("size=true type=true")
  1592. {
  1593. json const j = json::parse("[1,2,3,4,5]");
  1594. std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 5, 1, 2, 3, 4, 5};
  1595. const auto result = json::to_bjdata(j, true, true);
  1596. CHECK(result == expected);
  1597. // roundtrip
  1598. CHECK(json::from_bjdata(result) == j);
  1599. CHECK(json::from_bjdata(result, true, false) == j);
  1600. }
  1601. }
  1602. SECTION("[[[[]]]]")
  1603. {
  1604. SECTION("size=false type=false")
  1605. {
  1606. json const j = json::parse("[[[[]]]]");
  1607. std::vector<uint8_t> const expected = {'[', '[', '[', '[', ']', ']', ']', ']'};
  1608. const auto result = json::to_bjdata(j);
  1609. CHECK(result == expected);
  1610. // roundtrip
  1611. CHECK(json::from_bjdata(result) == j);
  1612. CHECK(json::from_bjdata(result, true, false) == j);
  1613. }
  1614. SECTION("size=true type=false")
  1615. {
  1616. json const j = json::parse("[[[[]]]]");
  1617. std::vector<uint8_t> const expected = {'[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 0};
  1618. const auto result = json::to_bjdata(j, true);
  1619. CHECK(result == expected);
  1620. // roundtrip
  1621. CHECK(json::from_bjdata(result) == j);
  1622. CHECK(json::from_bjdata(result, true, false) == j);
  1623. }
  1624. SECTION("size=true type=true")
  1625. {
  1626. json const j = json::parse("[[[[]]]]");
  1627. std::vector<uint8_t> const expected = {'[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 1, '[', '#', 'i', 0};
  1628. const auto result = json::to_bjdata(j, true, true);
  1629. CHECK(result == expected);
  1630. // roundtrip
  1631. CHECK(json::from_bjdata(result) == j);
  1632. CHECK(json::from_bjdata(result, true, false) == j);
  1633. }
  1634. }
  1635. SECTION("array with int16_t elements")
  1636. {
  1637. SECTION("size=false type=false")
  1638. {
  1639. json j(257, nullptr);
  1640. std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
  1641. expected[0] = '['; // opening array
  1642. expected[258] = ']'; // closing array
  1643. const auto result = json::to_bjdata(j);
  1644. CHECK(result == expected);
  1645. // roundtrip
  1646. CHECK(json::from_bjdata(result) == j);
  1647. CHECK(json::from_bjdata(result, true, false) == j);
  1648. }
  1649. SECTION("size=true type=false")
  1650. {
  1651. json j(257, nullptr);
  1652. std::vector<uint8_t> expected(j.size() + 5, 'Z'); // all null
  1653. expected[0] = '['; // opening array
  1654. expected[1] = '#'; // array size
  1655. expected[2] = 'I'; // int16
  1656. expected[3] = 0x01; // 0x0101, first byte
  1657. expected[4] = 0x01; // 0x0101, second byte
  1658. const auto result = json::to_bjdata(j, true);
  1659. CHECK(result == expected);
  1660. // roundtrip
  1661. CHECK(json::from_bjdata(result) == j);
  1662. CHECK(json::from_bjdata(result, true, false) == j);
  1663. }
  1664. }
  1665. SECTION("array with uint16_t elements")
  1666. {
  1667. SECTION("size=false type=false")
  1668. {
  1669. json j(32768, nullptr);
  1670. std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
  1671. expected[0] = '['; // opening array
  1672. expected[32769] = ']'; // closing array
  1673. const auto result = json::to_bjdata(j);
  1674. CHECK(result == expected);
  1675. // roundtrip
  1676. CHECK(json::from_bjdata(result) == j);
  1677. CHECK(json::from_bjdata(result, true, false) == j);
  1678. }
  1679. SECTION("size=true type=false")
  1680. {
  1681. json j(32768, nullptr);
  1682. std::vector<uint8_t> expected(j.size() + 5, 'Z'); // all null
  1683. expected[0] = '['; // opening array
  1684. expected[1] = '#'; // array size
  1685. expected[2] = 'u'; // int16
  1686. expected[3] = 0x00; // 0x0101, first byte
  1687. expected[4] = 0x80; // 0x0101, second byte
  1688. const auto result = json::to_bjdata(j, true);
  1689. CHECK(result == expected);
  1690. // roundtrip
  1691. CHECK(json::from_bjdata(result) == j);
  1692. CHECK(json::from_bjdata(result, true, false) == j);
  1693. }
  1694. }
  1695. SECTION("array with int32_t elements")
  1696. {
  1697. SECTION("size=false type=false")
  1698. {
  1699. json j(65793, nullptr);
  1700. std::vector<uint8_t> expected(j.size() + 2, 'Z'); // all null
  1701. expected[0] = '['; // opening array
  1702. expected[65794] = ']'; // closing array
  1703. const auto result = json::to_bjdata(j);
  1704. CHECK(result == expected);
  1705. // roundtrip
  1706. CHECK(json::from_bjdata(result) == j);
  1707. CHECK(json::from_bjdata(result, true, false) == j);
  1708. }
  1709. SECTION("size=true type=false")
  1710. {
  1711. json j(65793, nullptr);
  1712. std::vector<uint8_t> expected(j.size() + 7, 'Z'); // all null
  1713. expected[0] = '['; // opening array
  1714. expected[1] = '#'; // array size
  1715. expected[2] = 'l'; // int32
  1716. expected[3] = 0x01; // 0x00010101, fourth byte
  1717. expected[4] = 0x01; // 0x00010101, third byte
  1718. expected[5] = 0x01; // 0x00010101, second byte
  1719. expected[6] = 0x00; // 0x00010101, first byte
  1720. const auto result = json::to_bjdata(j, true);
  1721. CHECK(result == expected);
  1722. // roundtrip
  1723. CHECK(json::from_bjdata(result) == j);
  1724. CHECK(json::from_bjdata(result, true, false) == j);
  1725. }
  1726. }
  1727. }
  1728. SECTION("object")
  1729. {
  1730. SECTION("empty")
  1731. {
  1732. SECTION("size=false type=false")
  1733. {
  1734. json const j = json::object();
  1735. std::vector<uint8_t> const expected = {'{', '}'};
  1736. const auto result = json::to_bjdata(j);
  1737. CHECK(result == expected);
  1738. // roundtrip
  1739. CHECK(json::from_bjdata(result) == j);
  1740. CHECK(json::from_bjdata(result, true, false) == j);
  1741. }
  1742. SECTION("size=true type=false")
  1743. {
  1744. json const j = json::object();
  1745. std::vector<uint8_t> const expected = {'{', '#', 'i', 0};
  1746. const auto result = json::to_bjdata(j, true);
  1747. CHECK(result == expected);
  1748. // roundtrip
  1749. CHECK(json::from_bjdata(result) == j);
  1750. CHECK(json::from_bjdata(result, true, false) == j);
  1751. }
  1752. SECTION("size=true type=true")
  1753. {
  1754. json const j = json::object();
  1755. std::vector<uint8_t> const expected = {'{', '#', 'i', 0};
  1756. const auto result = json::to_bjdata(j, true, true);
  1757. CHECK(result == expected);
  1758. // roundtrip
  1759. CHECK(json::from_bjdata(result) == j);
  1760. CHECK(json::from_bjdata(result, true, false) == j);
  1761. }
  1762. }
  1763. SECTION("{\"\":null}")
  1764. {
  1765. SECTION("size=false type=false")
  1766. {
  1767. json const j = {{"", nullptr}};
  1768. std::vector<uint8_t> const expected = {'{', 'i', 0, 'Z', '}'};
  1769. const auto result = json::to_bjdata(j);
  1770. CHECK(result == expected);
  1771. // roundtrip
  1772. CHECK(json::from_bjdata(result) == j);
  1773. CHECK(json::from_bjdata(result, true, false) == j);
  1774. }
  1775. SECTION("size=true type=false")
  1776. {
  1777. json const j = {{"", nullptr}};
  1778. std::vector<uint8_t> const expected = {'{', '#', 'i', 1, 'i', 0, 'Z'};
  1779. const auto result = json::to_bjdata(j, true);
  1780. CHECK(result == expected);
  1781. // roundtrip
  1782. CHECK(json::from_bjdata(result) == j);
  1783. CHECK(json::from_bjdata(result, true, false) == j);
  1784. }
  1785. }
  1786. SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
  1787. {
  1788. SECTION("size=false type=false")
  1789. {
  1790. json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
  1791. std::vector<uint8_t> const expected =
  1792. {
  1793. '{', 'i', 1, 'a', '{', 'i', 1, 'b', '{', 'i', 1, 'c', '{', '}', '}', '}', '}'
  1794. };
  1795. const auto result = json::to_bjdata(j);
  1796. CHECK(result == expected);
  1797. // roundtrip
  1798. CHECK(json::from_bjdata(result) == j);
  1799. CHECK(json::from_bjdata(result, true, false) == j);
  1800. }
  1801. SECTION("size=true type=false")
  1802. {
  1803. json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
  1804. std::vector<uint8_t> const expected =
  1805. {
  1806. '{', '#', 'i', 1, 'i', 1, 'a', '{', '#', 'i', 1, 'i', 1, 'b', '{', '#', 'i', 1, 'i', 1, 'c', '{', '#', 'i', 0
  1807. };
  1808. const auto result = json::to_bjdata(j, true);
  1809. CHECK(result == expected);
  1810. // roundtrip
  1811. CHECK(json::from_bjdata(result) == j);
  1812. CHECK(json::from_bjdata(result, true, false) == j);
  1813. }
  1814. SECTION("size=true type=true ignore object type marker")
  1815. {
  1816. json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
  1817. std::vector<uint8_t> const expected =
  1818. {
  1819. '{', '#', 'i', 1, 'i', 1, 'a', '{', '#', 'i', 1, 'i', 1, 'b', '{', '#', 'i', 1, 'i', 1, 'c', '{', '#', 'i', 0
  1820. };
  1821. const auto result = json::to_bjdata(j, true, true);
  1822. CHECK(result == expected);
  1823. // roundtrip
  1824. CHECK(json::from_bjdata(result) == j);
  1825. CHECK(json::from_bjdata(result, true, false) == j);
  1826. }
  1827. }
  1828. }
  1829. }
  1830. SECTION("errors")
  1831. {
  1832. SECTION("strict mode")
  1833. {
  1834. std::vector<uint8_t> const vec = {'Z', 'Z'};
  1835. SECTION("non-strict mode")
  1836. {
  1837. const auto result = json::from_bjdata(vec, false);
  1838. CHECK(result == json());
  1839. }
  1840. SECTION("strict mode")
  1841. {
  1842. json _;
  1843. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec),
  1844. "[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&);
  1845. }
  1846. }
  1847. }
  1848. SECTION("SAX aborts")
  1849. {
  1850. SECTION("start_array()")
  1851. {
  1852. std::vector<uint8_t> const v = {'[', 'T', 'F', ']'};
  1853. SaxCountdown scp(0);
  1854. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1855. }
  1856. SECTION("start_object()")
  1857. {
  1858. std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
  1859. SaxCountdown scp(0);
  1860. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1861. }
  1862. SECTION("key() in object")
  1863. {
  1864. std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
  1865. SaxCountdown scp(1);
  1866. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1867. }
  1868. SECTION("start_array(len)")
  1869. {
  1870. std::vector<uint8_t> const v = {'[', '#', 'i', '2', 'T', 'F'};
  1871. SaxCountdown scp(0);
  1872. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1873. }
  1874. SECTION("start_object(len)")
  1875. {
  1876. std::vector<uint8_t> const v = {'{', '#', 'i', '1', 3, 'f', 'o', 'o', 'F'};
  1877. SaxCountdown scp(0);
  1878. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1879. }
  1880. SECTION("key() in object with length")
  1881. {
  1882. std::vector<uint8_t> const v = {'{', 'i', 3, 'f', 'o', 'o', 'F', '}'};
  1883. SaxCountdown scp(1);
  1884. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1885. }
  1886. SECTION("start_array() in ndarray _ArraySize_")
  1887. {
  1888. std::vector<uint8_t> const v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 1, 2};
  1889. SaxCountdown scp(2);
  1890. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1891. }
  1892. SECTION("number_integer() in ndarray _ArraySize_")
  1893. {
  1894. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 1, 2};
  1895. SaxCountdown scp(3);
  1896. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1897. }
  1898. SECTION("key() in ndarray _ArrayType_")
  1899. {
  1900. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
  1901. SaxCountdown scp(6);
  1902. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1903. }
  1904. SECTION("string() in ndarray _ArrayType_")
  1905. {
  1906. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
  1907. SaxCountdown scp(7);
  1908. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1909. }
  1910. SECTION("key() in ndarray _ArrayData_")
  1911. {
  1912. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
  1913. SaxCountdown scp(8);
  1914. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1915. }
  1916. SECTION("string() in ndarray _ArrayData_")
  1917. {
  1918. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'U', '#', 'i', 2, 2, 2, 1, 2, 3, 4};
  1919. SaxCountdown scp(9);
  1920. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1921. }
  1922. SECTION("string() in ndarray _ArrayType_")
  1923. {
  1924. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 3, 2, 6, 5, 4, 3, 2, 1};
  1925. SaxCountdown scp(11);
  1926. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1927. }
  1928. SECTION("start_array() in ndarray _ArrayData_")
  1929. {
  1930. std::vector<uint8_t> const v = {'[', '$', 'U', '#', '[', 'i', 2, 'i', 3, ']', 6, 5, 4, 3, 2, 1};
  1931. SaxCountdown scp(13);
  1932. CHECK_FALSE(json::sax_parse(v, &scp, json::input_format_t::bjdata));
  1933. }
  1934. }
  1935. SECTION("parsing values")
  1936. {
  1937. SECTION("strings")
  1938. {
  1939. // create a single-character string for all number types
  1940. std::vector<uint8_t> s_i = {'S', 'i', 1, 'a'};
  1941. std::vector<uint8_t> const s_U = {'S', 'U', 1, 'a'};
  1942. std::vector<uint8_t> const s_I = {'S', 'I', 1, 0, 'a'};
  1943. std::vector<uint8_t> const s_u = {'S', 'u', 1, 0, 'a'};
  1944. std::vector<uint8_t> const s_l = {'S', 'l', 1, 0, 0, 0, 'a'};
  1945. std::vector<uint8_t> const s_m = {'S', 'm', 1, 0, 0, 0, 'a'};
  1946. std::vector<uint8_t> const s_L = {'S', 'L', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
  1947. std::vector<uint8_t> const s_M = {'S', 'M', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
  1948. // check if string is parsed correctly to "a"
  1949. CHECK(json::from_bjdata(s_i) == "a");
  1950. CHECK(json::from_bjdata(s_U) == "a");
  1951. CHECK(json::from_bjdata(s_I) == "a");
  1952. CHECK(json::from_bjdata(s_u) == "a");
  1953. CHECK(json::from_bjdata(s_l) == "a");
  1954. CHECK(json::from_bjdata(s_m) == "a");
  1955. CHECK(json::from_bjdata(s_L) == "a");
  1956. CHECK(json::from_bjdata(s_M) == "a");
  1957. // roundtrip: output should be optimized
  1958. CHECK(json::to_bjdata(json::from_bjdata(s_i)) == s_i);
  1959. CHECK(json::to_bjdata(json::from_bjdata(s_U)) == s_i);
  1960. CHECK(json::to_bjdata(json::from_bjdata(s_I)) == s_i);
  1961. CHECK(json::to_bjdata(json::from_bjdata(s_u)) == s_i);
  1962. CHECK(json::to_bjdata(json::from_bjdata(s_l)) == s_i);
  1963. CHECK(json::to_bjdata(json::from_bjdata(s_m)) == s_i);
  1964. CHECK(json::to_bjdata(json::from_bjdata(s_L)) == s_i);
  1965. CHECK(json::to_bjdata(json::from_bjdata(s_M)) == s_i);
  1966. }
  1967. SECTION("number")
  1968. {
  1969. SECTION("float")
  1970. {
  1971. // float32
  1972. std::vector<uint8_t> const v_d = {'d', 0xd0, 0x0f, 0x49, 0x40};
  1973. CHECK(json::from_bjdata(v_d) == 3.14159f);
  1974. // float64
  1975. std::vector<uint8_t> const v_D = {'D', 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40};
  1976. CHECK(json::from_bjdata(v_D) == 3.14159);
  1977. // float32 is serialized as float64 as the library does not support float32
  1978. CHECK(json::to_bjdata(json::from_bjdata(v_d)) == json::to_bjdata(3.14159f));
  1979. }
  1980. }
  1981. SECTION("array")
  1982. {
  1983. SECTION("optimized version (length only)")
  1984. {
  1985. // create vector with two elements of the same type
  1986. std::vector<uint8_t> const v_TU = {'[', '#', 'U', 2, 'T', 'T'};
  1987. std::vector<uint8_t> const v_T = {'[', '#', 'i', 2, 'T', 'T'};
  1988. std::vector<uint8_t> const v_F = {'[', '#', 'i', 2, 'F', 'F'};
  1989. std::vector<uint8_t> const v_Z = {'[', '#', 'i', 2, 'Z', 'Z'};
  1990. std::vector<uint8_t> const v_i = {'[', '#', 'i', 2, 'i', 0x7F, 'i', 0x7F};
  1991. std::vector<uint8_t> const v_U = {'[', '#', 'i', 2, 'U', 0xFF, 'U', 0xFF};
  1992. std::vector<uint8_t> const v_I = {'[', '#', 'i', 2, 'I', 0xFF, 0x7F, 'I', 0xFF, 0x7F};
  1993. std::vector<uint8_t> const v_u = {'[', '#', 'i', 2, 'u', 0x0F, 0xA7, 'u', 0x0F, 0xA7};
  1994. std::vector<uint8_t> const v_l = {'[', '#', 'i', 2, 'l', 0xFF, 0xFF, 0xFF, 0x7F, 'l', 0xFF, 0xFF, 0xFF, 0x7F};
  1995. std::vector<uint8_t> const v_m = {'[', '#', 'i', 2, 'm', 0xFF, 0xC9, 0x9A, 0xBB, 'm', 0xFF, 0xC9, 0x9A, 0xBB};
  1996. 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};
  1997. 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};
  1998. 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};
  1999. std::vector<uint8_t> const v_S = {'[', '#', 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
  2000. std::vector<uint8_t> const v_C = {'[', '#', 'i', 2, 'C', 'a', 'C', 'a'};
  2001. // check if vector is parsed correctly
  2002. CHECK(json::from_bjdata(v_TU) == json({true, true}));
  2003. CHECK(json::from_bjdata(v_T) == json({true, true}));
  2004. CHECK(json::from_bjdata(v_F) == json({false, false}));
  2005. CHECK(json::from_bjdata(v_Z) == json({nullptr, nullptr}));
  2006. CHECK(json::from_bjdata(v_i) == json({127, 127}));
  2007. CHECK(json::from_bjdata(v_U) == json({255, 255}));
  2008. CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
  2009. CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
  2010. CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
  2011. CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
  2012. CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
  2013. CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
  2014. CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
  2015. CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
  2016. CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
  2017. // roundtrip: output should be optimized
  2018. CHECK(json::to_bjdata(json::from_bjdata(v_T), true) == v_T);
  2019. CHECK(json::to_bjdata(json::from_bjdata(v_F), true) == v_F);
  2020. CHECK(json::to_bjdata(json::from_bjdata(v_Z), true) == v_Z);
  2021. CHECK(json::to_bjdata(json::from_bjdata(v_i), true) == v_i);
  2022. CHECK(json::to_bjdata(json::from_bjdata(v_U), true) == v_U);
  2023. CHECK(json::to_bjdata(json::from_bjdata(v_I), true) == v_I);
  2024. CHECK(json::to_bjdata(json::from_bjdata(v_u), true) == v_u);
  2025. CHECK(json::to_bjdata(json::from_bjdata(v_l), true) == v_l);
  2026. CHECK(json::to_bjdata(json::from_bjdata(v_m), true) == v_m);
  2027. CHECK(json::to_bjdata(json::from_bjdata(v_L), true) == v_L);
  2028. CHECK(json::to_bjdata(json::from_bjdata(v_M), true) == v_M);
  2029. CHECK(json::to_bjdata(json::from_bjdata(v_D), true) == v_D);
  2030. CHECK(json::to_bjdata(json::from_bjdata(v_S), true) == v_S);
  2031. CHECK(json::to_bjdata(json::from_bjdata(v_C), true) == v_S); // char is serialized to string
  2032. }
  2033. SECTION("optimized version (type and length)")
  2034. {
  2035. // create vector with two elements of the same type
  2036. std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', 'i', 2, 0x7F, 0x7F};
  2037. std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', 'i', 2, 0xFF, 0xFF};
  2038. std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', 'i', 2, 0xFF, 0x7F, 0xFF, 0x7F};
  2039. std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', 'i', 2, 0x0F, 0xA7, 0x0F, 0xA7};
  2040. std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', 'i', 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
  2041. std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
  2042. 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};
  2043. 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};
  2044. 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};
  2045. std::vector<uint8_t> const v_S = {'[', '#', 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
  2046. std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', 'i', 2, 'a', 'a'};
  2047. // check if vector is parsed correctly
  2048. CHECK(json::from_bjdata(v_i) == json({127, 127}));
  2049. CHECK(json::from_bjdata(v_U) == json({255, 255}));
  2050. CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
  2051. CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
  2052. CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
  2053. CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
  2054. CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
  2055. CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
  2056. CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
  2057. CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
  2058. CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
  2059. // roundtrip: output should be optimized
  2060. std::vector<uint8_t> const v_empty = {'[', '#', 'i', 0};
  2061. CHECK(json::to_bjdata(json::from_bjdata(v_i), true, true) == v_i);
  2062. CHECK(json::to_bjdata(json::from_bjdata(v_U), true, true) == v_U);
  2063. CHECK(json::to_bjdata(json::from_bjdata(v_I), true, true) == v_I);
  2064. CHECK(json::to_bjdata(json::from_bjdata(v_u), true, true) == v_u);
  2065. CHECK(json::to_bjdata(json::from_bjdata(v_l), true, true) == v_l);
  2066. CHECK(json::to_bjdata(json::from_bjdata(v_m), true, true) == v_m);
  2067. CHECK(json::to_bjdata(json::from_bjdata(v_L), true, true) == v_L);
  2068. CHECK(json::to_bjdata(json::from_bjdata(v_M), true, true) == v_M);
  2069. CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
  2070. CHECK(json::to_bjdata(json::from_bjdata(v_S), true, true) == v_S);
  2071. CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_S); // char is serialized to string
  2072. }
  2073. SECTION("optimized ndarray (type and vector-size as optimized 1D array)")
  2074. {
  2075. // create vector with two elements of the same type
  2076. std::vector<uint8_t> const v_0 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 1, 0};
  2077. std::vector<uint8_t> const v_1 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 1, 2, 0x7F, 0x7F};
  2078. std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0x7F, 0x7F};
  2079. std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF};
  2080. std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0x7F, 0xFF, 0x7F};
  2081. std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0x0F, 0xA7, 0x0F, 0xA7};
  2082. std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
  2083. std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
  2084. 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};
  2085. 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};
  2086. 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};
  2087. std::vector<uint8_t> const v_S = {'[', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
  2088. std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '$', 'i', '#', 'i', 2, 1, 2, 'a', 'a'};
  2089. // check if vector is parsed correctly
  2090. CHECK(json::from_bjdata(v_0) == json::array());
  2091. CHECK(json::from_bjdata(v_1) == json({127, 127}));
  2092. CHECK(json::from_bjdata(v_i) == json({127, 127}));
  2093. CHECK(json::from_bjdata(v_U) == json({255, 255}));
  2094. CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
  2095. CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
  2096. CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
  2097. CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
  2098. CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
  2099. CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
  2100. CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
  2101. CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
  2102. CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
  2103. }
  2104. SECTION("optimized ndarray (type and vector-size ndarray with JData annotations)")
  2105. {
  2106. // create vector with 0, 1, 2 elements of the same type
  2107. std::vector<uint8_t> const v_e = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 1, 0xFE, 0xFF};
  2108. std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
  2109. std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
  2110. 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};
  2111. 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};
  2112. 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};
  2113. 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};
  2114. 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};
  2115. 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};
  2116. 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};
  2117. 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};
  2118. std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 'a', 'b', 'c', 'd', 'e', 'f'};
  2119. // check if vector is parsed correctly
  2120. CHECK(json::from_bjdata(v_e) == json({{"_ArrayData_", {254, 255}}, {"_ArraySize_", {2, 1}}, {"_ArrayType_", "uint8"}}));
  2121. CHECK(json::from_bjdata(v_U) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint8"}}));
  2122. CHECK(json::from_bjdata(v_i) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int8"}}));
  2123. CHECK(json::from_bjdata(v_i) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int8"}}));
  2124. CHECK(json::from_bjdata(v_u) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint16"}}));
  2125. CHECK(json::from_bjdata(v_I) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int16"}}));
  2126. CHECK(json::from_bjdata(v_m) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint32"}}));
  2127. CHECK(json::from_bjdata(v_l) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int32"}}));
  2128. CHECK(json::from_bjdata(v_M) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint64"}}));
  2129. CHECK(json::from_bjdata(v_L) == json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "int64"}}));
  2130. 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"}}));
  2131. CHECK(json::from_bjdata(v_D) == json({{"_ArrayData_", {1., 2., 3., 4., 5., 6.}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "double"}}));
  2132. CHECK(json::from_bjdata(v_C) == json({{"_ArrayData_", {'a', 'b', 'c', 'd', 'e', 'f'}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "char"}}));
  2133. // roundtrip: output should be optimized
  2134. CHECK(json::to_bjdata(json::from_bjdata(v_e), true, true) == v_e);
  2135. CHECK(json::to_bjdata(json::from_bjdata(v_U), true, true) == v_U);
  2136. CHECK(json::to_bjdata(json::from_bjdata(v_i), true, true) == v_i);
  2137. CHECK(json::to_bjdata(json::from_bjdata(v_u), true, true) == v_u);
  2138. CHECK(json::to_bjdata(json::from_bjdata(v_I), true, true) == v_I);
  2139. CHECK(json::to_bjdata(json::from_bjdata(v_m), true, true) == v_m);
  2140. CHECK(json::to_bjdata(json::from_bjdata(v_l), true, true) == v_l);
  2141. CHECK(json::to_bjdata(json::from_bjdata(v_M), true, true) == v_M);
  2142. CHECK(json::to_bjdata(json::from_bjdata(v_L), true, true) == v_L);
  2143. CHECK(json::to_bjdata(json::from_bjdata(v_d), true, true) == v_d);
  2144. CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
  2145. CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_C);
  2146. }
  2147. SECTION("optimized ndarray (type and vector-size as 1D array)")
  2148. {
  2149. // create vector with two elements of the same type
  2150. std::vector<uint8_t> const v_0 = {'[', '$', 'i', '#', '[', ']'};
  2151. std::vector<uint8_t> const v_E = {'[', '$', 'i', '#', '[', 'i', 2, 'i', 0, ']'};
  2152. std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', 'i', 1, 'i', 2, ']', 0x7F, 0x7F};
  2153. std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF};
  2154. std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0x7F, 0xFF, 0x7F};
  2155. std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', 'i', 1, 'i', 2, ']', 0x0F, 0xA7, 0x0F, 0xA7};
  2156. std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
  2157. std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
  2158. 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};
  2159. 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};
  2160. 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};
  2161. std::vector<uint8_t> const v_S = {'[', '#', '[', 'i', 1, 'i', 2, ']', 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
  2162. std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', 'i', 1, 'i', 2, ']', 'a', 'a'};
  2163. std::vector<uint8_t> const v_R = {'[', '#', '[', 'i', 2, ']', 'i', 6, 'U', 7};
  2164. // check if vector is parsed correctly
  2165. CHECK(json::from_bjdata(v_0) == json::array());
  2166. CHECK(json::from_bjdata(v_E) == json::array());
  2167. CHECK(json::from_bjdata(v_i) == json({127, 127}));
  2168. CHECK(json::from_bjdata(v_U) == json({255, 255}));
  2169. CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
  2170. CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
  2171. CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
  2172. CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
  2173. CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
  2174. CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
  2175. CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
  2176. CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
  2177. CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
  2178. CHECK(json::from_bjdata(v_R) == json({6, 7}));
  2179. }
  2180. SECTION("optimized ndarray (type and vector-size as size-optimized array)")
  2181. {
  2182. // create vector with two elements of the same type
  2183. std::vector<uint8_t> const v_i = {'[', '$', 'i', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0x7F, 0x7F};
  2184. std::vector<uint8_t> const v_U = {'[', '$', 'U', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF};
  2185. std::vector<uint8_t> const v_I = {'[', '$', 'I', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0x7F, 0xFF, 0x7F};
  2186. std::vector<uint8_t> const v_u = {'[', '$', 'u', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0x0F, 0xA7, 0x0F, 0xA7};
  2187. std::vector<uint8_t> const v_l = {'[', '$', 'l', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F};
  2188. std::vector<uint8_t> const v_m = {'[', '$', 'm', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0xFF, 0xC9, 0x9A, 0xBB};
  2189. 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};
  2190. 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};
  2191. 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};
  2192. std::vector<uint8_t> const v_S = {'[', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 'S', 'i', 1, 'a', 'S', 'i', 1, 'a'};
  2193. std::vector<uint8_t> const v_C = {'[', '$', 'C', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2, 'a', 'a'};
  2194. // check if vector is parsed correctly
  2195. CHECK(json::from_bjdata(v_i) == json({127, 127}));
  2196. CHECK(json::from_bjdata(v_U) == json({255, 255}));
  2197. CHECK(json::from_bjdata(v_I) == json({32767, 32767}));
  2198. CHECK(json::from_bjdata(v_u) == json({42767, 42767}));
  2199. CHECK(json::from_bjdata(v_l) == json({2147483647, 2147483647}));
  2200. CHECK(json::from_bjdata(v_m) == json({3147483647, 3147483647}));
  2201. CHECK(json::from_bjdata(v_L) == json({9223372036854775807, 9223372036854775807}));
  2202. CHECK(json::from_bjdata(v_M) == json({10223372036854775807ull, 10223372036854775807ull}));
  2203. CHECK(json::from_bjdata(v_D) == json({3.1415926, 3.1415926}));
  2204. CHECK(json::from_bjdata(v_S) == json({"a", "a"}));
  2205. CHECK(json::from_bjdata(v_C) == json({"a", "a"}));
  2206. }
  2207. SECTION("invalid ndarray annotations remains as object")
  2208. {
  2209. // check if invalid ND array annotations stay as object
  2210. json j_type = json({{"_ArrayData_", {1, 2, 3, 4, 5, 6}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "invalidtype"}});
  2211. json j_size = json({{"_ArrayData_", {1, 2, 3, 4, 5}}, {"_ArraySize_", {2, 3}}, {"_ArrayType_", "uint8"}});
  2212. // roundtrip: output should stay as object
  2213. CHECK(json::from_bjdata(json::to_bjdata(j_type), true, true) == j_type);
  2214. CHECK(json::from_bjdata(json::to_bjdata(j_size), true, true) == j_size);
  2215. }
  2216. }
  2217. }
  2218. SECTION("parse errors")
  2219. {
  2220. SECTION("empty byte vector")
  2221. {
  2222. json _;
  2223. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(std::vector<uint8_t>()),
  2224. "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing BJData value: unexpected end of input", json::parse_error&);
  2225. }
  2226. SECTION("char")
  2227. {
  2228. SECTION("eof after C byte")
  2229. {
  2230. std::vector<uint8_t> const v = {'C'};
  2231. json _;
  2232. 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&);
  2233. }
  2234. SECTION("byte out of range")
  2235. {
  2236. std::vector<uint8_t> const v = {'C', 130};
  2237. json _;
  2238. 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");
  2239. }
  2240. }
  2241. SECTION("strings")
  2242. {
  2243. SECTION("eof after S byte")
  2244. {
  2245. std::vector<uint8_t> const v = {'S'};
  2246. json _;
  2247. 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&);
  2248. }
  2249. SECTION("invalid byte")
  2250. {
  2251. std::vector<uint8_t> const v = {'S', '1', 'a'};
  2252. json _;
  2253. 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&);
  2254. }
  2255. SECTION("parse bjdata markers in ubjson")
  2256. {
  2257. // create a single-character string for all number types
  2258. std::vector<uint8_t> const s_u = {'S', 'u', 1, 0, 'a'};
  2259. std::vector<uint8_t> const s_m = {'S', 'm', 1, 0, 0, 0, 'a'};
  2260. std::vector<uint8_t> const s_M = {'S', 'M', 1, 0, 0, 0, 0, 0, 0, 0, 'a'};
  2261. json _;
  2262. // check if string is parsed correctly to "a"
  2263. 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&);
  2264. 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&);
  2265. 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&);
  2266. }
  2267. }
  2268. SECTION("array")
  2269. {
  2270. SECTION("optimized array: no size following type")
  2271. {
  2272. std::vector<uint8_t> const v = {'[', '$', 'i', 2};
  2273. json _;
  2274. 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&);
  2275. }
  2276. SECTION("optimized array: negative size")
  2277. {
  2278. std::vector<uint8_t> const v1 = {'[', '#', 'i', 0xF1};
  2279. std::vector<uint8_t> const v2 = {'[', '$', 'I', '#', 'i', 0xF2};
  2280. std::vector<uint8_t> const v3 = {'[', '$', 'I', '#', '[', 'i', 0xF4, 'i', 0x02, ']'};
  2281. std::vector<uint8_t> const v4 = {'[', '$', 0xF6, '#', 'i', 0xF7};
  2282. std::vector<uint8_t> const v5 = {'[', '$', 'I', '#', '[', 'i', 0xF5, 'i', 0xF1, ']'};
  2283. std::vector<uint8_t> const v6 = {'[', '#', '[', 'i', 0xF3, 'i', 0x02, ']'};
  2284. std::vector<uint8_t> const vI = {'[', '#', 'I', 0x00, 0xF1};
  2285. std::vector<uint8_t> const vl = {'[', '#', 'l', 0x00, 0x00, 0x00, 0xF2};
  2286. std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3};
  2287. std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
  2288. std::vector<uint8_t> const vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
  2289. json _;
  2290. 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&);
  2291. CHECK(json::from_bjdata(v1, true, false).is_discarded());
  2292. 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&);
  2293. CHECK(json::from_bjdata(v2, true, false).is_discarded());
  2294. 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&);
  2295. CHECK(json::from_bjdata(v3, true, false).is_discarded());
  2296. 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&);
  2297. CHECK(json::from_bjdata(v4, true, false).is_discarded());
  2298. 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&);
  2299. CHECK(json::from_bjdata(v5, true, false).is_discarded());
  2300. 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&);
  2301. CHECK(json::from_bjdata(v6, true, false).is_discarded());
  2302. 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&);
  2303. CHECK(json::from_bjdata(vI, true, false).is_discarded());
  2304. 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&);
  2305. CHECK(json::from_bjdata(vl, true, false).is_discarded());
  2306. 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&);
  2307. CHECK(json::from_bjdata(vL, true, false).is_discarded());
  2308. #if SIZE_MAX != 0xffffffff
  2309. 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&);
  2310. #else
  2311. 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&);
  2312. #endif
  2313. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  2314. #if SIZE_MAX != 0xffffffff
  2315. 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&);
  2316. #else
  2317. 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&);
  2318. #endif
  2319. CHECK(json::from_bjdata(vMX, true, false).is_discarded());
  2320. }
  2321. SECTION("optimized array: integer value overflow")
  2322. {
  2323. #if SIZE_MAX == 0xffffffff
  2324. std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
  2325. std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
  2326. json _;
  2327. 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&);
  2328. CHECK(json::from_bjdata(vL, true, false).is_discarded());
  2329. 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&);
  2330. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  2331. #endif
  2332. }
  2333. SECTION("do not accept NTFZ markers in ndarray optimized type (with count)")
  2334. {
  2335. json _;
  2336. std::vector<uint8_t> const v_N = {'[', '$', 'N', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
  2337. std::vector<uint8_t> const v_T = {'[', '$', 'T', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
  2338. std::vector<uint8_t> const v_F = {'[', '$', 'F', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
  2339. std::vector<uint8_t> const v_Z = {'[', '$', 'Z', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
  2340. 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&);
  2341. CHECK(json::from_bjdata(v_N, true, false).is_discarded());
  2342. 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&);
  2343. CHECK(json::from_bjdata(v_T, true, false).is_discarded());
  2344. 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&);
  2345. CHECK(json::from_bjdata(v_F, true, false).is_discarded());
  2346. 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&);
  2347. CHECK(json::from_bjdata(v_Z, true, false).is_discarded());
  2348. }
  2349. SECTION("do not accept NTFZ markers in ndarray optimized type (without count)")
  2350. {
  2351. json _;
  2352. std::vector<uint8_t> const v_N = {'[', '$', 'N', '#', '[', 'i', 1, 'i', 2, ']'};
  2353. std::vector<uint8_t> const v_T = {'[', '$', 'T', '#', '[', 'i', 1, 'i', 2, ']'};
  2354. std::vector<uint8_t> const v_F = {'[', '$', 'F', '#', '[', 'i', 1, 'i', 2, ']'};
  2355. std::vector<uint8_t> const v_Z = {'[', '$', 'Z', '#', '[', 'i', 1, 'i', 2, ']'};
  2356. 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&);
  2357. CHECK(json::from_bjdata(v_N, true, false).is_discarded());
  2358. 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&);
  2359. CHECK(json::from_bjdata(v_T, true, false).is_discarded());
  2360. 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&);
  2361. CHECK(json::from_bjdata(v_F, true, false).is_discarded());
  2362. 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&);
  2363. CHECK(json::from_bjdata(v_Z, true, false).is_discarded());
  2364. }
  2365. }
  2366. SECTION("strings")
  2367. {
  2368. std::vector<uint8_t> const vS = {'S'};
  2369. json _;
  2370. 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&);
  2371. CHECK(json::from_bjdata(vS, true, false).is_discarded());
  2372. std::vector<uint8_t> const v = {'S', 'i', '2', 'a'};
  2373. 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&);
  2374. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2375. std::vector<uint8_t> const vC = {'C'};
  2376. 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&);
  2377. CHECK(json::from_bjdata(vC, true, false).is_discarded());
  2378. }
  2379. SECTION("sizes")
  2380. {
  2381. std::vector<uint8_t> const vU = {'[', '#', 'U'};
  2382. json _;
  2383. 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&);
  2384. CHECK(json::from_bjdata(vU, true, false).is_discarded());
  2385. std::vector<uint8_t> const vi = {'[', '#', 'i'};
  2386. 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&);
  2387. CHECK(json::from_bjdata(vi, true, false).is_discarded());
  2388. std::vector<uint8_t> const vI = {'[', '#', 'I'};
  2389. 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&);
  2390. CHECK(json::from_bjdata(vI, true, false).is_discarded());
  2391. std::vector<uint8_t> const vu = {'[', '#', 'u'};
  2392. 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&);
  2393. CHECK(json::from_bjdata(vu, true, false).is_discarded());
  2394. std::vector<uint8_t> const vl = {'[', '#', 'l'};
  2395. 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&);
  2396. CHECK(json::from_bjdata(vl, true, false).is_discarded());
  2397. std::vector<uint8_t> const vm = {'[', '#', 'm'};
  2398. 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&);
  2399. CHECK(json::from_bjdata(vm, true, false).is_discarded());
  2400. std::vector<uint8_t> const vL = {'[', '#', 'L'};
  2401. 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&);
  2402. CHECK(json::from_bjdata(vL, true, false).is_discarded());
  2403. std::vector<uint8_t> const vM = {'[', '#', 'M'};
  2404. 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&);
  2405. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  2406. std::vector<uint8_t> const v0 = {'[', '#', 'T', ']'};
  2407. 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");
  2408. CHECK(json::from_bjdata(v0, true, false).is_discarded());
  2409. }
  2410. SECTION("parse bjdata markers as array size in ubjson")
  2411. {
  2412. json _;
  2413. std::vector<uint8_t> const vu = {'[', '#', 'u'};
  2414. 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&);
  2415. CHECK(json::from_ubjson(vu, true, false).is_discarded());
  2416. std::vector<uint8_t> const vm = {'[', '#', 'm'};
  2417. 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&);
  2418. CHECK(json::from_ubjson(vm, true, false).is_discarded());
  2419. std::vector<uint8_t> const vM = {'[', '#', 'M'};
  2420. 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&);
  2421. CHECK(json::from_ubjson(vM, true, false).is_discarded());
  2422. std::vector<uint8_t> const v0 = {'[', '#', '['};
  2423. 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&);
  2424. CHECK(json::from_ubjson(v0, true, false).is_discarded());
  2425. }
  2426. SECTION("types")
  2427. {
  2428. std::vector<uint8_t> const v0 = {'[', '$'};
  2429. json _;
  2430. 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&);
  2431. CHECK(json::from_bjdata(v0, true, false).is_discarded());
  2432. std::vector<uint8_t> const vi = {'[', '$', '#'};
  2433. 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&);
  2434. CHECK(json::from_bjdata(vi, true, false).is_discarded());
  2435. std::vector<uint8_t> const vU = {'[', '$', 'U'};
  2436. 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&);
  2437. CHECK(json::from_bjdata(vU, true, false).is_discarded());
  2438. std::vector<uint8_t> const v1 = {'[', '$', '['};
  2439. 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&);
  2440. CHECK(json::from_bjdata(v1, true, false).is_discarded());
  2441. }
  2442. SECTION("arrays")
  2443. {
  2444. std::vector<uint8_t> const vST = {'[', '$', 'i', '#', 'i', 2, 1};
  2445. json _;
  2446. 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&);
  2447. CHECK(json::from_bjdata(vST, true, false).is_discarded());
  2448. std::vector<uint8_t> const vS = {'[', '#', 'i', 2, 'i', 1};
  2449. 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&);
  2450. CHECK(json::from_bjdata(vS, true, false).is_discarded());
  2451. std::vector<uint8_t> const v = {'[', 'i', 2, 'i', 1};
  2452. 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&);
  2453. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2454. }
  2455. SECTION("ndarrays")
  2456. {
  2457. std::vector<uint8_t> const vST = {'[', '$', 'i', '#', '[', '$', 'i', '#'};
  2458. json _;
  2459. 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&);
  2460. CHECK(json::from_bjdata(vST, true, false).is_discarded());
  2461. std::vector<uint8_t> const v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2};
  2462. 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&);
  2463. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2464. std::vector<uint8_t> const vS0 = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1};
  2465. 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&);
  2466. CHECK(json::from_bjdata(vS0, true, false).is_discarded());
  2467. std::vector<uint8_t> const vS = {'[', '$', 'i', '#', '[', '#', 'i', 2, 1, 2, 1};
  2468. 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&);
  2469. CHECK(json::from_bjdata(vS, true, false).is_discarded());
  2470. std::vector<uint8_t> const vT = {'[', '$', 'i', '#', '[', 'i', 2, 'i'};
  2471. 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&);
  2472. CHECK(json::from_bjdata(vT, true, false).is_discarded());
  2473. std::vector<uint8_t> const vT0 = {'[', '$', 'i', '#', '[', 'i'};
  2474. 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&);
  2475. CHECK(json::from_bjdata(vT0, true, false).is_discarded());
  2476. std::vector<uint8_t> const vu = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'u', 1, 0};
  2477. 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&);
  2478. CHECK(json::from_bjdata(vu, true, false).is_discarded());
  2479. std::vector<uint8_t> const vm = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'm', 1, 0, 0, 0};
  2480. 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&);
  2481. CHECK(json::from_bjdata(vm, true, false).is_discarded());
  2482. std::vector<uint8_t> const vM = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'M', 1, 0, 0, 0, 0, 0, 0, 0};
  2483. 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&);
  2484. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  2485. std::vector<uint8_t> const vU = {'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 1, 2, 3, 4, 5};
  2486. 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&);
  2487. CHECK(json::from_bjdata(vU, true, false).is_discarded());
  2488. std::vector<uint8_t> const vT1 = {'[', '$', 'T', '#', '[', '$', 'i', '#', 'i', 2, 2, 3};
  2489. CHECK(json::from_bjdata(vT1, true, false).is_discarded());
  2490. std::vector<uint8_t> const vh = {'[', '$', 'h', '#', '[', '$', 'i', '#', 'i', 2, 2, 3};
  2491. CHECK(json::from_bjdata(vh, true, false).is_discarded());
  2492. std::vector<uint8_t> const vR = {'[', '$', 'i', '#', '[', 'i', 1, '[', ']', ']', 1};
  2493. 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&);
  2494. CHECK(json::from_bjdata(vR, true, false).is_discarded());
  2495. std::vector<uint8_t> const vRo = {'[', '$', 'i', '#', '[', 'i', 0, '{', '}', ']', 1};
  2496. 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&);
  2497. CHECK(json::from_bjdata(vRo, true, false).is_discarded());
  2498. std::vector<uint8_t> const vR1 = {'[', '$', 'i', '#', '[', '[', 'i', 1, ']', ']', 1};
  2499. 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&);
  2500. CHECK(json::from_bjdata(vR1, true, false).is_discarded());
  2501. std::vector<uint8_t> const vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
  2502. 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&);
  2503. CHECK(json::from_bjdata(vR2, true, false).is_discarded());
  2504. std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
  2505. 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&);
  2506. CHECK(json::from_bjdata(vR3, true, false).is_discarded());
  2507. std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
  2508. 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&);
  2509. CHECK(json::from_bjdata(vR4, true, false).is_discarded());
  2510. std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
  2511. 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&);
  2512. CHECK(json::from_bjdata(vR5, true, false).is_discarded());
  2513. std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
  2514. 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&);
  2515. CHECK(json::from_bjdata(vR6, true, false).is_discarded());
  2516. std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
  2517. 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&);
  2518. CHECK(json::from_bjdata(vH, true, false).is_discarded());
  2519. }
  2520. SECTION("objects")
  2521. {
  2522. std::vector<uint8_t> const vST = {'{', '$', 'i', '#', 'i', 2, 'i', 1, 'a', 1};
  2523. json _;
  2524. 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&);
  2525. CHECK(json::from_bjdata(vST, true, false).is_discarded());
  2526. std::vector<uint8_t> const vT = {'{', '$', 'i', 'i', 1, 'a', 1};
  2527. 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");
  2528. CHECK(json::from_bjdata(vT, true, false).is_discarded());
  2529. std::vector<uint8_t> const vS = {'{', '#', 'i', 2, 'i', 1, 'a', 'i', 1};
  2530. 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&);
  2531. CHECK(json::from_bjdata(vS, true, false).is_discarded());
  2532. std::vector<uint8_t> const v = {'{', 'i', 1, 'a', 'i', 1};
  2533. 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&);
  2534. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2535. std::vector<uint8_t> const v2 = {'{', 'i', 1, 'a', 'i', 1, 'i'};
  2536. 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&);
  2537. CHECK(json::from_bjdata(v2, true, false).is_discarded());
  2538. std::vector<uint8_t> const v3 = {'{', 'i', 1, 'a'};
  2539. 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&);
  2540. CHECK(json::from_bjdata(v3, true, false).is_discarded());
  2541. std::vector<uint8_t> const vST1 = {'{', '$', 'd', '#', 'i', 2, 'i', 1, 'a'};
  2542. 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&);
  2543. CHECK(json::from_bjdata(vST1, true, false).is_discarded());
  2544. std::vector<uint8_t> const vST2 = {'{', '#', 'i', 2, 'i', 1, 'a'};
  2545. 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&);
  2546. CHECK(json::from_bjdata(vST2, true, false).is_discarded());
  2547. std::vector<uint8_t> const vO = {'{', '#', '[', 'i', 2, 'i', 1, ']', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'i', 2};
  2548. 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&);
  2549. CHECK(json::from_bjdata(vO, true, false).is_discarded());
  2550. std::vector<uint8_t> const vO2 = {'{', '$', 'i', '#', '[', 'i', 2, 'i', 1, ']', 'i', 1, 'a', 1, 'i', 1, 'b', 2};
  2551. 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&);
  2552. CHECK(json::from_bjdata(vO2, true, false).is_discarded());
  2553. }
  2554. }
  2555. SECTION("writing optimized values")
  2556. {
  2557. SECTION("integer")
  2558. {
  2559. SECTION("array of i")
  2560. {
  2561. json const j = {1, -1};
  2562. std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 2, 1, 0xff};
  2563. CHECK(json::to_bjdata(j, true, true) == expected);
  2564. }
  2565. SECTION("array of U")
  2566. {
  2567. json const j = {200, 201};
  2568. std::vector<uint8_t> const expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9};
  2569. CHECK(json::to_bjdata(j, true, true) == expected);
  2570. }
  2571. SECTION("array of I")
  2572. {
  2573. json const j = {30000, -30000};
  2574. std::vector<uint8_t> const expected = {'[', '$', 'I', '#', 'i', 2, 0x30, 0x75, 0xd0, 0x8a};
  2575. CHECK(json::to_bjdata(j, true, true) == expected);
  2576. }
  2577. SECTION("array of u")
  2578. {
  2579. json const j = {50000, 50001};
  2580. std::vector<uint8_t> const expected = {'[', '$', 'u', '#', 'i', 2, 0x50, 0xC3, 0x51, 0xC3};
  2581. CHECK(json::to_bjdata(j, true, true) == expected);
  2582. }
  2583. SECTION("array of l")
  2584. {
  2585. json const j = {70000, -70000};
  2586. std::vector<uint8_t> const expected = {'[', '$', 'l', '#', 'i', 2, 0x70, 0x11, 0x01, 0x00, 0x90, 0xEE, 0xFE, 0xFF};
  2587. CHECK(json::to_bjdata(j, true, true) == expected);
  2588. }
  2589. SECTION("array of m")
  2590. {
  2591. json const j = {3147483647, 3147483648};
  2592. std::vector<uint8_t> const expected = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0x00, 0xCA, 0x9A, 0xBB};
  2593. CHECK(json::to_bjdata(j, true, true) == expected);
  2594. }
  2595. SECTION("array of L")
  2596. {
  2597. json const j = {5000000000, -5000000000};
  2598. 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};
  2599. CHECK(json::to_bjdata(j, true, true) == expected);
  2600. }
  2601. }
  2602. SECTION("unsigned integer")
  2603. {
  2604. SECTION("array of i")
  2605. {
  2606. json const j = {1u, 2u};
  2607. std::vector<uint8_t> const expected = {'[', '$', 'i', '#', 'i', 2, 1, 2};
  2608. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'i', 1, 'i', 2};
  2609. CHECK(json::to_bjdata(j, true, true) == expected);
  2610. CHECK(json::to_bjdata(j, true) == expected_size);
  2611. }
  2612. SECTION("array of U")
  2613. {
  2614. json const j = {200u, 201u};
  2615. std::vector<uint8_t> const expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9};
  2616. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'U', 0xC8, 'U', 0xC9};
  2617. CHECK(json::to_bjdata(j, true, true) == expected);
  2618. CHECK(json::to_bjdata(j, true) == expected_size);
  2619. }
  2620. SECTION("array of I")
  2621. {
  2622. json const j = {30000u, 30001u};
  2623. std::vector<uint8_t> const expected = {'[', '$', 'I', '#', 'i', 2, 0x30, 0x75, 0x31, 0x75};
  2624. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'I', 0x30, 0x75, 'I', 0x31, 0x75};
  2625. CHECK(json::to_bjdata(j, true, true) == expected);
  2626. CHECK(json::to_bjdata(j, true) == expected_size);
  2627. }
  2628. SECTION("array of u")
  2629. {
  2630. json const j = {50000u, 50001u};
  2631. std::vector<uint8_t> const expected = {'[', '$', 'u', '#', 'i', 2, 0x50, 0xC3, 0x51, 0xC3};
  2632. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'u', 0x50, 0xC3, 'u', 0x51, 0xC3};
  2633. CHECK(json::to_bjdata(j, true, true) == expected);
  2634. CHECK(json::to_bjdata(j, true) == expected_size);
  2635. }
  2636. SECTION("array of l")
  2637. {
  2638. json const j = {70000u, 70001u};
  2639. std::vector<uint8_t> const expected = {'[', '$', 'l', '#', 'i', 2, 0x70, 0x11, 0x01, 0x00, 0x71, 0x11, 0x01, 0x00};
  2640. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'l', 0x70, 0x11, 0x01, 0x00, 'l', 0x71, 0x11, 0x01, 0x00};
  2641. CHECK(json::to_bjdata(j, true, true) == expected);
  2642. CHECK(json::to_bjdata(j, true) == expected_size);
  2643. }
  2644. SECTION("array of m")
  2645. {
  2646. json const j = {3147483647u, 3147483648u};
  2647. std::vector<uint8_t> const expected = {'[', '$', 'm', '#', 'i', 2, 0xFF, 0xC9, 0x9A, 0xBB, 0x00, 0xCA, 0x9A, 0xBB};
  2648. std::vector<uint8_t> const expected_size = {'[', '#', 'i', 2, 'm', 0xFF, 0xC9, 0x9A, 0xBB, 'm', 0x00, 0xCA, 0x9A, 0xBB};
  2649. CHECK(json::to_bjdata(j, true, true) == expected);
  2650. CHECK(json::to_bjdata(j, true) == expected_size);
  2651. }
  2652. SECTION("array of L")
  2653. {
  2654. json const j = {5000000000u, 5000000001u};
  2655. 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};
  2656. 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};
  2657. CHECK(json::to_bjdata(j, true, true) == expected);
  2658. CHECK(json::to_bjdata(j, true) == expected_size);
  2659. }
  2660. SECTION("array of M")
  2661. {
  2662. json const j = {10223372036854775807ull, 10223372036854775808ull};
  2663. 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};
  2664. 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};
  2665. CHECK(json::to_bjdata(j, true, true) == expected);
  2666. CHECK(json::to_bjdata(j, true) == expected_size);
  2667. }
  2668. }
  2669. }
  2670. }
  2671. TEST_CASE("Universal Binary JSON Specification Examples 1")
  2672. {
  2673. SECTION("Null Value")
  2674. {
  2675. json const j = {{"passcode", nullptr}};
  2676. std::vector<uint8_t> v = {'{', 'i', 8, 'p', 'a', 's', 's', 'c', 'o', 'd', 'e', 'Z', '}'};
  2677. CHECK(json::to_bjdata(j) == v);
  2678. CHECK(json::from_bjdata(v) == j);
  2679. }
  2680. SECTION("No-Op Value")
  2681. {
  2682. json const j = {"foo", "bar", "baz"};
  2683. std::vector<uint8_t> v = {'[', 'S', 'i', 3, 'f', 'o', 'o',
  2684. 'S', 'i', 3, 'b', 'a', 'r',
  2685. 'S', 'i', 3, 'b', 'a', 'z', ']'
  2686. };
  2687. std::vector<uint8_t> const v2 = {'[', 'S', 'i', 3, 'f', 'o', 'o', 'N',
  2688. 'S', 'i', 3, 'b', 'a', 'r', 'N', 'N', 'N',
  2689. 'S', 'i', 3, 'b', 'a', 'z', 'N', 'N', ']'
  2690. };
  2691. CHECK(json::to_bjdata(j) == v);
  2692. CHECK(json::from_bjdata(v) == j);
  2693. CHECK(json::from_bjdata(v2) == j);
  2694. }
  2695. SECTION("Boolean Types")
  2696. {
  2697. json const j = {{"authorized", true}, {"verified", false}};
  2698. std::vector<uint8_t> v = {'{', 'i', 10, 'a', 'u', 't', 'h', 'o', 'r', 'i', 'z', 'e', 'd', 'T',
  2699. 'i', 8, 'v', 'e', 'r', 'i', 'f', 'i', 'e', 'd', 'F', '}'
  2700. };
  2701. CHECK(json::to_bjdata(j) == v);
  2702. CHECK(json::from_bjdata(v) == j);
  2703. }
  2704. SECTION("Numeric Types")
  2705. {
  2706. json j =
  2707. {
  2708. {"int8", 16},
  2709. {"uint8", 255},
  2710. {"int16", 32767},
  2711. {"uint16", 42767},
  2712. {"int32", 2147483647},
  2713. {"uint32", 3147483647},
  2714. {"int64", 9223372036854775807},
  2715. {"uint64", 10223372036854775807ull},
  2716. {"float64", 113243.7863123}
  2717. };
  2718. std::vector<uint8_t> v = {'{',
  2719. 'i', 7, 'f', 'l', 'o', 'a', 't', '6', '4', 'D', 0xcf, 0x34, 0xbc, 0x94, 0xbc, 0xa5, 0xfb, 0x40,
  2720. 'i', 5, 'i', 'n', 't', '1', '6', 'I', 0xff, 0x7f,
  2721. 'i', 5, 'i', 'n', 't', '3', '2', 'l', 0xff, 0xff, 0xff, 0x7f,
  2722. 'i', 5, 'i', 'n', 't', '6', '4', 'L', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
  2723. 'i', 4, 'i', 'n', 't', '8', 'i', 16,
  2724. 'i', 6, 'u', 'i', 'n', 't', '1', '6', 'u', 0x0F, 0xA7,
  2725. 'i', 6, 'u', 'i', 'n', 't', '3', '2', 'm', 0xFF, 0xC9, 0x9A, 0xBB,
  2726. 'i', 6, 'u', 'i', 'n', 't', '6', '4', 'M', 0xFF, 0xFF, 0x63, 0xA7, 0xB3, 0xB6, 0xE0, 0x8D,
  2727. 'i', 5, 'u', 'i', 'n', 't', '8', 'U', 0xff,
  2728. '}'
  2729. };
  2730. CHECK(json::to_bjdata(j) == v);
  2731. CHECK(json::from_bjdata(v) == j);
  2732. }
  2733. SECTION("Char Type")
  2734. {
  2735. json const j = {{"rolecode", "a"}, {"delim", ";"}};
  2736. 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', '}'};
  2737. //CHECK(json::to_bjdata(j) == v);
  2738. CHECK(json::from_bjdata(v) == j);
  2739. }
  2740. SECTION("String Type")
  2741. {
  2742. SECTION("English")
  2743. {
  2744. json const j = "hello";
  2745. std::vector<uint8_t> v = {'S', 'i', 5, 'h', 'e', 'l', 'l', 'o'};
  2746. CHECK(json::to_bjdata(j) == v);
  2747. CHECK(json::from_bjdata(v) == j);
  2748. }
  2749. SECTION("Russian")
  2750. {
  2751. json const j = "привет";
  2752. std::vector<uint8_t> v = {'S', 'i', 12, 0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82};
  2753. CHECK(json::to_bjdata(j) == v);
  2754. CHECK(json::from_bjdata(v) == j);
  2755. }
  2756. SECTION("Russian")
  2757. {
  2758. json const j = "مرحبا";
  2759. std::vector<uint8_t> v = {'S', 'i', 10, 0xD9, 0x85, 0xD8, 0xB1, 0xD8, 0xAD, 0xD8, 0xA8, 0xD8, 0xA7};
  2760. CHECK(json::to_bjdata(j) == v);
  2761. CHECK(json::from_bjdata(v) == j);
  2762. }
  2763. }
  2764. SECTION("Array Type")
  2765. {
  2766. SECTION("size=false type=false")
  2767. {
  2768. // note the float has been replaced by a double
  2769. json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
  2770. 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', ']'};
  2771. CHECK(json::to_bjdata(j) == v);
  2772. CHECK(json::from_bjdata(v) == j);
  2773. }
  2774. SECTION("size=true type=false")
  2775. {
  2776. // note the float has been replaced by a double
  2777. json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
  2778. 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'};
  2779. CHECK(json::to_bjdata(j, true) == v);
  2780. CHECK(json::from_bjdata(v) == j);
  2781. }
  2782. SECTION("size=true type=true")
  2783. {
  2784. // note the float has been replaced by a double
  2785. json const j = {nullptr, true, false, 4782345193, 153.132, "ham"};
  2786. 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'};
  2787. CHECK(json::to_bjdata(j, true, true) == v);
  2788. CHECK(json::from_bjdata(v) == j);
  2789. }
  2790. }
  2791. SECTION("Object Type")
  2792. {
  2793. SECTION("size=false type=false")
  2794. {
  2795. json j =
  2796. {
  2797. {
  2798. "post", {
  2799. {"id", 1137},
  2800. {"author", "rkalla"},
  2801. {"timestamp", 1364482090592},
  2802. {"body", "I totally agree!"}
  2803. }
  2804. }
  2805. };
  2806. std::vector<uint8_t> v = {'{', 'i', 4, 'p', 'o', 's', 't', '{',
  2807. 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
  2808. 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
  2809. 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
  2810. 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
  2811. '}', '}'
  2812. };
  2813. CHECK(json::to_bjdata(j) == v);
  2814. CHECK(json::from_bjdata(v) == j);
  2815. }
  2816. SECTION("size=true type=false")
  2817. {
  2818. json j =
  2819. {
  2820. {
  2821. "post", {
  2822. {"id", 1137},
  2823. {"author", "rkalla"},
  2824. {"timestamp", 1364482090592},
  2825. {"body", "I totally agree!"}
  2826. }
  2827. }
  2828. };
  2829. std::vector<uint8_t> v = {'{', '#', 'i', 1, 'i', 4, 'p', 'o', 's', 't', '{', '#', 'i', 4,
  2830. 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
  2831. 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
  2832. 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
  2833. 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
  2834. };
  2835. CHECK(json::to_bjdata(j, true) == v);
  2836. CHECK(json::from_bjdata(v) == j);
  2837. }
  2838. SECTION("size=true type=true")
  2839. {
  2840. json j =
  2841. {
  2842. {
  2843. "post", {
  2844. {"id", 1137},
  2845. {"author", "rkalla"},
  2846. {"timestamp", 1364482090592},
  2847. {"body", "I totally agree!"}
  2848. }
  2849. }
  2850. };
  2851. std::vector<uint8_t> v = {'{', '#', 'i', 1, 'i', 4, 'p', 'o', 's', 't', '{', '#', 'i', 4,
  2852. 'i', 6, 'a', 'u', 't', 'h', 'o', 'r', 'S', 'i', 6, 'r', 'k', 'a', 'l', 'l', 'a',
  2853. 'i', 4, 'b', 'o', 'd', 'y', 'S', 'i', 16, 'I', ' ', 't', 'o', 't', 'a', 'l', 'l', 'y', ' ', 'a', 'g', 'r', 'e', 'e', '!',
  2854. 'i', 2, 'i', 'd', 'I', 0x71, 0x04,
  2855. 'i', 9, 't', 'i', 'm', 'e', 's', 't', 'a', 'm', 'p', 'L', 0x60, 0x66, 0x78, 0xB1, 0x3D, 0x01, 0x00, 0x00,
  2856. };
  2857. CHECK(json::to_bjdata(j, true, true) == v);
  2858. CHECK(json::from_bjdata(v) == j);
  2859. }
  2860. }
  2861. SECTION("Optimized Format")
  2862. {
  2863. SECTION("Array Example")
  2864. {
  2865. SECTION("No Optimization")
  2866. {
  2867. // note the floats have been replaced by doubles
  2868. json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
  2869. std::vector<uint8_t> v = {'[',
  2870. 'D', 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
  2871. 'D', 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
  2872. 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2873. 'D', 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
  2874. 'D', 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
  2875. ']'
  2876. };
  2877. CHECK(json::to_bjdata(j) == v);
  2878. CHECK(json::from_bjdata(v) == j);
  2879. }
  2880. SECTION("Optimized with count")
  2881. {
  2882. // note the floats have been replaced by doubles
  2883. json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
  2884. std::vector<uint8_t> v = {'[', '#', 'i', 5,
  2885. 'D', 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
  2886. 'D', 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
  2887. 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2888. 'D', 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
  2889. 'D', 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
  2890. };
  2891. CHECK(json::to_bjdata(j, true) == v);
  2892. CHECK(json::from_bjdata(v) == j);
  2893. }
  2894. SECTION("Optimized with type & count")
  2895. {
  2896. // note the floats have been replaced by doubles
  2897. json const j = {29.97, 31.13, 67.0, 2.113, 23.888};
  2898. std::vector<uint8_t> v = {'[', '$', 'D', '#', 'i', 5,
  2899. 0xb8, 0x1e, 0x85, 0xeb, 0x51, 0xf8, 0x3d, 0x40,
  2900. 0xe1, 0x7a, 0x14, 0xae, 0x47, 0x21, 0x3f, 0x40,
  2901. 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2902. 0x81, 0x95, 0x43, 0x8b, 0x6c, 0xe7, 0x00, 0x40,
  2903. 0x17, 0xd9, 0xce, 0xf7, 0x53, 0xe3, 0x37, 0x40,
  2904. };
  2905. CHECK(json::to_bjdata(j, true, true) == v);
  2906. CHECK(json::from_bjdata(v) == j);
  2907. }
  2908. }
  2909. SECTION("Object Example")
  2910. {
  2911. SECTION("No Optimization")
  2912. {
  2913. // note the floats have been replaced by doubles
  2914. json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
  2915. std::vector<uint8_t> v = {'{',
  2916. 'i', 3, 'a', 'l', 't', 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2917. 'i', 3, 'l', 'a', 't', 'D', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
  2918. 'i', 4, 'l', 'o', 'n', 'g', 'D', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
  2919. '}'
  2920. };
  2921. CHECK(json::to_bjdata(j) == v);
  2922. CHECK(json::from_bjdata(v) == j);
  2923. }
  2924. SECTION("Optimized with count")
  2925. {
  2926. // note the floats have been replaced by doubles
  2927. json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
  2928. std::vector<uint8_t> v = {'{', '#', 'i', 3,
  2929. 'i', 3, 'a', 'l', 't', 'D', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2930. 'i', 3, 'l', 'a', 't', 'D', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
  2931. 'i', 4, 'l', 'o', 'n', 'g', 'D', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
  2932. };
  2933. CHECK(json::to_bjdata(j, true) == v);
  2934. CHECK(json::from_bjdata(v) == j);
  2935. }
  2936. SECTION("Optimized with type & count")
  2937. {
  2938. // note the floats have been replaced by doubles
  2939. json const j = { {"lat", 29.976}, {"long", 31.131}, {"alt", 67.0} };
  2940. std::vector<uint8_t> v = {'{', '$', 'D', '#', 'i', 3,
  2941. 'i', 3, 'a', 'l', 't', 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x50, 0x40,
  2942. 'i', 3, 'l', 'a', 't', 0x60, 0xe5, 0xd0, 0x22, 0xdb, 0xf9, 0x3d, 0x40,
  2943. 'i', 4, 'l', 'o', 'n', 'g', 0xa8, 0xc6, 0x4b, 0x37, 0x89, 0x21, 0x3f, 0x40,
  2944. };
  2945. CHECK(json::to_bjdata(j, true, true) == v);
  2946. CHECK(json::from_bjdata(v) == j);
  2947. }
  2948. }
  2949. SECTION("Special Cases (Null, No-Op and Boolean)")
  2950. {
  2951. SECTION("Array")
  2952. {
  2953. json _;
  2954. std::vector<uint8_t> const v = {'[', '$', 'N', '#', 'I', 0x00, 0x02};
  2955. 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&);
  2956. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2957. }
  2958. SECTION("Object")
  2959. {
  2960. json _;
  2961. 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'};
  2962. 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&);
  2963. CHECK(json::from_bjdata(v, true, false).is_discarded());
  2964. }
  2965. }
  2966. }
  2967. }
  2968. #if !defined(JSON_NOEXCEPTION)
  2969. TEST_CASE("all BJData first bytes")
  2970. {
  2971. // these bytes will fail immediately with exception parse_error.112
  2972. std::set<uint8_t> supported =
  2973. {
  2974. 'T', 'F', 'Z', 'U', 'i', 'I', 'l', 'L', 'd', 'D', 'C', 'S', '[', '{', 'N', 'H', 'u', 'm', 'M', 'h'
  2975. };
  2976. for (auto i = 0; i < 256; ++i)
  2977. {
  2978. const auto byte = static_cast<uint8_t>(i);
  2979. CAPTURE(byte)
  2980. try
  2981. {
  2982. auto res = json::from_bjdata(std::vector<uint8_t>(1, byte));
  2983. }
  2984. catch (const json::parse_error& e)
  2985. {
  2986. // check that parse_error.112 is only thrown if the
  2987. // first byte is not in the supported set
  2988. INFO_WITH_TEMP(e.what());
  2989. if (supported.find(byte) == supported.end())
  2990. {
  2991. CHECK(e.id == 112);
  2992. }
  2993. else
  2994. {
  2995. CHECK(e.id != 112);
  2996. }
  2997. }
  2998. }
  2999. }
  3000. #endif
  3001. TEST_CASE("BJData roundtrips" * doctest::skip())
  3002. {
  3003. SECTION("input from self-generated BJData files")
  3004. {
  3005. for (const std::string filename :
  3006. {
  3007. TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json",
  3008. TEST_DATA_DIRECTORY "/json.org/1.json",
  3009. TEST_DATA_DIRECTORY "/json.org/2.json",
  3010. TEST_DATA_DIRECTORY "/json.org/3.json",
  3011. TEST_DATA_DIRECTORY "/json.org/4.json",
  3012. TEST_DATA_DIRECTORY "/json.org/5.json",
  3013. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json",
  3014. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json",
  3015. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip03.json",
  3016. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip04.json",
  3017. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip05.json",
  3018. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip06.json",
  3019. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip07.json",
  3020. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip08.json",
  3021. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip09.json",
  3022. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip10.json",
  3023. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip11.json",
  3024. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip12.json",
  3025. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip13.json",
  3026. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip14.json",
  3027. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip15.json",
  3028. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip16.json",
  3029. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip17.json",
  3030. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip18.json",
  3031. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip19.json",
  3032. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip20.json",
  3033. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip21.json",
  3034. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip22.json",
  3035. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip23.json",
  3036. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip24.json",
  3037. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip25.json",
  3038. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip26.json",
  3039. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip27.json",
  3040. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip28.json",
  3041. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip29.json",
  3042. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip30.json",
  3043. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip31.json",
  3044. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip32.json",
  3045. TEST_DATA_DIRECTORY "/json_testsuite/sample.json",
  3046. TEST_DATA_DIRECTORY "/json_tests/pass1.json",
  3047. TEST_DATA_DIRECTORY "/json_tests/pass2.json",
  3048. TEST_DATA_DIRECTORY "/json_tests/pass3.json"
  3049. })
  3050. {
  3051. CAPTURE(filename)
  3052. {
  3053. INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
  3054. // parse JSON file
  3055. std::ifstream f_json(filename);
  3056. json j1 = json::parse(f_json);
  3057. // parse BJData file
  3058. auto packed = utils::read_binary_file(filename + ".bjdata");
  3059. json j2;
  3060. CHECK_NOTHROW(j2 = json::from_bjdata(packed));
  3061. // compare parsed JSON values
  3062. CHECK(j1 == j2);
  3063. }
  3064. {
  3065. INFO_WITH_TEMP(filename + ": std::ifstream");
  3066. // parse JSON file
  3067. std::ifstream f_json(filename);
  3068. json j1 = json::parse(f_json);
  3069. // parse BJData file
  3070. std::ifstream f_bjdata(filename + ".bjdata", std::ios::binary);
  3071. json j2;
  3072. CHECK_NOTHROW(j2 = json::from_bjdata(f_bjdata));
  3073. // compare parsed JSON values
  3074. CHECK(j1 == j2);
  3075. }
  3076. {
  3077. INFO_WITH_TEMP(filename + ": output to output adapters");
  3078. // parse JSON file
  3079. std::ifstream f_json(filename);
  3080. json const j1 = json::parse(f_json);
  3081. // parse BJData file
  3082. auto packed = utils::read_binary_file(filename + ".bjdata");
  3083. {
  3084. INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
  3085. std::vector<uint8_t> vec;
  3086. json::to_bjdata(j1, vec);
  3087. CHECK(vec == packed);
  3088. }
  3089. }
  3090. }
  3091. }
  3092. }