unit-cbor.cpp 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++ (test suite)
  4. | | |__ | | | | | | version 3.7.3
  5. |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  7. SPDX-License-Identifier: MIT
  8. Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all
  16. copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. #include "doctest_compatibility.h"
  26. DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
  27. #include <nlohmann/json.hpp>
  28. using nlohmann::json;
  29. #include <fstream>
  30. #include <sstream>
  31. #include <iomanip>
  32. #include <iostream>
  33. #include <set>
  34. #include <test_data.hpp>
  35. namespace
  36. {
  37. class SaxCountdown
  38. {
  39. public:
  40. explicit SaxCountdown(const int count) : events_left(count)
  41. {}
  42. bool null()
  43. {
  44. return events_left-- > 0;
  45. }
  46. bool boolean(bool)
  47. {
  48. return events_left-- > 0;
  49. }
  50. bool number_integer(json::number_integer_t)
  51. {
  52. return events_left-- > 0;
  53. }
  54. bool number_unsigned(json::number_unsigned_t)
  55. {
  56. return events_left-- > 0;
  57. }
  58. bool number_float(json::number_float_t, const std::string&)
  59. {
  60. return events_left-- > 0;
  61. }
  62. bool string(std::string&)
  63. {
  64. return events_left-- > 0;
  65. }
  66. bool binary(std::vector<std::uint8_t>&)
  67. {
  68. return events_left-- > 0;
  69. }
  70. bool start_object(std::size_t)
  71. {
  72. return events_left-- > 0;
  73. }
  74. bool key(std::string&)
  75. {
  76. return events_left-- > 0;
  77. }
  78. bool end_object()
  79. {
  80. return events_left-- > 0;
  81. }
  82. bool start_array(std::size_t)
  83. {
  84. return events_left-- > 0;
  85. }
  86. bool end_array()
  87. {
  88. return events_left-- > 0;
  89. }
  90. bool parse_error(std::size_t, const std::string&, const json::exception&)
  91. {
  92. return false;
  93. }
  94. private:
  95. int events_left = 0;
  96. };
  97. }
  98. TEST_CASE("CBOR")
  99. {
  100. SECTION("individual values")
  101. {
  102. SECTION("discarded")
  103. {
  104. // discarded values are not serialized
  105. json j = json::value_t::discarded;
  106. const auto result = json::to_cbor(j);
  107. CHECK(result.empty());
  108. }
  109. SECTION("NaN")
  110. {
  111. // NaN value
  112. json j = std::numeric_limits<json::number_float_t>::quiet_NaN();
  113. std::vector<uint8_t> expected = {0xf9, 0x7e, 0x00};
  114. const auto result = json::to_cbor(j);
  115. CHECK(result == expected);
  116. }
  117. SECTION("Infinity")
  118. {
  119. // Infinity value
  120. json j = std::numeric_limits<json::number_float_t>::infinity();
  121. std::vector<uint8_t> expected = {0xf9, 0x7c, 0x00};
  122. const auto result = json::to_cbor(j);
  123. CHECK(result == expected);
  124. }
  125. SECTION("null")
  126. {
  127. json j = nullptr;
  128. std::vector<uint8_t> expected = {0xf6};
  129. const auto result = json::to_cbor(j);
  130. CHECK(result == expected);
  131. // roundtrip
  132. CHECK(json::from_cbor(result) == j);
  133. CHECK(json::from_cbor(result, true, false) == j);
  134. }
  135. SECTION("boolean")
  136. {
  137. SECTION("true")
  138. {
  139. json j = true;
  140. std::vector<uint8_t> expected = {0xf5};
  141. const auto result = json::to_cbor(j);
  142. CHECK(result == expected);
  143. // roundtrip
  144. CHECK(json::from_cbor(result) == j);
  145. CHECK(json::from_cbor(result, true, false) == j);
  146. }
  147. SECTION("false")
  148. {
  149. json j = false;
  150. std::vector<uint8_t> expected = {0xf4};
  151. const auto result = json::to_cbor(j);
  152. CHECK(result == expected);
  153. // roundtrip
  154. CHECK(json::from_cbor(result) == j);
  155. CHECK(json::from_cbor(result, true, false) == j);
  156. }
  157. }
  158. SECTION("number")
  159. {
  160. SECTION("signed")
  161. {
  162. SECTION("-9223372036854775808..-4294967297")
  163. {
  164. std::vector<int64_t> numbers;
  165. numbers.push_back(INT64_MIN);
  166. numbers.push_back(-1000000000000000000);
  167. numbers.push_back(-100000000000000000);
  168. numbers.push_back(-10000000000000000);
  169. numbers.push_back(-1000000000000000);
  170. numbers.push_back(-100000000000000);
  171. numbers.push_back(-10000000000000);
  172. numbers.push_back(-1000000000000);
  173. numbers.push_back(-100000000000);
  174. numbers.push_back(-10000000000);
  175. numbers.push_back(-4294967297);
  176. for (auto i : numbers)
  177. {
  178. CAPTURE(i)
  179. // create JSON value with integer number
  180. json j = i;
  181. // check type
  182. CHECK(j.is_number_integer());
  183. // create expected byte vector
  184. std::vector<uint8_t> expected;
  185. expected.push_back(static_cast<uint8_t>(0x3b));
  186. uint64_t positive = static_cast<uint64_t>(-1 - i);
  187. expected.push_back(static_cast<uint8_t>((positive >> 56) & 0xff));
  188. expected.push_back(static_cast<uint8_t>((positive >> 48) & 0xff));
  189. expected.push_back(static_cast<uint8_t>((positive >> 40) & 0xff));
  190. expected.push_back(static_cast<uint8_t>((positive >> 32) & 0xff));
  191. expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
  192. expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
  193. expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
  194. expected.push_back(static_cast<uint8_t>(positive & 0xff));
  195. // compare result + size
  196. const auto result = json::to_cbor(j);
  197. CHECK(result == expected);
  198. CHECK(result.size() == 9);
  199. // check individual bytes
  200. CHECK(result[0] == 0x3b);
  201. uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
  202. (static_cast<uint64_t>(result[2]) << 060) +
  203. (static_cast<uint64_t>(result[3]) << 050) +
  204. (static_cast<uint64_t>(result[4]) << 040) +
  205. (static_cast<uint64_t>(result[5]) << 030) +
  206. (static_cast<uint64_t>(result[6]) << 020) +
  207. (static_cast<uint64_t>(result[7]) << 010) +
  208. static_cast<uint64_t>(result[8]);
  209. CHECK(restored == positive);
  210. CHECK(-1 - static_cast<int64_t>(restored) == i);
  211. // roundtrip
  212. CHECK(json::from_cbor(result) == j);
  213. CHECK(json::from_cbor(result, true, false) == j);
  214. }
  215. }
  216. SECTION("-4294967296..-65537")
  217. {
  218. std::vector<int64_t> numbers;
  219. numbers.push_back(-65537);
  220. numbers.push_back(-100000);
  221. numbers.push_back(-1000000);
  222. numbers.push_back(-10000000);
  223. numbers.push_back(-100000000);
  224. numbers.push_back(-1000000000);
  225. numbers.push_back(-4294967296);
  226. for (auto i : numbers)
  227. {
  228. CAPTURE(i)
  229. // create JSON value with integer number
  230. json j = i;
  231. // check type
  232. CHECK(j.is_number_integer());
  233. // create expected byte vector
  234. std::vector<uint8_t> expected;
  235. expected.push_back(static_cast<uint8_t>(0x3a));
  236. uint32_t positive = static_cast<uint32_t>(static_cast<uint64_t>(-1 - i) & 0x00000000ffffffff);
  237. expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
  238. expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
  239. expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
  240. expected.push_back(static_cast<uint8_t>(positive & 0xff));
  241. // compare result + size
  242. const auto result = json::to_cbor(j);
  243. CHECK(result == expected);
  244. CHECK(result.size() == 5);
  245. // check individual bytes
  246. CHECK(result[0] == 0x3a);
  247. uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
  248. (static_cast<uint32_t>(result[2]) << 020) +
  249. (static_cast<uint32_t>(result[3]) << 010) +
  250. static_cast<uint32_t>(result[4]);
  251. CHECK(restored == positive);
  252. CHECK(-1ll - restored == i);
  253. // roundtrip
  254. CHECK(json::from_cbor(result) == j);
  255. CHECK(json::from_cbor(result, true, false) == j);
  256. }
  257. }
  258. SECTION("-65536..-257")
  259. {
  260. for (int32_t i = -65536; i <= -257; ++i)
  261. {
  262. CAPTURE(i)
  263. // create JSON value with integer number
  264. json j = i;
  265. // check type
  266. CHECK(j.is_number_integer());
  267. // create expected byte vector
  268. std::vector<uint8_t> expected;
  269. expected.push_back(static_cast<uint8_t>(0x39));
  270. uint16_t positive = static_cast<uint16_t>(-1 - i);
  271. expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
  272. expected.push_back(static_cast<uint8_t>(positive & 0xff));
  273. // compare result + size
  274. const auto result = json::to_cbor(j);
  275. CHECK(result == expected);
  276. CHECK(result.size() == 3);
  277. // check individual bytes
  278. CHECK(result[0] == 0x39);
  279. uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
  280. CHECK(restored == positive);
  281. CHECK(-1 - restored == i);
  282. // roundtrip
  283. CHECK(json::from_cbor(result) == j);
  284. CHECK(json::from_cbor(result, true, false) == j);
  285. }
  286. }
  287. SECTION("-9263 (int 16)")
  288. {
  289. json j = -9263;
  290. std::vector<uint8_t> expected = {0x39, 0x24, 0x2e};
  291. const auto result = json::to_cbor(j);
  292. CHECK(result == expected);
  293. int16_t restored = static_cast<int16_t>(-1 - ((result[1] << 8) + result[2]));
  294. CHECK(restored == -9263);
  295. // roundtrip
  296. CHECK(json::from_cbor(result) == j);
  297. CHECK(json::from_cbor(result, true, false) == j);
  298. }
  299. SECTION("-256..-24")
  300. {
  301. for (auto i = -256; i < -24; ++i)
  302. {
  303. CAPTURE(i)
  304. // create JSON value with integer number
  305. json j = i;
  306. // check type
  307. CHECK(j.is_number_integer());
  308. // create expected byte vector
  309. std::vector<uint8_t> expected;
  310. expected.push_back(0x38);
  311. expected.push_back(static_cast<uint8_t>(-1 - i));
  312. // compare result + size
  313. const auto result = json::to_cbor(j);
  314. CHECK(result == expected);
  315. CHECK(result.size() == 2);
  316. // check individual bytes
  317. CHECK(result[0] == 0x38);
  318. CHECK(static_cast<int16_t>(-1 - result[1]) == i);
  319. // roundtrip
  320. CHECK(json::from_cbor(result) == j);
  321. CHECK(json::from_cbor(result, true, false) == j);
  322. }
  323. }
  324. SECTION("-24..-1")
  325. {
  326. for (auto i = -24; i <= -1; ++i)
  327. {
  328. CAPTURE(i)
  329. // create JSON value with integer number
  330. json j = i;
  331. // check type
  332. CHECK(j.is_number_integer());
  333. // create expected byte vector
  334. std::vector<uint8_t> expected;
  335. expected.push_back(static_cast<uint8_t>(0x20 - 1 - static_cast<uint8_t>(i)));
  336. // compare result + size
  337. const auto result = json::to_cbor(j);
  338. CHECK(result == expected);
  339. CHECK(result.size() == 1);
  340. // check individual bytes
  341. CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i);
  342. // roundtrip
  343. CHECK(json::from_cbor(result) == j);
  344. CHECK(json::from_cbor(result, true, false) == j);
  345. }
  346. }
  347. SECTION("0..23")
  348. {
  349. for (size_t i = 0; i <= 23; ++i)
  350. {
  351. CAPTURE(i)
  352. // create JSON value with integer number
  353. json j = -1;
  354. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  355. // check type
  356. CHECK(j.is_number_integer());
  357. // create expected byte vector
  358. std::vector<uint8_t> expected;
  359. expected.push_back(static_cast<uint8_t>(i));
  360. // compare result + size
  361. const auto result = json::to_cbor(j);
  362. CHECK(result == expected);
  363. CHECK(result.size() == 1);
  364. // check individual bytes
  365. CHECK(result[0] == i);
  366. // roundtrip
  367. CHECK(json::from_cbor(result) == j);
  368. CHECK(json::from_cbor(result, true, false) == j);
  369. }
  370. }
  371. SECTION("24..255")
  372. {
  373. for (size_t i = 24; i <= 255; ++i)
  374. {
  375. CAPTURE(i)
  376. // create JSON value with integer number
  377. json j = -1;
  378. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  379. // check type
  380. CHECK(j.is_number_integer());
  381. // create expected byte vector
  382. std::vector<uint8_t> expected;
  383. expected.push_back(static_cast<uint8_t>(0x18));
  384. expected.push_back(static_cast<uint8_t>(i));
  385. // compare result + size
  386. const auto result = json::to_cbor(j);
  387. CHECK(result == expected);
  388. CHECK(result.size() == 2);
  389. // check individual bytes
  390. CHECK(result[0] == 0x18);
  391. CHECK(result[1] == i);
  392. // roundtrip
  393. CHECK(json::from_cbor(result) == j);
  394. CHECK(json::from_cbor(result, true, false) == j);
  395. }
  396. }
  397. SECTION("256..65535")
  398. {
  399. for (size_t i = 256; i <= 65535; ++i)
  400. {
  401. CAPTURE(i)
  402. // create JSON value with integer number
  403. json j = -1;
  404. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  405. // check type
  406. CHECK(j.is_number_integer());
  407. // create expected byte vector
  408. std::vector<uint8_t> expected;
  409. expected.push_back(static_cast<uint8_t>(0x19));
  410. expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
  411. expected.push_back(static_cast<uint8_t>(i & 0xff));
  412. // compare result + size
  413. const auto result = json::to_cbor(j);
  414. CHECK(result == expected);
  415. CHECK(result.size() == 3);
  416. // check individual bytes
  417. CHECK(result[0] == 0x19);
  418. uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
  419. CHECK(restored == i);
  420. // roundtrip
  421. CHECK(json::from_cbor(result) == j);
  422. CHECK(json::from_cbor(result, true, false) == j);
  423. }
  424. }
  425. SECTION("65536..4294967295")
  426. {
  427. for (uint32_t i :
  428. {
  429. 65536u, 77777u, 1048576u
  430. })
  431. {
  432. CAPTURE(i)
  433. // create JSON value with integer number
  434. json j = -1;
  435. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  436. // check type
  437. CHECK(j.is_number_integer());
  438. // create expected byte vector
  439. std::vector<uint8_t> expected;
  440. expected.push_back(0x1a);
  441. expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
  442. expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
  443. expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
  444. expected.push_back(static_cast<uint8_t>(i & 0xff));
  445. // compare result + size
  446. const auto result = json::to_cbor(j);
  447. CHECK(result == expected);
  448. CHECK(result.size() == 5);
  449. // check individual bytes
  450. CHECK(result[0] == 0x1a);
  451. uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
  452. (static_cast<uint32_t>(result[2]) << 020) +
  453. (static_cast<uint32_t>(result[3]) << 010) +
  454. static_cast<uint32_t>(result[4]);
  455. CHECK(restored == i);
  456. // roundtrip
  457. CHECK(json::from_cbor(result) == j);
  458. CHECK(json::from_cbor(result, true, false) == j);
  459. }
  460. }
  461. SECTION("4294967296..4611686018427387903")
  462. {
  463. for (uint64_t i :
  464. {
  465. 4294967296ul, 4611686018427387903ul
  466. })
  467. {
  468. CAPTURE(i)
  469. // create JSON value with integer number
  470. json j = -1;
  471. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  472. // check type
  473. CHECK(j.is_number_integer());
  474. // create expected byte vector
  475. std::vector<uint8_t> expected;
  476. expected.push_back(0x1b);
  477. expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
  478. expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
  479. expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
  480. expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
  481. expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
  482. expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
  483. expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
  484. expected.push_back(static_cast<uint8_t>(i & 0xff));
  485. // compare result + size
  486. const auto result = json::to_cbor(j);
  487. CHECK(result == expected);
  488. CHECK(result.size() == 9);
  489. // check individual bytes
  490. CHECK(result[0] == 0x1b);
  491. uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
  492. (static_cast<uint64_t>(result[2]) << 060) +
  493. (static_cast<uint64_t>(result[3]) << 050) +
  494. (static_cast<uint64_t>(result[4]) << 040) +
  495. (static_cast<uint64_t>(result[5]) << 030) +
  496. (static_cast<uint64_t>(result[6]) << 020) +
  497. (static_cast<uint64_t>(result[7]) << 010) +
  498. static_cast<uint64_t>(result[8]);
  499. CHECK(restored == i);
  500. // roundtrip
  501. CHECK(json::from_cbor(result) == j);
  502. CHECK(json::from_cbor(result, true, false) == j);
  503. }
  504. }
  505. SECTION("-32768..-129 (int 16)")
  506. {
  507. for (int16_t i = -32768; i <= -129; ++i)
  508. {
  509. CAPTURE(i)
  510. // create JSON value with integer number
  511. json j = i;
  512. // check type
  513. CHECK(j.is_number_integer());
  514. // create expected byte vector
  515. std::vector<uint8_t> expected;
  516. expected.push_back(0xd1);
  517. expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
  518. expected.push_back(static_cast<uint8_t>(i & 0xff));
  519. // compare result + size
  520. const auto result = json::to_msgpack(j);
  521. CHECK(result == expected);
  522. CHECK(result.size() == 3);
  523. // check individual bytes
  524. CHECK(result[0] == 0xd1);
  525. int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]);
  526. CHECK(restored == i);
  527. // roundtrip
  528. CHECK(json::from_msgpack(result) == j);
  529. }
  530. }
  531. }
  532. SECTION("unsigned")
  533. {
  534. SECTION("0..23 (Integer)")
  535. {
  536. for (size_t i = 0; i <= 23; ++i)
  537. {
  538. CAPTURE(i)
  539. // create JSON value with unsigned integer number
  540. json j = i;
  541. // check type
  542. CHECK(j.is_number_unsigned());
  543. // create expected byte vector
  544. std::vector<uint8_t> expected;
  545. expected.push_back(static_cast<uint8_t>(i));
  546. // compare result + size
  547. const auto result = json::to_cbor(j);
  548. CHECK(result == expected);
  549. CHECK(result.size() == 1);
  550. // check individual bytes
  551. CHECK(result[0] == i);
  552. // roundtrip
  553. CHECK(json::from_cbor(result) == j);
  554. CHECK(json::from_cbor(result, true, false) == j);
  555. }
  556. }
  557. SECTION("24..255 (one-byte uint8_t)")
  558. {
  559. for (size_t i = 24; i <= 255; ++i)
  560. {
  561. CAPTURE(i)
  562. // create JSON value with unsigned integer number
  563. json j = i;
  564. // check type
  565. CHECK(j.is_number_unsigned());
  566. // create expected byte vector
  567. std::vector<uint8_t> expected;
  568. expected.push_back(0x18);
  569. expected.push_back(static_cast<uint8_t>(i));
  570. // compare result + size
  571. const auto result = json::to_cbor(j);
  572. CHECK(result == expected);
  573. CHECK(result.size() == 2);
  574. // check individual bytes
  575. CHECK(result[0] == 0x18);
  576. uint8_t restored = static_cast<uint8_t>(result[1]);
  577. CHECK(restored == i);
  578. // roundtrip
  579. CHECK(json::from_cbor(result) == j);
  580. CHECK(json::from_cbor(result, true, false) == j);
  581. }
  582. }
  583. SECTION("256..65535 (two-byte uint16_t)")
  584. {
  585. for (size_t i = 256; i <= 65535; ++i)
  586. {
  587. CAPTURE(i)
  588. // create JSON value with unsigned integer number
  589. json j = i;
  590. // check type
  591. CHECK(j.is_number_unsigned());
  592. // create expected byte vector
  593. std::vector<uint8_t> expected;
  594. expected.push_back(0x19);
  595. expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
  596. expected.push_back(static_cast<uint8_t>(i & 0xff));
  597. // compare result + size
  598. const auto result = json::to_cbor(j);
  599. CHECK(result == expected);
  600. CHECK(result.size() == 3);
  601. // check individual bytes
  602. CHECK(result[0] == 0x19);
  603. uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
  604. CHECK(restored == i);
  605. // roundtrip
  606. CHECK(json::from_cbor(result) == j);
  607. CHECK(json::from_cbor(result, true, false) == j);
  608. }
  609. }
  610. SECTION("65536..4294967295 (four-byte uint32_t)")
  611. {
  612. for (uint32_t i :
  613. {
  614. 65536u, 77777u, 1048576u
  615. })
  616. {
  617. CAPTURE(i)
  618. // create JSON value with unsigned integer number
  619. json j = i;
  620. // check type
  621. CHECK(j.is_number_unsigned());
  622. // create expected byte vector
  623. std::vector<uint8_t> expected;
  624. expected.push_back(0x1a);
  625. expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
  626. expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
  627. expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
  628. expected.push_back(static_cast<uint8_t>(i & 0xff));
  629. // compare result + size
  630. const auto result = json::to_cbor(j);
  631. CHECK(result == expected);
  632. CHECK(result.size() == 5);
  633. // check individual bytes
  634. CHECK(result[0] == 0x1a);
  635. uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
  636. (static_cast<uint32_t>(result[2]) << 020) +
  637. (static_cast<uint32_t>(result[3]) << 010) +
  638. static_cast<uint32_t>(result[4]);
  639. CHECK(restored == i);
  640. // roundtrip
  641. CHECK(json::from_cbor(result) == j);
  642. CHECK(json::from_cbor(result, true, false) == j);
  643. }
  644. }
  645. SECTION("4294967296..4611686018427387903 (eight-byte uint64_t)")
  646. {
  647. for (uint64_t i :
  648. {
  649. 4294967296ul, 4611686018427387903ul
  650. })
  651. {
  652. CAPTURE(i)
  653. // create JSON value with integer number
  654. json j = i;
  655. // check type
  656. CHECK(j.is_number_unsigned());
  657. // create expected byte vector
  658. std::vector<uint8_t> expected;
  659. expected.push_back(0x1b);
  660. expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
  661. expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
  662. expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
  663. expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
  664. expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
  665. expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
  666. expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
  667. expected.push_back(static_cast<uint8_t>(i & 0xff));
  668. // compare result + size
  669. const auto result = json::to_cbor(j);
  670. CHECK(result == expected);
  671. CHECK(result.size() == 9);
  672. // check individual bytes
  673. CHECK(result[0] == 0x1b);
  674. uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
  675. (static_cast<uint64_t>(result[2]) << 060) +
  676. (static_cast<uint64_t>(result[3]) << 050) +
  677. (static_cast<uint64_t>(result[4]) << 040) +
  678. (static_cast<uint64_t>(result[5]) << 030) +
  679. (static_cast<uint64_t>(result[6]) << 020) +
  680. (static_cast<uint64_t>(result[7]) << 010) +
  681. static_cast<uint64_t>(result[8]);
  682. CHECK(restored == i);
  683. // roundtrip
  684. CHECK(json::from_cbor(result) == j);
  685. CHECK(json::from_cbor(result, true, false) == j);
  686. }
  687. }
  688. }
  689. SECTION("double-precision float")
  690. {
  691. SECTION("3.1415925")
  692. {
  693. double v = 3.1415925;
  694. json j = v;
  695. std::vector<uint8_t> expected =
  696. {
  697. 0xfb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
  698. };
  699. const auto result = json::to_cbor(j);
  700. CHECK(result == expected);
  701. // roundtrip
  702. CHECK(json::from_cbor(result) == j);
  703. CHECK(json::from_cbor(result) == v);
  704. CHECK(json::from_cbor(result, true, false) == j);
  705. }
  706. }
  707. SECTION("single-precision float")
  708. {
  709. SECTION("0.5")
  710. {
  711. double v = 0.5;
  712. json j = v;
  713. // its double-precision float binary value is
  714. // {0xfb, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  715. // but to save memory, we can store it as single-precision float.
  716. std::vector<uint8_t> expected = {0xfa, 0x3f, 0x00, 0x00, 0x00};
  717. const auto result = json::to_cbor(j);
  718. CHECK(result == expected);
  719. // roundtrip
  720. CHECK(json::from_cbor(result) == j);
  721. CHECK(json::from_cbor(result) == v);
  722. }
  723. SECTION("0.0")
  724. {
  725. double v = 0.0;
  726. json j = v;
  727. // its double-precision binary value is:
  728. // {0xfb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  729. std::vector<uint8_t> expected = {0xfa, 0x00, 0x00, 0x00, 0x00};
  730. const auto result = json::to_cbor(j);
  731. CHECK(result == expected);
  732. // roundtrip
  733. CHECK(json::from_cbor(result) == j);
  734. CHECK(json::from_cbor(result) == v);
  735. }
  736. SECTION("-0.0")
  737. {
  738. double v = -0.0;
  739. json j = v;
  740. // its double-precision binary value is:
  741. // {0xfb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  742. std::vector<uint8_t> expected = {0xfa, 0x80, 0x00, 0x00, 0x00};
  743. const auto result = json::to_cbor(j);
  744. CHECK(result == expected);
  745. // roundtrip
  746. CHECK(json::from_cbor(result) == j);
  747. CHECK(json::from_cbor(result) == v);
  748. }
  749. SECTION("100.0")
  750. {
  751. double v = 100.0;
  752. json j = v;
  753. // its double-precision binary value is:
  754. // {0xfb, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  755. std::vector<uint8_t> expected = {0xfa, 0x42, 0xc8, 0x00, 0x00};
  756. const auto result = json::to_cbor(j);
  757. CHECK(result == expected);
  758. // roundtrip
  759. CHECK(json::from_cbor(result) == j);
  760. CHECK(json::from_cbor(result) == v);
  761. }
  762. SECTION("200.0")
  763. {
  764. double v = 200.0;
  765. json j = v;
  766. // its double-precision binary value is:
  767. // {0xfb, 0x40, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  768. std::vector<uint8_t> expected = {0xfa, 0x43, 0x48, 0x00, 0x00};
  769. const auto result = json::to_cbor(j);
  770. CHECK(result == expected);
  771. // roundtrip
  772. CHECK(json::from_cbor(result) == j);
  773. CHECK(json::from_cbor(result) == v);
  774. }
  775. SECTION("3.40282e+38(max float)")
  776. {
  777. float v = (std::numeric_limits<float>::max)();
  778. json j = v;
  779. std::vector<uint8_t> expected =
  780. {
  781. 0xfa, 0x7f, 0x7f, 0xff, 0xff
  782. };
  783. const auto result = json::to_cbor(j);
  784. CHECK(result == expected);
  785. // roundtrip
  786. CHECK(json::from_cbor(result) == j);
  787. CHECK(json::from_cbor(result) == v);
  788. }
  789. SECTION("-3.40282e+38(lowest float)")
  790. {
  791. double v = static_cast<double>(std::numeric_limits<float>::lowest());
  792. json j = v;
  793. std::vector<uint8_t> expected =
  794. {
  795. 0xfa, 0xff, 0x7f, 0xff, 0xff
  796. };
  797. const auto result = json::to_cbor(j);
  798. CHECK(result == expected);
  799. // roundtrip
  800. CHECK(json::from_cbor(result) == j);
  801. CHECK(json::from_cbor(result) == v);
  802. }
  803. SECTION("1 + 3.40282e+38(more than max float)")
  804. {
  805. double v = static_cast<double>((std::numeric_limits<float>::max)()) + 0.1e+34;
  806. json j = v;
  807. std::vector<uint8_t> expected =
  808. {
  809. 0xfb, 0x47, 0xf0, 0x00, 0x03, 0x04, 0xdc, 0x64, 0x49
  810. };
  811. // double
  812. const auto result = json::to_cbor(j);
  813. CHECK(result == expected);
  814. // roundtrip
  815. CHECK(json::from_cbor(result) == j);
  816. CHECK(json::from_cbor(result) == v);
  817. }
  818. SECTION("-1 - 3.40282e+38(less than lowest float)")
  819. {
  820. double v = static_cast<double>(std::numeric_limits<float>::lowest()) - 1.0;
  821. json j = v;
  822. std::vector<uint8_t> expected =
  823. {
  824. 0xfa, 0xff, 0x7f, 0xff, 0xff
  825. };
  826. // the same with lowest float
  827. const auto result = json::to_cbor(j);
  828. CHECK(result == expected);
  829. // roundtrip
  830. CHECK(json::from_cbor(result) == j);
  831. CHECK(json::from_cbor(result) == v);
  832. }
  833. }
  834. SECTION("half-precision float (edge cases)")
  835. {
  836. SECTION("errors")
  837. {
  838. SECTION("no byte follows")
  839. {
  840. json _;
  841. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf9})), json::parse_error&);
  842. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf9})),
  843. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
  844. CHECK(json::from_cbor(std::vector<uint8_t>({0xf9}), true, false).is_discarded());
  845. }
  846. SECTION("only one byte follows")
  847. {
  848. json _;
  849. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})), json::parse_error&);
  850. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c})),
  851. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
  852. CHECK(json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c}), true, false).is_discarded());
  853. }
  854. }
  855. SECTION("exp = 0b00000")
  856. {
  857. SECTION("0 (0 00000 0000000000)")
  858. {
  859. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x00}));
  860. json::number_float_t d = j;
  861. CHECK(d == 0.0);
  862. }
  863. SECTION("-0 (1 00000 0000000000)")
  864. {
  865. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x80, 0x00}));
  866. json::number_float_t d = j;
  867. CHECK(d == -0.0);
  868. }
  869. SECTION("2**-24 (0 00000 0000000001)")
  870. {
  871. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x01}));
  872. json::number_float_t d = j;
  873. CHECK(d == std::pow(2.0, -24.0));
  874. }
  875. }
  876. SECTION("exp = 0b11111")
  877. {
  878. SECTION("infinity (0 11111 0000000000)")
  879. {
  880. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x00}));
  881. json::number_float_t d = j;
  882. CHECK(d == std::numeric_limits<json::number_float_t>::infinity());
  883. CHECK(j.dump() == "null");
  884. }
  885. SECTION("-infinity (1 11111 0000000000)")
  886. {
  887. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0xfc, 0x00}));
  888. json::number_float_t d = j;
  889. CHECK(d == -std::numeric_limits<json::number_float_t>::infinity());
  890. CHECK(j.dump() == "null");
  891. }
  892. }
  893. SECTION("other values from https://en.wikipedia.org/wiki/Half-precision_floating-point_format")
  894. {
  895. SECTION("1 (0 01111 0000000000)")
  896. {
  897. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x3c, 0x00}));
  898. json::number_float_t d = j;
  899. CHECK(d == 1);
  900. }
  901. SECTION("-2 (1 10000 0000000000)")
  902. {
  903. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0xc0, 0x00}));
  904. json::number_float_t d = j;
  905. CHECK(d == -2);
  906. }
  907. SECTION("65504 (0 11110 1111111111)")
  908. {
  909. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7b, 0xff}));
  910. json::number_float_t d = j;
  911. CHECK(d == 65504);
  912. }
  913. }
  914. SECTION("infinity")
  915. {
  916. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x00}));
  917. json::number_float_t d = j;
  918. CHECK(not std::isfinite(d));
  919. CHECK(j.dump() == "null");
  920. }
  921. SECTION("NaN")
  922. {
  923. json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7e, 0x00}));
  924. json::number_float_t d = j;
  925. CHECK(std::isnan(d));
  926. CHECK(j.dump() == "null");
  927. }
  928. }
  929. }
  930. SECTION("string")
  931. {
  932. SECTION("N = 0..23")
  933. {
  934. for (size_t N = 0; N <= 0x17; ++N)
  935. {
  936. CAPTURE(N)
  937. // create JSON value with string containing of N * 'x'
  938. const auto s = std::string(N, 'x');
  939. json j = s;
  940. // create expected byte vector
  941. std::vector<uint8_t> expected;
  942. expected.push_back(static_cast<uint8_t>(0x60 + N));
  943. for (size_t i = 0; i < N; ++i)
  944. {
  945. expected.push_back('x');
  946. }
  947. // compare result + size
  948. const auto result = json::to_cbor(j);
  949. CHECK(result == expected);
  950. CHECK(result.size() == N + 1);
  951. // check that no null byte is appended
  952. if (N > 0)
  953. {
  954. CHECK(result.back() != '\x00');
  955. }
  956. // roundtrip
  957. CHECK(json::from_cbor(result) == j);
  958. CHECK(json::from_cbor(result, true, false) == j);
  959. }
  960. }
  961. SECTION("N = 24..255")
  962. {
  963. for (size_t N = 24; N <= 255; ++N)
  964. {
  965. CAPTURE(N)
  966. // create JSON value with string containing of N * 'x'
  967. const auto s = std::string(N, 'x');
  968. json j = s;
  969. // create expected byte vector
  970. std::vector<uint8_t> expected;
  971. expected.push_back(0x78);
  972. expected.push_back(static_cast<uint8_t>(N));
  973. for (size_t i = 0; i < N; ++i)
  974. {
  975. expected.push_back('x');
  976. }
  977. // compare result + size
  978. const auto result = json::to_cbor(j);
  979. CHECK(result == expected);
  980. CHECK(result.size() == N + 2);
  981. // check that no null byte is appended
  982. CHECK(result.back() != '\x00');
  983. // roundtrip
  984. CHECK(json::from_cbor(result) == j);
  985. CHECK(json::from_cbor(result, true, false) == j);
  986. }
  987. }
  988. SECTION("N = 256..65535")
  989. {
  990. for (size_t N :
  991. {
  992. 256u, 999u, 1025u, 3333u, 2048u, 65535u
  993. })
  994. {
  995. CAPTURE(N)
  996. // create JSON value with string containing of N * 'x'
  997. const auto s = std::string(N, 'x');
  998. json j = s;
  999. // create expected byte vector (hack: create string first)
  1000. std::vector<uint8_t> expected(N, 'x');
  1001. // reverse order of commands, because we insert at begin()
  1002. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1003. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1004. expected.insert(expected.begin(), 0x79);
  1005. // compare result + size
  1006. const auto result = json::to_cbor(j);
  1007. CHECK(result == expected);
  1008. CHECK(result.size() == N + 3);
  1009. // check that no null byte is appended
  1010. CHECK(result.back() != '\x00');
  1011. // roundtrip
  1012. CHECK(json::from_cbor(result) == j);
  1013. CHECK(json::from_cbor(result, true, false) == j);
  1014. }
  1015. }
  1016. SECTION("N = 65536..4294967295")
  1017. {
  1018. for (size_t N :
  1019. {
  1020. 65536u, 77777u, 1048576u
  1021. })
  1022. {
  1023. CAPTURE(N)
  1024. // create JSON value with string containing of N * 'x'
  1025. const auto s = std::string(N, 'x');
  1026. json j = s;
  1027. // create expected byte vector (hack: create string first)
  1028. std::vector<uint8_t> expected(N, 'x');
  1029. // reverse order of commands, because we insert at begin()
  1030. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1031. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1032. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
  1033. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
  1034. expected.insert(expected.begin(), 0x7a);
  1035. // compare result + size
  1036. const auto result = json::to_cbor(j);
  1037. CHECK(result == expected);
  1038. CHECK(result.size() == N + 5);
  1039. // check that no null byte is appended
  1040. CHECK(result.back() != '\x00');
  1041. // roundtrip
  1042. CHECK(json::from_cbor(result) == j);
  1043. CHECK(json::from_cbor(result, true, false) == j);
  1044. }
  1045. }
  1046. }
  1047. SECTION("array")
  1048. {
  1049. SECTION("empty")
  1050. {
  1051. json j = json::array();
  1052. std::vector<uint8_t> expected = {0x80};
  1053. const auto result = json::to_cbor(j);
  1054. CHECK(result == expected);
  1055. // roundtrip
  1056. CHECK(json::from_cbor(result) == j);
  1057. CHECK(json::from_cbor(result, true, false) == j);
  1058. }
  1059. SECTION("[null]")
  1060. {
  1061. json j = {nullptr};
  1062. std::vector<uint8_t> expected = {0x81, 0xf6};
  1063. const auto result = json::to_cbor(j);
  1064. CHECK(result == expected);
  1065. // roundtrip
  1066. CHECK(json::from_cbor(result) == j);
  1067. CHECK(json::from_cbor(result, true, false) == j);
  1068. }
  1069. SECTION("[1,2,3,4,5]")
  1070. {
  1071. json j = json::parse("[1,2,3,4,5]");
  1072. std::vector<uint8_t> expected = {0x85, 0x01, 0x02, 0x03, 0x04, 0x05};
  1073. const auto result = json::to_cbor(j);
  1074. CHECK(result == expected);
  1075. // roundtrip
  1076. CHECK(json::from_cbor(result) == j);
  1077. CHECK(json::from_cbor(result, true, false) == j);
  1078. }
  1079. SECTION("[[[[]]]]")
  1080. {
  1081. json j = json::parse("[[[[]]]]");
  1082. std::vector<uint8_t> expected = {0x81, 0x81, 0x81, 0x80};
  1083. const auto result = json::to_cbor(j);
  1084. CHECK(result == expected);
  1085. // roundtrip
  1086. CHECK(json::from_cbor(result) == j);
  1087. CHECK(json::from_cbor(result, true, false) == j);
  1088. }
  1089. SECTION("array with uint16_t elements")
  1090. {
  1091. json j(257, nullptr);
  1092. std::vector<uint8_t> expected(j.size() + 3, 0xf6); // all null
  1093. expected[0] = 0x99; // array 16 bit
  1094. expected[1] = 0x01; // size (0x0101), byte 0
  1095. expected[2] = 0x01; // size (0x0101), byte 1
  1096. const auto result = json::to_cbor(j);
  1097. CHECK(result == expected);
  1098. // roundtrip
  1099. CHECK(json::from_cbor(result) == j);
  1100. CHECK(json::from_cbor(result, true, false) == j);
  1101. }
  1102. SECTION("array with uint32_t elements")
  1103. {
  1104. json j(65793, nullptr);
  1105. std::vector<uint8_t> expected(j.size() + 5, 0xf6); // all null
  1106. expected[0] = 0x9a; // array 32 bit
  1107. expected[1] = 0x00; // size (0x00010101), byte 0
  1108. expected[2] = 0x01; // size (0x00010101), byte 1
  1109. expected[3] = 0x01; // size (0x00010101), byte 2
  1110. expected[4] = 0x01; // size (0x00010101), byte 3
  1111. const auto result = json::to_cbor(j);
  1112. CHECK(result == expected);
  1113. // roundtrip
  1114. CHECK(json::from_cbor(result) == j);
  1115. CHECK(json::from_cbor(result, true, false) == j);
  1116. }
  1117. }
  1118. SECTION("object")
  1119. {
  1120. SECTION("empty")
  1121. {
  1122. json j = json::object();
  1123. std::vector<uint8_t> expected = {0xa0};
  1124. const auto result = json::to_cbor(j);
  1125. CHECK(result == expected);
  1126. // roundtrip
  1127. CHECK(json::from_cbor(result) == j);
  1128. CHECK(json::from_cbor(result, true, false) == j);
  1129. }
  1130. SECTION("{\"\":null}")
  1131. {
  1132. json j = {{"", nullptr}};
  1133. std::vector<uint8_t> expected = {0xa1, 0x60, 0xf6};
  1134. const auto result = json::to_cbor(j);
  1135. CHECK(result == expected);
  1136. // roundtrip
  1137. CHECK(json::from_cbor(result) == j);
  1138. CHECK(json::from_cbor(result, true, false) == j);
  1139. }
  1140. SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
  1141. {
  1142. json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
  1143. std::vector<uint8_t> expected =
  1144. {
  1145. 0xa1, 0x61, 0x61, 0xa1, 0x61, 0x62, 0xa1, 0x61, 0x63, 0xa0
  1146. };
  1147. const auto result = json::to_cbor(j);
  1148. CHECK(result == expected);
  1149. // roundtrip
  1150. CHECK(json::from_cbor(result) == j);
  1151. CHECK(json::from_cbor(result, true, false) == j);
  1152. }
  1153. SECTION("object with uint8_t elements")
  1154. {
  1155. json j;
  1156. for (auto i = 0; i < 255; ++i)
  1157. {
  1158. // format i to a fixed width of 5
  1159. // each entry will need 7 bytes: 6 for string, 1 for null
  1160. std::stringstream ss;
  1161. ss << std::setw(5) << std::setfill('0') << i;
  1162. j.emplace(ss.str(), nullptr);
  1163. }
  1164. const auto result = json::to_cbor(j);
  1165. // Checking against an expected vector byte by byte is
  1166. // difficult, because no assumption on the order of key/value
  1167. // pairs are made. We therefore only check the prefix (type and
  1168. // size and the overall size. The rest is then handled in the
  1169. // roundtrip check.
  1170. CHECK(result.size() == 1787); // 1 type, 1 size, 255*7 content
  1171. CHECK(result[0] == 0xb8); // map 8 bit
  1172. CHECK(result[1] == 0xff); // size byte (0xff)
  1173. // roundtrip
  1174. CHECK(json::from_cbor(result) == j);
  1175. CHECK(json::from_cbor(result, true, false) == j);
  1176. }
  1177. SECTION("object with uint16_t elements")
  1178. {
  1179. json j;
  1180. for (auto i = 0; i < 256; ++i)
  1181. {
  1182. // format i to a fixed width of 5
  1183. // each entry will need 7 bytes: 6 for string, 1 for null
  1184. std::stringstream ss;
  1185. ss << std::setw(5) << std::setfill('0') << i;
  1186. j.emplace(ss.str(), nullptr);
  1187. }
  1188. const auto result = json::to_cbor(j);
  1189. // Checking against an expected vector byte by byte is
  1190. // difficult, because no assumption on the order of key/value
  1191. // pairs are made. We therefore only check the prefix (type and
  1192. // size and the overall size. The rest is then handled in the
  1193. // roundtrip check.
  1194. CHECK(result.size() == 1795); // 1 type, 2 size, 256*7 content
  1195. CHECK(result[0] == 0xb9); // map 16 bit
  1196. CHECK(result[1] == 0x01); // byte 0 of size (0x0100)
  1197. CHECK(result[2] == 0x00); // byte 1 of size (0x0100)
  1198. // roundtrip
  1199. CHECK(json::from_cbor(result) == j);
  1200. CHECK(json::from_cbor(result, true, false) == j);
  1201. }
  1202. SECTION("object with uint32_t elements")
  1203. {
  1204. json j;
  1205. for (auto i = 0; i < 65536; ++i)
  1206. {
  1207. // format i to a fixed width of 5
  1208. // each entry will need 7 bytes: 6 for string, 1 for null
  1209. std::stringstream ss;
  1210. ss << std::setw(5) << std::setfill('0') << i;
  1211. j.emplace(ss.str(), nullptr);
  1212. }
  1213. const auto result = json::to_cbor(j);
  1214. // Checking against an expected vector byte by byte is
  1215. // difficult, because no assumption on the order of key/value
  1216. // pairs are made. We therefore only check the prefix (type and
  1217. // size and the overall size. The rest is then handled in the
  1218. // roundtrip check.
  1219. CHECK(result.size() == 458757); // 1 type, 4 size, 65536*7 content
  1220. CHECK(result[0] == 0xba); // map 32 bit
  1221. CHECK(result[1] == 0x00); // byte 0 of size (0x00010000)
  1222. CHECK(result[2] == 0x01); // byte 1 of size (0x00010000)
  1223. CHECK(result[3] == 0x00); // byte 2 of size (0x00010000)
  1224. CHECK(result[4] == 0x00); // byte 3 of size (0x00010000)
  1225. // roundtrip
  1226. CHECK(json::from_cbor(result) == j);
  1227. CHECK(json::from_cbor(result, true, false) == j);
  1228. }
  1229. }
  1230. SECTION("binary")
  1231. {
  1232. SECTION("N = 0..23")
  1233. {
  1234. for (size_t N = 0; N <= 0x17; ++N)
  1235. {
  1236. CAPTURE(N)
  1237. // create JSON value with byte array containing of N * 'x'
  1238. const auto s = std::vector<uint8_t>(N, 'x');
  1239. json j = json::binary(s);
  1240. // create expected byte vector
  1241. std::vector<uint8_t> expected;
  1242. expected.push_back(static_cast<uint8_t>(0x40 + N));
  1243. for (size_t i = 0; i < N; ++i)
  1244. {
  1245. expected.push_back(0x78);
  1246. }
  1247. // compare result + size
  1248. const auto result = json::to_cbor(j);
  1249. CHECK(result == expected);
  1250. CHECK(result.size() == N + 1);
  1251. // check that no null byte is appended
  1252. if (N > 0)
  1253. {
  1254. CHECK(result.back() != '\x00');
  1255. }
  1256. // roundtrip
  1257. CHECK(json::from_cbor(result) == j);
  1258. CHECK(json::from_cbor(result, true, false) == j);
  1259. }
  1260. }
  1261. SECTION("N = 24..255")
  1262. {
  1263. for (size_t N = 24; N <= 255; ++N)
  1264. {
  1265. CAPTURE(N)
  1266. // create JSON value with string containing of N * 'x'
  1267. const auto s = std::vector<uint8_t>(N, 'x');
  1268. json j = json::binary(s);
  1269. // create expected byte vector
  1270. std::vector<uint8_t> expected;
  1271. expected.push_back(0x58);
  1272. expected.push_back(static_cast<uint8_t>(N));
  1273. for (size_t i = 0; i < N; ++i)
  1274. {
  1275. expected.push_back('x');
  1276. }
  1277. // compare result + size
  1278. const auto result = json::to_cbor(j);
  1279. CHECK(result == expected);
  1280. CHECK(result.size() == N + 2);
  1281. // check that no null byte is appended
  1282. CHECK(result.back() != '\x00');
  1283. // roundtrip
  1284. CHECK(json::from_cbor(result) == j);
  1285. CHECK(json::from_cbor(result, true, false) == j);
  1286. }
  1287. }
  1288. SECTION("N = 256..65535")
  1289. {
  1290. for (size_t N :
  1291. {
  1292. 256u, 999u, 1025u, 3333u, 2048u, 65535u
  1293. })
  1294. {
  1295. CAPTURE(N)
  1296. // create JSON value with string containing of N * 'x'
  1297. const auto s = std::vector<uint8_t>(N, 'x');
  1298. json j = json::binary(s);
  1299. // create expected byte vector (hack: create string first)
  1300. std::vector<uint8_t> expected(N, 'x');
  1301. // reverse order of commands, because we insert at begin()
  1302. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1303. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1304. expected.insert(expected.begin(), 0x59);
  1305. // compare result + size
  1306. const auto result = json::to_cbor(j);
  1307. CHECK(result == expected);
  1308. CHECK(result.size() == N + 3);
  1309. // check that no null byte is appended
  1310. CHECK(result.back() != '\x00');
  1311. // roundtrip
  1312. CHECK(json::from_cbor(result) == j);
  1313. CHECK(json::from_cbor(result, true, false) == j);
  1314. }
  1315. }
  1316. SECTION("N = 65536..4294967295")
  1317. {
  1318. for (size_t N :
  1319. {
  1320. 65536u, 77777u, 1048576u
  1321. })
  1322. {
  1323. CAPTURE(N)
  1324. // create JSON value with string containing of N * 'x'
  1325. const auto s = std::vector<uint8_t>(N, 'x');
  1326. json j = json::binary(s);
  1327. // create expected byte vector (hack: create string first)
  1328. std::vector<uint8_t> expected(N, 'x');
  1329. // reverse order of commands, because we insert at begin()
  1330. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1331. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1332. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
  1333. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
  1334. expected.insert(expected.begin(), 0x5a);
  1335. // compare result + size
  1336. const auto result = json::to_cbor(j);
  1337. CHECK(result == expected);
  1338. CHECK(result.size() == N + 5);
  1339. // check that no null byte is appended
  1340. CHECK(result.back() != '\x00');
  1341. // roundtrip
  1342. CHECK(json::from_cbor(result) == j);
  1343. CHECK(json::from_cbor(result, true, false) == j);
  1344. }
  1345. }
  1346. SECTION("indefinite size")
  1347. {
  1348. std::vector<std::uint8_t> input = {0x5F, 0x44, 0xaa, 0xbb, 0xcc, 0xdd, 0x43, 0xee, 0xff, 0x99, 0xFF};
  1349. auto j = json::from_cbor(input);
  1350. CHECK(j.is_binary());
  1351. auto k = json::binary({0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x99});
  1352. CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict))
  1353. CHECK(j == k);
  1354. }
  1355. SECTION("binary in array")
  1356. {
  1357. // array with three empty byte strings
  1358. std::vector<std::uint8_t> input = {0x83, 0x40, 0x40, 0x40};
  1359. CHECK_NOTHROW(json::from_cbor(input));
  1360. }
  1361. SECTION("binary in object")
  1362. {
  1363. // object mapping "foo" to empty byte string
  1364. std::vector<std::uint8_t> input = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x40};
  1365. CHECK_NOTHROW(json::from_cbor(input));
  1366. }
  1367. SECTION("SAX callback with binary")
  1368. {
  1369. // object mapping "foo" to byte string
  1370. std::vector<std::uint8_t> input = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x41, 0x00};
  1371. // callback to set binary_seen to true if a binary value was seen
  1372. bool binary_seen = false;
  1373. auto callback = [&binary_seen](int /*depth*/, json::parse_event_t /*event*/, json & parsed)
  1374. {
  1375. if (parsed.is_binary())
  1376. {
  1377. binary_seen = true;
  1378. }
  1379. return true;
  1380. };
  1381. json j;
  1382. auto cbp = nlohmann::detail::json_sax_dom_callback_parser<json>(j, callback, true);
  1383. CHECK(json::sax_parse(input, &cbp, json::input_format_t::cbor));
  1384. CHECK(j.at("foo").is_binary());
  1385. CHECK(binary_seen);
  1386. }
  1387. }
  1388. }
  1389. SECTION("additional deserialization")
  1390. {
  1391. SECTION("0x5b (byte array)")
  1392. {
  1393. std::vector<uint8_t> given = {0x5b, 0x00, 0x00, 0x00, 0x00,
  1394. 0x00, 0x00, 0x00, 0x01, 0x61
  1395. };
  1396. json j = json::from_cbor(given);
  1397. CHECK(j == json::binary(std::vector<uint8_t> {'a'}));
  1398. }
  1399. SECTION("0x7b (string)")
  1400. {
  1401. std::vector<uint8_t> given = {0x7b, 0x00, 0x00, 0x00, 0x00,
  1402. 0x00, 0x00, 0x00, 0x01, 0x61
  1403. };
  1404. json j = json::from_cbor(given);
  1405. CHECK(j == "a");
  1406. }
  1407. SECTION("0x9b (array)")
  1408. {
  1409. std::vector<uint8_t> given = {0x9b, 0x00, 0x00, 0x00, 0x00,
  1410. 0x00, 0x00, 0x00, 0x01, 0xf4
  1411. };
  1412. json j = json::from_cbor(given);
  1413. CHECK(j == json::parse("[false]"));
  1414. }
  1415. SECTION("0xbb (map)")
  1416. {
  1417. std::vector<uint8_t> given = {0xbb, 0x00, 0x00, 0x00, 0x00,
  1418. 0x00, 0x00, 0x00, 0x01, 0x60, 0xf4
  1419. };
  1420. json j = json::from_cbor(given);
  1421. CHECK(j == json::parse("{\"\": false}"));
  1422. }
  1423. }
  1424. SECTION("errors")
  1425. {
  1426. SECTION("empty byte vector")
  1427. {
  1428. json _;
  1429. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>()), json::parse_error&);
  1430. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>()),
  1431. "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input");
  1432. CHECK(json::from_cbor(std::vector<uint8_t>(), true, false).is_discarded());
  1433. }
  1434. SECTION("too short byte vector")
  1435. {
  1436. json _;
  1437. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x18})), json::parse_error&);
  1438. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x19})), json::parse_error&);
  1439. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x19, 0x00})), json::parse_error&);
  1440. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a})), json::parse_error&);
  1441. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00})), json::parse_error&);
  1442. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00})), json::parse_error&);
  1443. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00})), json::parse_error&);
  1444. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b})), json::parse_error&);
  1445. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00})), json::parse_error&);
  1446. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00})), json::parse_error&);
  1447. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00})), json::parse_error&);
  1448. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
  1449. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
  1450. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
  1451. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
  1452. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x62})), json::parse_error&);
  1453. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x62, 0x60})), json::parse_error&);
  1454. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F})), json::parse_error&);
  1455. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x60})), json::parse_error&);
  1456. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x82, 0x01})), json::parse_error&);
  1457. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x9F, 0x01})), json::parse_error&);
  1458. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})), json::parse_error&);
  1459. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0X61})), json::parse_error&);
  1460. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0X61})), json::parse_error&);
  1461. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F})), json::parse_error&);
  1462. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x00})), json::parse_error&);
  1463. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x41})), json::parse_error&);
  1464. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x18})),
  1465. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
  1466. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x19})),
  1467. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
  1468. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x19, 0x00})),
  1469. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
  1470. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a})),
  1471. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
  1472. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00})),
  1473. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
  1474. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00})),
  1475. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
  1476. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00})),
  1477. "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
  1478. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b})),
  1479. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
  1480. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00})),
  1481. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input");
  1482. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00})),
  1483. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input");
  1484. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00})),
  1485. "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR number: unexpected end of input");
  1486. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00})),
  1487. "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR number: unexpected end of input");
  1488. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1489. "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing CBOR number: unexpected end of input");
  1490. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1491. "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing CBOR number: unexpected end of input");
  1492. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1493. "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing CBOR number: unexpected end of input");
  1494. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x62})),
  1495. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
  1496. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x62, 0x60})),
  1497. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
  1498. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x7F})),
  1499. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input");
  1500. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x60})),
  1501. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR string: unexpected end of input");
  1502. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x82, 0x01})),
  1503. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
  1504. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x9F, 0x01})),
  1505. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input");
  1506. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})),
  1507. "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input");
  1508. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61})),
  1509. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
  1510. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61})),
  1511. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
  1512. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x5F})),
  1513. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR binary: unexpected end of input");
  1514. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x00})),
  1515. "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR binary: expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x00");
  1516. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x41})),
  1517. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR binary: unexpected end of input");
  1518. CHECK(json::from_cbor(std::vector<uint8_t>({0x18}), true, false).is_discarded());
  1519. CHECK(json::from_cbor(std::vector<uint8_t>({0x19}), true, false).is_discarded());
  1520. CHECK(json::from_cbor(std::vector<uint8_t>({0x19, 0x00}), true, false).is_discarded());
  1521. CHECK(json::from_cbor(std::vector<uint8_t>({0x1a}), true, false).is_discarded());
  1522. CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00}), true, false).is_discarded());
  1523. CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00}), true, false).is_discarded());
  1524. CHECK(json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1525. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b}), true, false).is_discarded());
  1526. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00}), true, false).is_discarded());
  1527. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00}), true, false).is_discarded());
  1528. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1529. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1530. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1531. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1532. CHECK(json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1533. CHECK(json::from_cbor(std::vector<uint8_t>({0x62}), true, false).is_discarded());
  1534. CHECK(json::from_cbor(std::vector<uint8_t>({0x62, 0x60}), true, false).is_discarded());
  1535. CHECK(json::from_cbor(std::vector<uint8_t>({0x7F}), true, false).is_discarded());
  1536. CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x60}), true, false).is_discarded());
  1537. CHECK(json::from_cbor(std::vector<uint8_t>({0x82, 0x01}), true, false).is_discarded());
  1538. CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01}), true, false).is_discarded());
  1539. CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5}), true, false).is_discarded());
  1540. CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61}), true, false).is_discarded());
  1541. CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61}), true, false).is_discarded());
  1542. CHECK(json::from_cbor(std::vector<uint8_t>({0x5F}), true, false).is_discarded());
  1543. CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x00}), true, false).is_discarded());
  1544. CHECK(json::from_cbor(std::vector<uint8_t>({0x41}), true, false).is_discarded());
  1545. }
  1546. SECTION("unsupported bytes")
  1547. {
  1548. SECTION("concrete examples")
  1549. {
  1550. json _;
  1551. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x1c})), json::parse_error&);
  1552. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x1c})),
  1553. "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C");
  1554. CHECK(json::from_cbor(std::vector<uint8_t>({0x1c}), true, false).is_discarded());
  1555. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf8})), json::parse_error&);
  1556. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf8})),
  1557. "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0xF8");
  1558. CHECK(json::from_cbor(std::vector<uint8_t>({0xf8}), true, false).is_discarded());
  1559. }
  1560. SECTION("all unsupported bytes")
  1561. {
  1562. for (auto byte :
  1563. {
  1564. // ?
  1565. 0x1c, 0x1d, 0x1e, 0x1f,
  1566. // ?
  1567. 0x3c, 0x3d, 0x3e, 0x3f,
  1568. // ?
  1569. 0x5c, 0x5d, 0x5e,
  1570. // ?
  1571. 0x7c, 0x7d, 0x7e,
  1572. // ?
  1573. 0x9c, 0x9d, 0x9e,
  1574. // ?
  1575. 0xbc, 0xbd, 0xbe,
  1576. // date/time
  1577. 0xc0, 0xc1,
  1578. // bignum
  1579. 0xc2, 0xc3,
  1580. // fraction
  1581. 0xc4,
  1582. // bigfloat
  1583. 0xc5,
  1584. // tagged item
  1585. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4,
  1586. // expected conversion
  1587. 0xd5, 0xd6, 0xd7,
  1588. // more tagged items
  1589. 0xd8, 0xd9, 0xda, 0xdb,
  1590. // ?
  1591. 0xdc, 0xdd, 0xde, 0xdf,
  1592. // (simple value)
  1593. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
  1594. // undefined
  1595. 0xf7,
  1596. // simple value
  1597. 0xf8
  1598. })
  1599. {
  1600. json _;
  1601. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error&);
  1602. CHECK(json::from_cbor(std::vector<uint8_t>({static_cast<uint8_t>(byte)}), true, false).is_discarded());
  1603. }
  1604. }
  1605. }
  1606. SECTION("invalid string in map")
  1607. {
  1608. json _;
  1609. CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})), json::parse_error&);
  1610. CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})),
  1611. "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF");
  1612. CHECK(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01}), true, false).is_discarded());
  1613. }
  1614. SECTION("strict mode")
  1615. {
  1616. std::vector<uint8_t> vec = {0xf6, 0xf6};
  1617. SECTION("non-strict mode")
  1618. {
  1619. const auto result = json::from_cbor(vec, false);
  1620. CHECK(result == json());
  1621. CHECK(not json::from_cbor(vec, false, false).is_discarded());
  1622. }
  1623. SECTION("strict mode")
  1624. {
  1625. json _;
  1626. CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
  1627. CHECK_THROWS_WITH(_ = json::from_cbor(vec),
  1628. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: expected end of input; last byte: 0xF6");
  1629. CHECK(json::from_cbor(vec, true, false).is_discarded());
  1630. }
  1631. }
  1632. }
  1633. SECTION("SAX aborts")
  1634. {
  1635. SECTION("start_array(len)")
  1636. {
  1637. std::vector<uint8_t> v = {0x83, 0x01, 0x02, 0x03};
  1638. SaxCountdown scp(0);
  1639. CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
  1640. }
  1641. SECTION("start_object(len)")
  1642. {
  1643. std::vector<uint8_t> v = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0xF4};
  1644. SaxCountdown scp(0);
  1645. CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
  1646. }
  1647. SECTION("key()")
  1648. {
  1649. std::vector<uint8_t> v = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0xF4};
  1650. SaxCountdown scp(1);
  1651. CHECK(not json::sax_parse(v, &scp, json::input_format_t::cbor));
  1652. }
  1653. }
  1654. }
  1655. // use this testcase outside [hide] to run it with Valgrind
  1656. TEST_CASE("single CBOR roundtrip")
  1657. {
  1658. SECTION("sample.json")
  1659. {
  1660. std::string filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
  1661. // parse JSON file
  1662. std::ifstream f_json(filename);
  1663. json j1 = json::parse(f_json);
  1664. // parse CBOR file
  1665. std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
  1666. std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_cbor)),
  1667. std::istreambuf_iterator<char>());
  1668. json j2;
  1669. CHECK_NOTHROW(j2 = json::from_cbor(packed));
  1670. // compare parsed JSON values
  1671. CHECK(j1 == j2);
  1672. SECTION("roundtrips")
  1673. {
  1674. SECTION("std::ostringstream")
  1675. {
  1676. std::basic_ostringstream<std::uint8_t> ss;
  1677. json::to_cbor(j1, ss);
  1678. json j3 = json::from_cbor(ss.str());
  1679. CHECK(j1 == j3);
  1680. }
  1681. SECTION("std::string")
  1682. {
  1683. std::string s;
  1684. json::to_cbor(j1, s);
  1685. json j3 = json::from_cbor(s);
  1686. CHECK(j1 == j3);
  1687. }
  1688. }
  1689. // check with different start index
  1690. packed.insert(packed.begin(), 5, 0xff);
  1691. CHECK(j1 == json::from_cbor(packed.begin() + 5, packed.end()));
  1692. }
  1693. }
  1694. #if not defined(JSON_NOEXCEPTION)
  1695. TEST_CASE("CBOR regressions")
  1696. {
  1697. SECTION("fuzz test results")
  1698. {
  1699. /*
  1700. The following test cases were found during a two-day session with
  1701. AFL-Fuzz. As a result, empty byte vectors and excessive lengths are
  1702. detected.
  1703. */
  1704. for (std::string filename :
  1705. {
  1706. TEST_DATA_DIRECTORY "/cbor_regression/test01",
  1707. TEST_DATA_DIRECTORY "/cbor_regression/test02",
  1708. TEST_DATA_DIRECTORY "/cbor_regression/test03",
  1709. TEST_DATA_DIRECTORY "/cbor_regression/test04",
  1710. TEST_DATA_DIRECTORY "/cbor_regression/test05",
  1711. TEST_DATA_DIRECTORY "/cbor_regression/test06",
  1712. TEST_DATA_DIRECTORY "/cbor_regression/test07",
  1713. TEST_DATA_DIRECTORY "/cbor_regression/test08",
  1714. TEST_DATA_DIRECTORY "/cbor_regression/test09",
  1715. TEST_DATA_DIRECTORY "/cbor_regression/test10",
  1716. TEST_DATA_DIRECTORY "/cbor_regression/test11",
  1717. TEST_DATA_DIRECTORY "/cbor_regression/test12",
  1718. TEST_DATA_DIRECTORY "/cbor_regression/test13",
  1719. TEST_DATA_DIRECTORY "/cbor_regression/test14",
  1720. TEST_DATA_DIRECTORY "/cbor_regression/test15",
  1721. TEST_DATA_DIRECTORY "/cbor_regression/test16",
  1722. TEST_DATA_DIRECTORY "/cbor_regression/test17",
  1723. TEST_DATA_DIRECTORY "/cbor_regression/test18",
  1724. TEST_DATA_DIRECTORY "/cbor_regression/test19",
  1725. TEST_DATA_DIRECTORY "/cbor_regression/test20",
  1726. TEST_DATA_DIRECTORY "/cbor_regression/test21"
  1727. })
  1728. {
  1729. CAPTURE(filename)
  1730. try
  1731. {
  1732. // parse CBOR file
  1733. std::ifstream f_cbor(filename, std::ios::binary);
  1734. std::vector<uint8_t> vec1(
  1735. (std::istreambuf_iterator<char>(f_cbor)),
  1736. std::istreambuf_iterator<char>());
  1737. json j1 = json::from_cbor(vec1);
  1738. try
  1739. {
  1740. // step 2: round trip
  1741. std::vector<uint8_t> vec2 = json::to_cbor(j1);
  1742. // parse serialization
  1743. json j2 = json::from_cbor(vec2);
  1744. // deserializations must match
  1745. CHECK(j1 == j2);
  1746. }
  1747. catch (const json::parse_error&)
  1748. {
  1749. // parsing a CBOR serialization must not fail
  1750. CHECK(false);
  1751. }
  1752. }
  1753. catch (const json::parse_error&)
  1754. {
  1755. // parse errors are ok, because input may be random bytes
  1756. }
  1757. }
  1758. }
  1759. }
  1760. #endif
  1761. TEST_CASE("CBOR roundtrips" * doctest::skip())
  1762. {
  1763. SECTION("input from flynn")
  1764. {
  1765. // most of these are excluded due to differences in key order (not a real problem)
  1766. auto exclude_packed = std::set<std::string>
  1767. {
  1768. TEST_DATA_DIRECTORY "/json.org/1.json",
  1769. TEST_DATA_DIRECTORY "/json.org/2.json",
  1770. TEST_DATA_DIRECTORY "/json.org/3.json",
  1771. TEST_DATA_DIRECTORY "/json.org/4.json",
  1772. TEST_DATA_DIRECTORY "/json.org/5.json",
  1773. TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor
  1774. TEST_DATA_DIRECTORY "/json_tests/pass1.json",
  1775. TEST_DATA_DIRECTORY "/regression/working_file.json",
  1776. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json",
  1777. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
  1778. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json",
  1779. };
  1780. for (std::string filename :
  1781. {
  1782. TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json",
  1783. TEST_DATA_DIRECTORY "/json.org/1.json",
  1784. TEST_DATA_DIRECTORY "/json.org/2.json",
  1785. TEST_DATA_DIRECTORY "/json.org/3.json",
  1786. TEST_DATA_DIRECTORY "/json.org/4.json",
  1787. TEST_DATA_DIRECTORY "/json.org/5.json",
  1788. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json",
  1789. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json",
  1790. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip03.json",
  1791. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip04.json",
  1792. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip05.json",
  1793. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip06.json",
  1794. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip07.json",
  1795. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip08.json",
  1796. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip09.json",
  1797. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip10.json",
  1798. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip11.json",
  1799. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip12.json",
  1800. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip13.json",
  1801. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip14.json",
  1802. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip15.json",
  1803. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip16.json",
  1804. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip17.json",
  1805. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip18.json",
  1806. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip19.json",
  1807. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip20.json",
  1808. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip21.json",
  1809. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip22.json",
  1810. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip23.json",
  1811. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip24.json",
  1812. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip25.json",
  1813. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip26.json",
  1814. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip27.json",
  1815. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip28.json",
  1816. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip29.json",
  1817. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip30.json",
  1818. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip31.json",
  1819. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip32.json",
  1820. TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor
  1821. TEST_DATA_DIRECTORY "/json_tests/pass1.json",
  1822. TEST_DATA_DIRECTORY "/json_tests/pass2.json",
  1823. TEST_DATA_DIRECTORY "/json_tests/pass3.json",
  1824. TEST_DATA_DIRECTORY "/regression/floats.json",
  1825. TEST_DATA_DIRECTORY "/regression/signed_ints.json",
  1826. TEST_DATA_DIRECTORY "/regression/unsigned_ints.json",
  1827. TEST_DATA_DIRECTORY "/regression/working_file.json",
  1828. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
  1829. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty-string.json",
  1830. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty.json",
  1831. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
  1832. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_false.json",
  1833. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
  1834. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_null.json",
  1835. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
  1836. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
  1837. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
  1838. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
  1839. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number.json",
  1840. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e+1.json",
  1841. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e1.json",
  1842. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_after_space.json",
  1843. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
  1844. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
  1845. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
  1846. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
  1847. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
  1848. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_int.json",
  1849. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_one.json",
  1850. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
  1851. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
  1852. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
  1853. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
  1854. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
  1855. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
  1856. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
  1857. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
  1858. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
  1859. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
  1860. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
  1861. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_int.json",
  1862. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_real.json",
  1863. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
  1864. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
  1865. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
  1866. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json",
  1867. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json",
  1868. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
  1869. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
  1870. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty.json",
  1871. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty_key.json",
  1872. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
  1873. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
  1874. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json",
  1875. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json",
  1876. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
  1877. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
  1878. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
  1879. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
  1880. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
  1881. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
  1882. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
  1883. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
  1884. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
  1885. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_comments.json",
  1886. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
  1887. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
  1888. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
  1889. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
  1890. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array.json",
  1891. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
  1892. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
  1893. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
  1894. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
  1895. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
  1896. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
  1897. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_null_escape.json",
  1898. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
  1899. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_pi.json",
  1900. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
  1901. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_space.json",
  1902. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
  1903. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
  1904. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
  1905. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
  1906. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_uEscape.json",
  1907. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
  1908. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode.json",
  1909. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
  1910. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
  1911. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
  1912. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
  1913. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
  1914. // TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf16.json",
  1915. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf8.json",
  1916. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
  1917. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
  1918. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
  1919. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
  1920. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
  1921. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
  1922. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
  1923. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
  1924. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
  1925. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
  1926. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
  1927. })
  1928. {
  1929. CAPTURE(filename)
  1930. {
  1931. INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
  1932. // parse JSON file
  1933. std::ifstream f_json(filename);
  1934. json j1 = json::parse(f_json);
  1935. // parse CBOR file
  1936. std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
  1937. std::vector<uint8_t> packed(
  1938. (std::istreambuf_iterator<char>(f_cbor)),
  1939. std::istreambuf_iterator<char>());
  1940. json j2;
  1941. CHECK_NOTHROW(j2 = json::from_cbor(packed));
  1942. // compare parsed JSON values
  1943. CHECK(j1 == j2);
  1944. }
  1945. {
  1946. INFO_WITH_TEMP(filename + ": std::ifstream");
  1947. // parse JSON file
  1948. std::ifstream f_json(filename);
  1949. json j1 = json::parse(f_json);
  1950. // parse CBOR file
  1951. std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
  1952. json j2;
  1953. CHECK_NOTHROW(j2 = json::from_cbor(f_cbor));
  1954. // compare parsed JSON values
  1955. CHECK(j1 == j2);
  1956. }
  1957. {
  1958. INFO_WITH_TEMP(filename + ": uint8_t* and size");
  1959. // parse JSON file
  1960. std::ifstream f_json(filename);
  1961. json j1 = json::parse(f_json);
  1962. // parse CBOR file
  1963. std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
  1964. std::vector<uint8_t> packed(
  1965. (std::istreambuf_iterator<char>(f_cbor)),
  1966. std::istreambuf_iterator<char>());
  1967. json j2;
  1968. CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));
  1969. // compare parsed JSON values
  1970. CHECK(j1 == j2);
  1971. }
  1972. {
  1973. INFO_WITH_TEMP(filename + ": output to output adapters");
  1974. // parse JSON file
  1975. std::ifstream f_json(filename);
  1976. json j1 = json::parse(f_json);
  1977. // parse CBOR file
  1978. std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
  1979. std::vector<uint8_t> packed(
  1980. (std::istreambuf_iterator<char>(f_cbor)),
  1981. std::istreambuf_iterator<char>());
  1982. if (!exclude_packed.count(filename))
  1983. {
  1984. {
  1985. INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
  1986. std::vector<uint8_t> vec;
  1987. json::to_cbor(j1, vec);
  1988. CHECK(vec == packed);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. }
  1995. #if not defined(JSON_NOEXCEPTION)
  1996. TEST_CASE("all CBOR first bytes")
  1997. {
  1998. // these bytes will fail immediately with exception parse_error.112
  1999. std::set<uint8_t> unsupported =
  2000. {
  2001. //// types not supported by this library
  2002. // date/time
  2003. 0xc0, 0xc1,
  2004. // bignum
  2005. 0xc2, 0xc3,
  2006. // decimal fracion
  2007. 0xc4,
  2008. // bigfloat
  2009. 0xc5,
  2010. // tagged item
  2011. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
  2012. 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd8,
  2013. 0xd9, 0xda, 0xdb,
  2014. // expected conversion
  2015. 0xd5, 0xd6, 0xd7,
  2016. // simple value
  2017. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  2018. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xef, 0xf0,
  2019. 0xf1, 0xf2, 0xf3,
  2020. 0xf8,
  2021. // undefined
  2022. 0xf7,
  2023. //// bytes not specified by CBOR
  2024. 0x1c, 0x1d, 0x1e, 0x1f,
  2025. 0x3c, 0x3d, 0x3e, 0x3f,
  2026. 0x5c, 0x5d, 0x5e,
  2027. 0x7c, 0x7d, 0x7e,
  2028. 0x9c, 0x9d, 0x9e,
  2029. 0xbc, 0xbd, 0xbe,
  2030. 0xdc, 0xdd, 0xde, 0xdf,
  2031. 0xee,
  2032. 0xfc, 0xfe, 0xfd,
  2033. /// break cannot be the first byte
  2034. 0xff
  2035. };
  2036. for (auto i = 0; i < 256; ++i)
  2037. {
  2038. const auto byte = static_cast<uint8_t>(i);
  2039. try
  2040. {
  2041. auto res = json::from_cbor(std::vector<uint8_t>(1, byte));
  2042. }
  2043. catch (const json::parse_error& e)
  2044. {
  2045. // check that parse_error.112 is only thrown if the
  2046. // first byte is in the unsupported set
  2047. INFO_WITH_TEMP(e.what());
  2048. if (std::find(unsupported.begin(), unsupported.end(), byte) != unsupported.end())
  2049. {
  2050. CHECK(e.id == 112);
  2051. }
  2052. else
  2053. {
  2054. CHECK(e.id != 112);
  2055. }
  2056. }
  2057. }
  2058. }
  2059. #endif
  2060. TEST_CASE("examples from RFC 7049 Appendix A")
  2061. {
  2062. SECTION("numbers")
  2063. {
  2064. CHECK(json::to_cbor(json::parse("0")) == std::vector<uint8_t>({0x00}));
  2065. CHECK(json::parse("0") == json::from_cbor(std::vector<uint8_t>({0x00})));
  2066. CHECK(json::to_cbor(json::parse("1")) == std::vector<uint8_t>({0x01}));
  2067. CHECK(json::parse("1") == json::from_cbor(std::vector<uint8_t>({0x01})));
  2068. CHECK(json::to_cbor(json::parse("10")) == std::vector<uint8_t>({0x0a}));
  2069. CHECK(json::parse("10") == json::from_cbor(std::vector<uint8_t>({0x0a})));
  2070. CHECK(json::to_cbor(json::parse("23")) == std::vector<uint8_t>({0x17}));
  2071. CHECK(json::parse("23") == json::from_cbor(std::vector<uint8_t>({0x17})));
  2072. CHECK(json::to_cbor(json::parse("24")) == std::vector<uint8_t>({0x18, 0x18}));
  2073. CHECK(json::parse("24") == json::from_cbor(std::vector<uint8_t>({0x18, 0x18})));
  2074. CHECK(json::to_cbor(json::parse("25")) == std::vector<uint8_t>({0x18, 0x19}));
  2075. CHECK(json::parse("25") == json::from_cbor(std::vector<uint8_t>({0x18, 0x19})));
  2076. CHECK(json::to_cbor(json::parse("100")) == std::vector<uint8_t>({0x18, 0x64}));
  2077. CHECK(json::parse("100") == json::from_cbor(std::vector<uint8_t>({0x18, 0x64})));
  2078. CHECK(json::to_cbor(json::parse("1000")) == std::vector<uint8_t>({0x19, 0x03, 0xe8}));
  2079. CHECK(json::parse("1000") == json::from_cbor(std::vector<uint8_t>({0x19, 0x03, 0xe8})));
  2080. CHECK(json::to_cbor(json::parse("1000000")) == std::vector<uint8_t>({0x1a, 0x00, 0x0f, 0x42, 0x40}));
  2081. CHECK(json::parse("1000000") == json::from_cbor(std::vector<uint8_t>({0x1a, 0x00, 0x0f, 0x42, 0x40})));
  2082. CHECK(json::to_cbor(json::parse("1000000000000")) == std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00}));
  2083. CHECK(json::parse("1000000000000") == json::from_cbor(std::vector<uint8_t>({0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00})));
  2084. CHECK(json::to_cbor(json::parse("18446744073709551615")) == std::vector<uint8_t>({0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}));
  2085. CHECK(json::parse("18446744073709551615") == json::from_cbor(std::vector<uint8_t>({0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})));
  2086. // positive bignum is not supported
  2087. //CHECK(json::to_cbor(json::parse("18446744073709551616")) == std::vector<uint8_t>({0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
  2088. //CHECK(json::parse("18446744073709551616") == json::from_cbor(std::vector<uint8_t>({0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})));
  2089. //CHECK(json::to_cbor(json::parse("-18446744073709551616")) == std::vector<uint8_t>({0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}));
  2090. //CHECK(json::parse("-18446744073709551616") == json::from_cbor(std::vector<uint8_t>({0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})));
  2091. // negative bignum is not supported
  2092. //CHECK(json::to_cbor(json::parse("-18446744073709551617")) == std::vector<uint8_t>({0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
  2093. //CHECK(json::parse("-18446744073709551617") == json::from_cbor(std::vector<uint8_t>({0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})));
  2094. CHECK(json::to_cbor(json::parse("-1")) == std::vector<uint8_t>({0x20}));
  2095. CHECK(json::parse("-1") == json::from_cbor(std::vector<uint8_t>({0x20})));
  2096. CHECK(json::to_cbor(json::parse("-10")) == std::vector<uint8_t>({0x29}));
  2097. CHECK(json::parse("-10") == json::from_cbor(std::vector<uint8_t>({0x29})));
  2098. CHECK(json::to_cbor(json::parse("-100")) == std::vector<uint8_t>({0x38, 0x63}));
  2099. CHECK(json::parse("-100") == json::from_cbor(std::vector<uint8_t>({0x38, 0x63})));
  2100. CHECK(json::to_cbor(json::parse("-1000")) == std::vector<uint8_t>({0x39, 0x03, 0xe7}));
  2101. CHECK(json::parse("-1000") == json::from_cbor(std::vector<uint8_t>({0x39, 0x03, 0xe7})));
  2102. // half-precision float
  2103. //CHECK(json::to_cbor(json::parse("0.0")) == std::vector<uint8_t>({0xf9, 0x00, 0x00}));
  2104. CHECK(json::parse("0.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x00, 0x00})));
  2105. // half-precision float
  2106. //CHECK(json::to_cbor(json::parse("-0.0")) == std::vector<uint8_t>({0xf9, 0x80, 0x00}));
  2107. CHECK(json::parse("-0.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x80, 0x00})));
  2108. // half-precision float
  2109. //CHECK(json::to_cbor(json::parse("1.0")) == std::vector<uint8_t>({0xf9, 0x3c, 0x00}));
  2110. CHECK(json::parse("1.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x3c, 0x00})));
  2111. CHECK(json::to_cbor(json::parse("1.1")) == std::vector<uint8_t>({0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a}));
  2112. CHECK(json::parse("1.1") == json::from_cbor(std::vector<uint8_t>({0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a})));
  2113. // half-precision float
  2114. //CHECK(json::to_cbor(json::parse("1.5")) == std::vector<uint8_t>({0xf9, 0x3e, 0x00}));
  2115. CHECK(json::parse("1.5") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x3e, 0x00})));
  2116. // half-precision float
  2117. //CHECK(json::to_cbor(json::parse("65504.0")) == std::vector<uint8_t>({0xf9, 0x7b, 0xff}));
  2118. CHECK(json::parse("65504.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0x7b, 0xff})));
  2119. //CHECK(json::to_cbor(json::parse("100000.0")) == std::vector<uint8_t>({0xfa, 0x47, 0xc3, 0x50, 0x00}));
  2120. CHECK(json::parse("100000.0") == json::from_cbor(std::vector<uint8_t>({0xfa, 0x47, 0xc3, 0x50, 0x00})));
  2121. //CHECK(json::to_cbor(json::parse("3.4028234663852886e+38")) == std::vector<uint8_t>({0xfa, 0x7f, 0x7f, 0xff, 0xff}));
  2122. CHECK(json::parse("3.4028234663852886e+38") == json::from_cbor(std::vector<uint8_t>({0xfa, 0x7f, 0x7f, 0xff, 0xff})));
  2123. CHECK(json::to_cbor(json::parse("1.0e+300")) == std::vector<uint8_t>({0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c}));
  2124. CHECK(json::parse("1.0e+300") == json::from_cbor(std::vector<uint8_t>({0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c})));
  2125. // half-precision float
  2126. //CHECK(json::to_cbor(json::parse("5.960464477539063e-8")) == std::vector<uint8_t>({0xf9, 0x00, 0x01}));
  2127. CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
  2128. // half-precision float
  2129. //CHECK(json::to_cbor(json::parse("0.00006103515625")) == std::vector<uint8_t>({0xf9, 0x04, 0x00}));
  2130. CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
  2131. // half-precision float
  2132. //CHECK(json::to_cbor(json::parse("-4.0")) == std::vector<uint8_t>({0xf9, 0xc4, 0x00}));
  2133. CHECK(json::parse("-4.0") == json::from_cbor(std::vector<uint8_t>({0xf9, 0xc4, 0x00})));
  2134. CHECK(json::to_cbor(json::parse("-4.1")) == std::vector<uint8_t>({0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}));
  2135. CHECK(json::parse("-4.1") == json::from_cbor(std::vector<uint8_t>({0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66})));
  2136. }
  2137. SECTION("simple values")
  2138. {
  2139. CHECK(json::to_cbor(json::parse("false")) == std::vector<uint8_t>({0xf4}));
  2140. CHECK(json::parse("false") == json::from_cbor(std::vector<uint8_t>({0xf4})));
  2141. CHECK(json::to_cbor(json::parse("true")) == std::vector<uint8_t>({0xf5}));
  2142. CHECK(json::parse("true") == json::from_cbor(std::vector<uint8_t>({0xf5})));
  2143. CHECK(json::to_cbor(json::parse("true")) == std::vector<uint8_t>({0xf5}));
  2144. CHECK(json::parse("true") == json::from_cbor(std::vector<uint8_t>({0xf5})));
  2145. }
  2146. SECTION("strings")
  2147. {
  2148. CHECK(json::to_cbor(json::parse("\"\"")) == std::vector<uint8_t>({0x60}));
  2149. CHECK(json::parse("\"\"") == json::from_cbor(std::vector<uint8_t>({0x60})));
  2150. CHECK(json::to_cbor(json::parse("\"a\"")) == std::vector<uint8_t>({0x61, 0x61}));
  2151. CHECK(json::parse("\"a\"") == json::from_cbor(std::vector<uint8_t>({0x61, 0x61})));
  2152. CHECK(json::to_cbor(json::parse("\"IETF\"")) == std::vector<uint8_t>({0x64, 0x49, 0x45, 0x54, 0x46}));
  2153. CHECK(json::parse("\"IETF\"") == json::from_cbor(std::vector<uint8_t>({0x64, 0x49, 0x45, 0x54, 0x46})));
  2154. CHECK(json::to_cbor(json::parse("\"\\u00fc\"")) == std::vector<uint8_t>({0x62, 0xc3, 0xbc}));
  2155. CHECK(json::parse("\"\\u00fc\"") == json::from_cbor(std::vector<uint8_t>({0x62, 0xc3, 0xbc})));
  2156. CHECK(json::to_cbor(json::parse("\"\\u6c34\"")) == std::vector<uint8_t>({0x63, 0xe6, 0xb0, 0xb4}));
  2157. CHECK(json::parse("\"\\u6c34\"") == json::from_cbor(std::vector<uint8_t>({0x63, 0xe6, 0xb0, 0xb4})));
  2158. CHECK(json::to_cbor(json::parse("\"\\ud800\\udd51\"")) == std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91}));
  2159. CHECK(json::parse("\"\\ud800\\udd51\"") == json::from_cbor(std::vector<uint8_t>({0x64, 0xf0, 0x90, 0x85, 0x91})));
  2160. // indefinite length strings
  2161. CHECK(json::parse("\"streaming\"") == json::from_cbor(std::vector<uint8_t>({0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x67, 0xff})));
  2162. }
  2163. SECTION("byte arrays")
  2164. {
  2165. std::ifstream f_cbor(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.cbor", std::ios::binary);
  2166. std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_cbor)),
  2167. std::istreambuf_iterator<char>());
  2168. json j;
  2169. CHECK_NOTHROW(j = json::from_cbor(packed));
  2170. std::ifstream f_bin(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.out", std::ios::binary);
  2171. std::vector<uint8_t> expected((std::istreambuf_iterator<char>(f_bin)),
  2172. std::istreambuf_iterator<char>());
  2173. CHECK(j == json::binary(expected));
  2174. }
  2175. SECTION("arrays")
  2176. {
  2177. CHECK(json::to_cbor(json::parse("[]")) == std::vector<uint8_t>({0x80}));
  2178. CHECK(json::parse("[]") == json::from_cbor(std::vector<uint8_t>({0x80})));
  2179. CHECK(json::to_cbor(json::parse("[1, 2, 3]")) == std::vector<uint8_t>({0x83, 0x01, 0x02, 0x03}));
  2180. CHECK(json::parse("[1, 2, 3]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x02, 0x03})));
  2181. CHECK(json::to_cbor(json::parse("[1, [2, 3], [4, 5]]")) == std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05}));
  2182. CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05})));
  2183. CHECK(json::to_cbor(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]")) == std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19}));
  2184. CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19})));
  2185. // indefinite length arrays
  2186. CHECK(json::parse("[]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0xff})));
  2187. CHECK(json::parse("[1, [2, 3], [4, 5]] ") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff, 0xff})));
  2188. CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05, 0xff})));
  2189. CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff})));
  2190. CHECK(json::parse("[1, [2, 3], [4, 5]]") == json::from_cbor(std::vector<uint8_t>({0x83, 0x01, 0x9f, 0x02, 0x03, 0xff, 0x82, 0x04, 0x05})));
  2191. CHECK(json::parse("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]") == json::from_cbor(std::vector<uint8_t>({0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0xff})));
  2192. }
  2193. SECTION("objects")
  2194. {
  2195. CHECK(json::to_cbor(json::parse("{}")) == std::vector<uint8_t>({0xa0}));
  2196. CHECK(json::parse("{}") == json::from_cbor(std::vector<uint8_t>({0xa0})));
  2197. CHECK(json::to_cbor(json::parse("{\"a\": 1, \"b\": [2, 3]}")) == std::vector<uint8_t>({0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03}));
  2198. CHECK(json::parse("{\"a\": 1, \"b\": [2, 3]}") == json::from_cbor(std::vector<uint8_t>({0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03})));
  2199. CHECK(json::to_cbor(json::parse("[\"a\", {\"b\": \"c\"}]")) == std::vector<uint8_t>({0x82, 0x61, 0x61, 0xa1, 0x61, 0x62, 0x61, 0x63}));
  2200. CHECK(json::parse("[\"a\", {\"b\": \"c\"}]") == json::from_cbor(std::vector<uint8_t>({0x82, 0x61, 0x61, 0xa1, 0x61, 0x62, 0x61, 0x63})));
  2201. CHECK(json::to_cbor(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}")) == std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45}));
  2202. CHECK(json::parse("{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}") == json::from_cbor(std::vector<uint8_t>({0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45})));
  2203. // indefinite length objects
  2204. CHECK(json::parse("{\"a\": 1, \"b\": [2, 3]}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x61, 0x61, 0x01, 0x61, 0x62, 0x9f, 0x02, 0x03, 0xff, 0xff})));
  2205. CHECK(json::parse("[\"a\", {\"b\": \"c\"}]") == json::from_cbor(std::vector<uint8_t>({0x82, 0x61, 0x61, 0xbf, 0x61, 0x62, 0x61, 0x63, 0xff})));
  2206. CHECK(json::parse("{\"Fun\": true, \"Amt\": -2}") == json::from_cbor(std::vector<uint8_t>({0xbf, 0x63, 0x46, 0x75, 0x6e, 0xf5, 0x63, 0x41, 0x6d, 0x74, 0x21, 0xff})));
  2207. }
  2208. }