binary_writer.hpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <algorithm> // reverse
  10. #include <array> // array
  11. #include <map> // map
  12. #include <cmath> // isnan, isinf
  13. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  14. #include <cstring> // memcpy
  15. #include <limits> // numeric_limits
  16. #include <string> // string
  17. #include <utility> // move
  18. #include <vector> // vector
  19. #include <nlohmann/detail/input/binary_reader.hpp>
  20. #include <nlohmann/detail/macro_scope.hpp>
  21. #include <nlohmann/detail/output/output_adapters.hpp>
  22. #include <nlohmann/detail/string_concat.hpp>
  23. NLOHMANN_JSON_NAMESPACE_BEGIN
  24. namespace detail
  25. {
  26. ///////////////////
  27. // binary writer //
  28. ///////////////////
  29. /*!
  30. @brief serialization to CBOR and MessagePack values
  31. */
  32. template<typename BasicJsonType, typename CharType>
  33. class binary_writer
  34. {
  35. using string_t = typename BasicJsonType::string_t;
  36. using binary_t = typename BasicJsonType::binary_t;
  37. using number_float_t = typename BasicJsonType::number_float_t;
  38. public:
  39. /*!
  40. @brief create a binary writer
  41. @param[in] adapter output adapter to write to
  42. */
  43. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  44. {
  45. JSON_ASSERT(oa);
  46. }
  47. /*!
  48. @param[in] j JSON value to serialize
  49. @pre j.type() == value_t::object
  50. */
  51. void write_bson(const BasicJsonType& j)
  52. {
  53. switch (j.type())
  54. {
  55. case value_t::object:
  56. {
  57. write_bson_object(*j.m_data.m_value.object);
  58. break;
  59. }
  60. case value_t::null:
  61. case value_t::array:
  62. case value_t::string:
  63. case value_t::boolean:
  64. case value_t::number_integer:
  65. case value_t::number_unsigned:
  66. case value_t::number_float:
  67. case value_t::binary:
  68. case value_t::discarded:
  69. default:
  70. {
  71. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  72. }
  73. }
  74. }
  75. /*!
  76. @param[in] j JSON value to serialize
  77. */
  78. void write_cbor(const BasicJsonType& j)
  79. {
  80. switch (j.type())
  81. {
  82. case value_t::null:
  83. {
  84. oa->write_character(to_char_type(0xF6));
  85. break;
  86. }
  87. case value_t::boolean:
  88. {
  89. oa->write_character(j.m_data.m_value.boolean
  90. ? to_char_type(0xF5)
  91. : to_char_type(0xF4));
  92. break;
  93. }
  94. case value_t::number_integer:
  95. {
  96. if (j.m_data.m_value.number_integer >= 0)
  97. {
  98. // CBOR does not differentiate between positive signed
  99. // integers and unsigned integers. Therefore, we used the
  100. // code from the value_t::number_unsigned case here.
  101. if (j.m_data.m_value.number_integer <= 0x17)
  102. {
  103. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  104. }
  105. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  106. {
  107. oa->write_character(to_char_type(0x18));
  108. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  109. }
  110. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  111. {
  112. oa->write_character(to_char_type(0x19));
  113. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  114. }
  115. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  116. {
  117. oa->write_character(to_char_type(0x1A));
  118. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  119. }
  120. else
  121. {
  122. oa->write_character(to_char_type(0x1B));
  123. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  124. }
  125. }
  126. else
  127. {
  128. // The conversions below encode the sign in the first
  129. // byte, and the value is converted to a positive number.
  130. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  131. if (j.m_data.m_value.number_integer >= -24)
  132. {
  133. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  134. }
  135. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  136. {
  137. oa->write_character(to_char_type(0x38));
  138. write_number(static_cast<std::uint8_t>(positive_number));
  139. }
  140. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  141. {
  142. oa->write_character(to_char_type(0x39));
  143. write_number(static_cast<std::uint16_t>(positive_number));
  144. }
  145. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  146. {
  147. oa->write_character(to_char_type(0x3A));
  148. write_number(static_cast<std::uint32_t>(positive_number));
  149. }
  150. else
  151. {
  152. oa->write_character(to_char_type(0x3B));
  153. write_number(static_cast<std::uint64_t>(positive_number));
  154. }
  155. }
  156. break;
  157. }
  158. case value_t::number_unsigned:
  159. {
  160. if (j.m_data.m_value.number_unsigned <= 0x17)
  161. {
  162. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  163. }
  164. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  165. {
  166. oa->write_character(to_char_type(0x18));
  167. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  168. }
  169. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  170. {
  171. oa->write_character(to_char_type(0x19));
  172. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  173. }
  174. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  175. {
  176. oa->write_character(to_char_type(0x1A));
  177. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  178. }
  179. else
  180. {
  181. oa->write_character(to_char_type(0x1B));
  182. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  183. }
  184. break;
  185. }
  186. case value_t::number_float:
  187. {
  188. if (std::isnan(j.m_data.m_value.number_float))
  189. {
  190. // NaN is 0xf97e00 in CBOR
  191. oa->write_character(to_char_type(0xF9));
  192. oa->write_character(to_char_type(0x7E));
  193. oa->write_character(to_char_type(0x00));
  194. }
  195. else if (std::isinf(j.m_data.m_value.number_float))
  196. {
  197. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  198. oa->write_character(to_char_type(0xf9));
  199. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  200. oa->write_character(to_char_type(0x00));
  201. }
  202. else
  203. {
  204. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  205. }
  206. break;
  207. }
  208. case value_t::string:
  209. {
  210. // step 1: write control byte and the string length
  211. const auto N = j.m_data.m_value.string->size();
  212. if (N <= 0x17)
  213. {
  214. write_number(static_cast<std::uint8_t>(0x60 + N));
  215. }
  216. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  217. {
  218. oa->write_character(to_char_type(0x78));
  219. write_number(static_cast<std::uint8_t>(N));
  220. }
  221. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  222. {
  223. oa->write_character(to_char_type(0x79));
  224. write_number(static_cast<std::uint16_t>(N));
  225. }
  226. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  227. {
  228. oa->write_character(to_char_type(0x7A));
  229. write_number(static_cast<std::uint32_t>(N));
  230. }
  231. // LCOV_EXCL_START
  232. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  233. {
  234. oa->write_character(to_char_type(0x7B));
  235. write_number(static_cast<std::uint64_t>(N));
  236. }
  237. // LCOV_EXCL_STOP
  238. // step 2: write the string
  239. oa->write_characters(
  240. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  241. j.m_data.m_value.string->size());
  242. break;
  243. }
  244. case value_t::array:
  245. {
  246. // step 1: write control byte and the array size
  247. const auto N = j.m_data.m_value.array->size();
  248. if (N <= 0x17)
  249. {
  250. write_number(static_cast<std::uint8_t>(0x80 + N));
  251. }
  252. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  253. {
  254. oa->write_character(to_char_type(0x98));
  255. write_number(static_cast<std::uint8_t>(N));
  256. }
  257. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  258. {
  259. oa->write_character(to_char_type(0x99));
  260. write_number(static_cast<std::uint16_t>(N));
  261. }
  262. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  263. {
  264. oa->write_character(to_char_type(0x9A));
  265. write_number(static_cast<std::uint32_t>(N));
  266. }
  267. // LCOV_EXCL_START
  268. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  269. {
  270. oa->write_character(to_char_type(0x9B));
  271. write_number(static_cast<std::uint64_t>(N));
  272. }
  273. // LCOV_EXCL_STOP
  274. // step 2: write each element
  275. for (const auto& el : *j.m_data.m_value.array)
  276. {
  277. write_cbor(el);
  278. }
  279. break;
  280. }
  281. case value_t::binary:
  282. {
  283. if (j.m_data.m_value.binary->has_subtype())
  284. {
  285. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  286. {
  287. write_number(static_cast<std::uint8_t>(0xd8));
  288. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  289. }
  290. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  291. {
  292. write_number(static_cast<std::uint8_t>(0xd9));
  293. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  294. }
  295. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  296. {
  297. write_number(static_cast<std::uint8_t>(0xda));
  298. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  299. }
  300. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  301. {
  302. write_number(static_cast<std::uint8_t>(0xdb));
  303. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  304. }
  305. }
  306. // step 1: write control byte and the binary array size
  307. const auto N = j.m_data.m_value.binary->size();
  308. if (N <= 0x17)
  309. {
  310. write_number(static_cast<std::uint8_t>(0x40 + N));
  311. }
  312. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  313. {
  314. oa->write_character(to_char_type(0x58));
  315. write_number(static_cast<std::uint8_t>(N));
  316. }
  317. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  318. {
  319. oa->write_character(to_char_type(0x59));
  320. write_number(static_cast<std::uint16_t>(N));
  321. }
  322. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  323. {
  324. oa->write_character(to_char_type(0x5A));
  325. write_number(static_cast<std::uint32_t>(N));
  326. }
  327. // LCOV_EXCL_START
  328. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  329. {
  330. oa->write_character(to_char_type(0x5B));
  331. write_number(static_cast<std::uint64_t>(N));
  332. }
  333. // LCOV_EXCL_STOP
  334. // step 2: write each element
  335. oa->write_characters(
  336. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  337. N);
  338. break;
  339. }
  340. case value_t::object:
  341. {
  342. // step 1: write control byte and the object size
  343. const auto N = j.m_data.m_value.object->size();
  344. if (N <= 0x17)
  345. {
  346. write_number(static_cast<std::uint8_t>(0xA0 + N));
  347. }
  348. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  349. {
  350. oa->write_character(to_char_type(0xB8));
  351. write_number(static_cast<std::uint8_t>(N));
  352. }
  353. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  354. {
  355. oa->write_character(to_char_type(0xB9));
  356. write_number(static_cast<std::uint16_t>(N));
  357. }
  358. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  359. {
  360. oa->write_character(to_char_type(0xBA));
  361. write_number(static_cast<std::uint32_t>(N));
  362. }
  363. // LCOV_EXCL_START
  364. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  365. {
  366. oa->write_character(to_char_type(0xBB));
  367. write_number(static_cast<std::uint64_t>(N));
  368. }
  369. // LCOV_EXCL_STOP
  370. // step 2: write each element
  371. for (const auto& el : *j.m_data.m_value.object)
  372. {
  373. write_cbor(el.first);
  374. write_cbor(el.second);
  375. }
  376. break;
  377. }
  378. case value_t::discarded:
  379. default:
  380. break;
  381. }
  382. }
  383. /*!
  384. @param[in] j JSON value to serialize
  385. */
  386. void write_msgpack(const BasicJsonType& j)
  387. {
  388. switch (j.type())
  389. {
  390. case value_t::null: // nil
  391. {
  392. oa->write_character(to_char_type(0xC0));
  393. break;
  394. }
  395. case value_t::boolean: // true and false
  396. {
  397. oa->write_character(j.m_data.m_value.boolean
  398. ? to_char_type(0xC3)
  399. : to_char_type(0xC2));
  400. break;
  401. }
  402. case value_t::number_integer:
  403. {
  404. if (j.m_data.m_value.number_integer >= 0)
  405. {
  406. // MessagePack does not differentiate between positive
  407. // signed integers and unsigned integers. Therefore, we used
  408. // the code from the value_t::number_unsigned case here.
  409. if (j.m_data.m_value.number_unsigned < 128)
  410. {
  411. // positive fixnum
  412. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  413. }
  414. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  415. {
  416. // uint 8
  417. oa->write_character(to_char_type(0xCC));
  418. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  419. }
  420. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  421. {
  422. // uint 16
  423. oa->write_character(to_char_type(0xCD));
  424. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  425. }
  426. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  427. {
  428. // uint 32
  429. oa->write_character(to_char_type(0xCE));
  430. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  431. }
  432. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  433. {
  434. // uint 64
  435. oa->write_character(to_char_type(0xCF));
  436. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  437. }
  438. }
  439. else
  440. {
  441. if (j.m_data.m_value.number_integer >= -32)
  442. {
  443. // negative fixnum
  444. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  445. }
  446. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  447. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  448. {
  449. // int 8
  450. oa->write_character(to_char_type(0xD0));
  451. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  452. }
  453. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  454. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  455. {
  456. // int 16
  457. oa->write_character(to_char_type(0xD1));
  458. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  459. }
  460. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  461. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  462. {
  463. // int 32
  464. oa->write_character(to_char_type(0xD2));
  465. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  466. }
  467. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  468. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  469. {
  470. // int 64
  471. oa->write_character(to_char_type(0xD3));
  472. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  473. }
  474. }
  475. break;
  476. }
  477. case value_t::number_unsigned:
  478. {
  479. if (j.m_data.m_value.number_unsigned < 128)
  480. {
  481. // positive fixnum
  482. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  483. }
  484. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  485. {
  486. // uint 8
  487. oa->write_character(to_char_type(0xCC));
  488. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  489. }
  490. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  491. {
  492. // uint 16
  493. oa->write_character(to_char_type(0xCD));
  494. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  495. }
  496. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  497. {
  498. // uint 32
  499. oa->write_character(to_char_type(0xCE));
  500. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  501. }
  502. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  503. {
  504. // uint 64
  505. oa->write_character(to_char_type(0xCF));
  506. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  507. }
  508. break;
  509. }
  510. case value_t::number_float:
  511. {
  512. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  513. break;
  514. }
  515. case value_t::string:
  516. {
  517. // step 1: write control byte and the string length
  518. const auto N = j.m_data.m_value.string->size();
  519. if (N <= 31)
  520. {
  521. // fixstr
  522. write_number(static_cast<std::uint8_t>(0xA0 | N));
  523. }
  524. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  525. {
  526. // str 8
  527. oa->write_character(to_char_type(0xD9));
  528. write_number(static_cast<std::uint8_t>(N));
  529. }
  530. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  531. {
  532. // str 16
  533. oa->write_character(to_char_type(0xDA));
  534. write_number(static_cast<std::uint16_t>(N));
  535. }
  536. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  537. {
  538. // str 32
  539. oa->write_character(to_char_type(0xDB));
  540. write_number(static_cast<std::uint32_t>(N));
  541. }
  542. // step 2: write the string
  543. oa->write_characters(
  544. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  545. j.m_data.m_value.string->size());
  546. break;
  547. }
  548. case value_t::array:
  549. {
  550. // step 1: write control byte and the array size
  551. const auto N = j.m_data.m_value.array->size();
  552. if (N <= 15)
  553. {
  554. // fixarray
  555. write_number(static_cast<std::uint8_t>(0x90 | N));
  556. }
  557. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  558. {
  559. // array 16
  560. oa->write_character(to_char_type(0xDC));
  561. write_number(static_cast<std::uint16_t>(N));
  562. }
  563. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  564. {
  565. // array 32
  566. oa->write_character(to_char_type(0xDD));
  567. write_number(static_cast<std::uint32_t>(N));
  568. }
  569. // step 2: write each element
  570. for (const auto& el : *j.m_data.m_value.array)
  571. {
  572. write_msgpack(el);
  573. }
  574. break;
  575. }
  576. case value_t::binary:
  577. {
  578. // step 0: determine if the binary type has a set subtype to
  579. // determine whether or not to use the ext or fixext types
  580. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  581. // step 1: write control byte and the byte string length
  582. const auto N = j.m_data.m_value.binary->size();
  583. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  584. {
  585. std::uint8_t output_type{};
  586. bool fixed = true;
  587. if (use_ext)
  588. {
  589. switch (N)
  590. {
  591. case 1:
  592. output_type = 0xD4; // fixext 1
  593. break;
  594. case 2:
  595. output_type = 0xD5; // fixext 2
  596. break;
  597. case 4:
  598. output_type = 0xD6; // fixext 4
  599. break;
  600. case 8:
  601. output_type = 0xD7; // fixext 8
  602. break;
  603. case 16:
  604. output_type = 0xD8; // fixext 16
  605. break;
  606. default:
  607. output_type = 0xC7; // ext 8
  608. fixed = false;
  609. break;
  610. }
  611. }
  612. else
  613. {
  614. output_type = 0xC4; // bin 8
  615. fixed = false;
  616. }
  617. oa->write_character(to_char_type(output_type));
  618. if (!fixed)
  619. {
  620. write_number(static_cast<std::uint8_t>(N));
  621. }
  622. }
  623. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  624. {
  625. const std::uint8_t output_type = use_ext
  626. ? 0xC8 // ext 16
  627. : 0xC5; // bin 16
  628. oa->write_character(to_char_type(output_type));
  629. write_number(static_cast<std::uint16_t>(N));
  630. }
  631. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  632. {
  633. const std::uint8_t output_type = use_ext
  634. ? 0xC9 // ext 32
  635. : 0xC6; // bin 32
  636. oa->write_character(to_char_type(output_type));
  637. write_number(static_cast<std::uint32_t>(N));
  638. }
  639. // step 1.5: if this is an ext type, write the subtype
  640. if (use_ext)
  641. {
  642. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  643. }
  644. // step 2: write the byte string
  645. oa->write_characters(
  646. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  647. N);
  648. break;
  649. }
  650. case value_t::object:
  651. {
  652. // step 1: write control byte and the object size
  653. const auto N = j.m_data.m_value.object->size();
  654. if (N <= 15)
  655. {
  656. // fixmap
  657. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  658. }
  659. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  660. {
  661. // map 16
  662. oa->write_character(to_char_type(0xDE));
  663. write_number(static_cast<std::uint16_t>(N));
  664. }
  665. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  666. {
  667. // map 32
  668. oa->write_character(to_char_type(0xDF));
  669. write_number(static_cast<std::uint32_t>(N));
  670. }
  671. // step 2: write each element
  672. for (const auto& el : *j.m_data.m_value.object)
  673. {
  674. write_msgpack(el.first);
  675. write_msgpack(el.second);
  676. }
  677. break;
  678. }
  679. case value_t::discarded:
  680. default:
  681. break;
  682. }
  683. }
  684. /*!
  685. @param[in] j JSON value to serialize
  686. @param[in] use_count whether to use '#' prefixes (optimized format)
  687. @param[in] use_type whether to use '$' prefixes (optimized format)
  688. @param[in] add_prefix whether prefixes need to be used for this value
  689. @param[in] use_bjdata whether write in BJData format, default is false
  690. */
  691. void write_ubjson(const BasicJsonType& j, const bool use_count,
  692. const bool use_type, const bool add_prefix = true,
  693. const bool use_bjdata = false)
  694. {
  695. switch (j.type())
  696. {
  697. case value_t::null:
  698. {
  699. if (add_prefix)
  700. {
  701. oa->write_character(to_char_type('Z'));
  702. }
  703. break;
  704. }
  705. case value_t::boolean:
  706. {
  707. if (add_prefix)
  708. {
  709. oa->write_character(j.m_data.m_value.boolean
  710. ? to_char_type('T')
  711. : to_char_type('F'));
  712. }
  713. break;
  714. }
  715. case value_t::number_integer:
  716. {
  717. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  718. break;
  719. }
  720. case value_t::number_unsigned:
  721. {
  722. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  723. break;
  724. }
  725. case value_t::number_float:
  726. {
  727. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  728. break;
  729. }
  730. case value_t::string:
  731. {
  732. if (add_prefix)
  733. {
  734. oa->write_character(to_char_type('S'));
  735. }
  736. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  737. oa->write_characters(
  738. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  739. j.m_data.m_value.string->size());
  740. break;
  741. }
  742. case value_t::array:
  743. {
  744. if (add_prefix)
  745. {
  746. oa->write_character(to_char_type('['));
  747. }
  748. bool prefix_required = true;
  749. if (use_type && !j.m_data.m_value.array->empty())
  750. {
  751. JSON_ASSERT(use_count);
  752. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  753. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  754. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  755. {
  756. return ubjson_prefix(v, use_bjdata) == first_prefix;
  757. });
  758. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  759. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  760. {
  761. prefix_required = false;
  762. oa->write_character(to_char_type('$'));
  763. oa->write_character(first_prefix);
  764. }
  765. }
  766. if (use_count)
  767. {
  768. oa->write_character(to_char_type('#'));
  769. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  770. }
  771. for (const auto& el : *j.m_data.m_value.array)
  772. {
  773. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  774. }
  775. if (!use_count)
  776. {
  777. oa->write_character(to_char_type(']'));
  778. }
  779. break;
  780. }
  781. case value_t::binary:
  782. {
  783. if (add_prefix)
  784. {
  785. oa->write_character(to_char_type('['));
  786. }
  787. if (use_type && !j.m_data.m_value.binary->empty())
  788. {
  789. JSON_ASSERT(use_count);
  790. oa->write_character(to_char_type('$'));
  791. oa->write_character('U');
  792. }
  793. if (use_count)
  794. {
  795. oa->write_character(to_char_type('#'));
  796. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  797. }
  798. if (use_type)
  799. {
  800. oa->write_characters(
  801. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  802. j.m_data.m_value.binary->size());
  803. }
  804. else
  805. {
  806. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  807. {
  808. oa->write_character(to_char_type('U'));
  809. oa->write_character(j.m_data.m_value.binary->data()[i]);
  810. }
  811. }
  812. if (!use_count)
  813. {
  814. oa->write_character(to_char_type(']'));
  815. }
  816. break;
  817. }
  818. case value_t::object:
  819. {
  820. if (use_bjdata && j.m_data.m_value.object->size() == 3 && j.m_data.m_value.object->find("_ArrayType_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArraySize_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArrayData_") != j.m_data.m_value.object->end())
  821. {
  822. if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  823. {
  824. break;
  825. }
  826. }
  827. if (add_prefix)
  828. {
  829. oa->write_character(to_char_type('{'));
  830. }
  831. bool prefix_required = true;
  832. if (use_type && !j.m_data.m_value.object->empty())
  833. {
  834. JSON_ASSERT(use_count);
  835. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  836. const bool same_prefix = std::all_of(j.begin(), j.end(),
  837. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  838. {
  839. return ubjson_prefix(v, use_bjdata) == first_prefix;
  840. });
  841. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  842. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  843. {
  844. prefix_required = false;
  845. oa->write_character(to_char_type('$'));
  846. oa->write_character(first_prefix);
  847. }
  848. }
  849. if (use_count)
  850. {
  851. oa->write_character(to_char_type('#'));
  852. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  853. }
  854. for (const auto& el : *j.m_data.m_value.object)
  855. {
  856. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  857. oa->write_characters(
  858. reinterpret_cast<const CharType*>(el.first.c_str()),
  859. el.first.size());
  860. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  861. }
  862. if (!use_count)
  863. {
  864. oa->write_character(to_char_type('}'));
  865. }
  866. break;
  867. }
  868. case value_t::discarded:
  869. default:
  870. break;
  871. }
  872. }
  873. private:
  874. //////////
  875. // BSON //
  876. //////////
  877. /*!
  878. @return The size of a BSON document entry header, including the id marker
  879. and the entry name size (and its null-terminator).
  880. */
  881. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  882. {
  883. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  884. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  885. {
  886. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  887. static_cast<void>(j);
  888. }
  889. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  890. }
  891. /*!
  892. @brief Writes the given @a element_type and @a name to the output adapter
  893. */
  894. void write_bson_entry_header(const string_t& name,
  895. const std::uint8_t element_type)
  896. {
  897. oa->write_character(to_char_type(element_type)); // boolean
  898. oa->write_characters(
  899. reinterpret_cast<const CharType*>(name.c_str()),
  900. name.size() + 1u);
  901. }
  902. /*!
  903. @brief Writes a BSON element with key @a name and boolean value @a value
  904. */
  905. void write_bson_boolean(const string_t& name,
  906. const bool value)
  907. {
  908. write_bson_entry_header(name, 0x08);
  909. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  910. }
  911. /*!
  912. @brief Writes a BSON element with key @a name and double value @a value
  913. */
  914. void write_bson_double(const string_t& name,
  915. const double value)
  916. {
  917. write_bson_entry_header(name, 0x01);
  918. write_number<double>(value, true);
  919. }
  920. /*!
  921. @return The size of the BSON-encoded string in @a value
  922. */
  923. static std::size_t calc_bson_string_size(const string_t& value)
  924. {
  925. return sizeof(std::int32_t) + value.size() + 1ul;
  926. }
  927. /*!
  928. @brief Writes a BSON element with key @a name and string value @a value
  929. */
  930. void write_bson_string(const string_t& name,
  931. const string_t& value)
  932. {
  933. write_bson_entry_header(name, 0x02);
  934. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  935. oa->write_characters(
  936. reinterpret_cast<const CharType*>(value.c_str()),
  937. value.size() + 1);
  938. }
  939. /*!
  940. @brief Writes a BSON element with key @a name and null value
  941. */
  942. void write_bson_null(const string_t& name)
  943. {
  944. write_bson_entry_header(name, 0x0A);
  945. }
  946. /*!
  947. @return The size of the BSON-encoded integer @a value
  948. */
  949. static std::size_t calc_bson_integer_size(const std::int64_t value)
  950. {
  951. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  952. ? sizeof(std::int32_t)
  953. : sizeof(std::int64_t);
  954. }
  955. /*!
  956. @brief Writes a BSON element with key @a name and integer @a value
  957. */
  958. void write_bson_integer(const string_t& name,
  959. const std::int64_t value)
  960. {
  961. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  962. {
  963. write_bson_entry_header(name, 0x10); // int32
  964. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  965. }
  966. else
  967. {
  968. write_bson_entry_header(name, 0x12); // int64
  969. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  970. }
  971. }
  972. /*!
  973. @return The size of the BSON-encoded unsigned integer in @a j
  974. */
  975. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  976. {
  977. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  978. ? sizeof(std::int32_t)
  979. : sizeof(std::int64_t);
  980. }
  981. /*!
  982. @brief Writes a BSON element with key @a name and unsigned @a value
  983. */
  984. void write_bson_unsigned(const string_t& name,
  985. const BasicJsonType& j)
  986. {
  987. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  988. {
  989. write_bson_entry_header(name, 0x10 /* int32 */);
  990. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  991. }
  992. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  993. {
  994. write_bson_entry_header(name, 0x12 /* int64 */);
  995. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  996. }
  997. else
  998. {
  999. JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_data.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j));
  1000. }
  1001. }
  1002. /*!
  1003. @brief Writes a BSON element with key @a name and object @a value
  1004. */
  1005. void write_bson_object_entry(const string_t& name,
  1006. const typename BasicJsonType::object_t& value)
  1007. {
  1008. write_bson_entry_header(name, 0x03); // object
  1009. write_bson_object(value);
  1010. }
  1011. /*!
  1012. @return The size of the BSON-encoded array @a value
  1013. */
  1014. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  1015. {
  1016. std::size_t array_index = 0ul;
  1017. const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast<std::size_t>(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
  1018. {
  1019. return result + calc_bson_element_size(std::to_string(array_index++), el);
  1020. });
  1021. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  1022. }
  1023. /*!
  1024. @return The size of the BSON-encoded binary array @a value
  1025. */
  1026. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  1027. {
  1028. return sizeof(std::int32_t) + value.size() + 1ul;
  1029. }
  1030. /*!
  1031. @brief Writes a BSON element with key @a name and array @a value
  1032. */
  1033. void write_bson_array(const string_t& name,
  1034. const typename BasicJsonType::array_t& value)
  1035. {
  1036. write_bson_entry_header(name, 0x04); // array
  1037. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  1038. std::size_t array_index = 0ul;
  1039. for (const auto& el : value)
  1040. {
  1041. write_bson_element(std::to_string(array_index++), el);
  1042. }
  1043. oa->write_character(to_char_type(0x00));
  1044. }
  1045. /*!
  1046. @brief Writes a BSON element with key @a name and binary value @a value
  1047. */
  1048. void write_bson_binary(const string_t& name,
  1049. const binary_t& value)
  1050. {
  1051. write_bson_entry_header(name, 0x05);
  1052. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  1053. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  1054. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  1055. }
  1056. /*!
  1057. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  1058. @return The calculated size for the BSON document entry for @a j with the given @a name.
  1059. */
  1060. static std::size_t calc_bson_element_size(const string_t& name,
  1061. const BasicJsonType& j)
  1062. {
  1063. const auto header_size = calc_bson_entry_header_size(name, j);
  1064. switch (j.type())
  1065. {
  1066. case value_t::object:
  1067. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  1068. case value_t::array:
  1069. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  1070. case value_t::binary:
  1071. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  1072. case value_t::boolean:
  1073. return header_size + 1ul;
  1074. case value_t::number_float:
  1075. return header_size + 8ul;
  1076. case value_t::number_integer:
  1077. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  1078. case value_t::number_unsigned:
  1079. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  1080. case value_t::string:
  1081. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  1082. case value_t::null:
  1083. return header_size + 0ul;
  1084. // LCOV_EXCL_START
  1085. case value_t::discarded:
  1086. default:
  1087. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  1088. return 0ul;
  1089. // LCOV_EXCL_STOP
  1090. }
  1091. }
  1092. /*!
  1093. @brief Serializes the JSON value @a j to BSON and associates it with the
  1094. key @a name.
  1095. @param name The name to associate with the JSON entity @a j within the
  1096. current BSON document
  1097. */
  1098. void write_bson_element(const string_t& name,
  1099. const BasicJsonType& j)
  1100. {
  1101. switch (j.type())
  1102. {
  1103. case value_t::object:
  1104. return write_bson_object_entry(name, *j.m_data.m_value.object);
  1105. case value_t::array:
  1106. return write_bson_array(name, *j.m_data.m_value.array);
  1107. case value_t::binary:
  1108. return write_bson_binary(name, *j.m_data.m_value.binary);
  1109. case value_t::boolean:
  1110. return write_bson_boolean(name, j.m_data.m_value.boolean);
  1111. case value_t::number_float:
  1112. return write_bson_double(name, j.m_data.m_value.number_float);
  1113. case value_t::number_integer:
  1114. return write_bson_integer(name, j.m_data.m_value.number_integer);
  1115. case value_t::number_unsigned:
  1116. return write_bson_unsigned(name, j);
  1117. case value_t::string:
  1118. return write_bson_string(name, *j.m_data.m_value.string);
  1119. case value_t::null:
  1120. return write_bson_null(name);
  1121. // LCOV_EXCL_START
  1122. case value_t::discarded:
  1123. default:
  1124. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  1125. return;
  1126. // LCOV_EXCL_STOP
  1127. }
  1128. }
  1129. /*!
  1130. @brief Calculates the size of the BSON serialization of the given
  1131. JSON-object @a j.
  1132. @param[in] value JSON value to serialize
  1133. @pre value.type() == value_t::object
  1134. */
  1135. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  1136. {
  1137. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  1138. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  1139. {
  1140. return result += calc_bson_element_size(el.first, el.second);
  1141. });
  1142. return sizeof(std::int32_t) + document_size + 1ul;
  1143. }
  1144. /*!
  1145. @param[in] value JSON value to serialize
  1146. @pre value.type() == value_t::object
  1147. */
  1148. void write_bson_object(const typename BasicJsonType::object_t& value)
  1149. {
  1150. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  1151. for (const auto& el : value)
  1152. {
  1153. write_bson_element(el.first, el.second);
  1154. }
  1155. oa->write_character(to_char_type(0x00));
  1156. }
  1157. //////////
  1158. // CBOR //
  1159. //////////
  1160. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  1161. {
  1162. return to_char_type(0xFA); // Single-Precision Float
  1163. }
  1164. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  1165. {
  1166. return to_char_type(0xFB); // Double-Precision Float
  1167. }
  1168. /////////////
  1169. // MsgPack //
  1170. /////////////
  1171. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  1172. {
  1173. return to_char_type(0xCA); // float 32
  1174. }
  1175. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  1176. {
  1177. return to_char_type(0xCB); // float 64
  1178. }
  1179. ////////////
  1180. // UBJSON //
  1181. ////////////
  1182. // UBJSON: write number (floating point)
  1183. template<typename NumberType, typename std::enable_if<
  1184. std::is_floating_point<NumberType>::value, int>::type = 0>
  1185. void write_number_with_ubjson_prefix(const NumberType n,
  1186. const bool add_prefix,
  1187. const bool use_bjdata)
  1188. {
  1189. if (add_prefix)
  1190. {
  1191. oa->write_character(get_ubjson_float_prefix(n));
  1192. }
  1193. write_number(n, use_bjdata);
  1194. }
  1195. // UBJSON: write number (unsigned integer)
  1196. template<typename NumberType, typename std::enable_if<
  1197. std::is_unsigned<NumberType>::value, int>::type = 0>
  1198. void write_number_with_ubjson_prefix(const NumberType n,
  1199. const bool add_prefix,
  1200. const bool use_bjdata)
  1201. {
  1202. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  1203. {
  1204. if (add_prefix)
  1205. {
  1206. oa->write_character(to_char_type('i')); // int8
  1207. }
  1208. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  1209. }
  1210. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  1211. {
  1212. if (add_prefix)
  1213. {
  1214. oa->write_character(to_char_type('U')); // uint8
  1215. }
  1216. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  1217. }
  1218. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  1219. {
  1220. if (add_prefix)
  1221. {
  1222. oa->write_character(to_char_type('I')); // int16
  1223. }
  1224. write_number(static_cast<std::int16_t>(n), use_bjdata);
  1225. }
  1226. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  1227. {
  1228. if (add_prefix)
  1229. {
  1230. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  1231. }
  1232. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  1233. }
  1234. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  1235. {
  1236. if (add_prefix)
  1237. {
  1238. oa->write_character(to_char_type('l')); // int32
  1239. }
  1240. write_number(static_cast<std::int32_t>(n), use_bjdata);
  1241. }
  1242. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  1243. {
  1244. if (add_prefix)
  1245. {
  1246. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  1247. }
  1248. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  1249. }
  1250. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  1251. {
  1252. if (add_prefix)
  1253. {
  1254. oa->write_character(to_char_type('L')); // int64
  1255. }
  1256. write_number(static_cast<std::int64_t>(n), use_bjdata);
  1257. }
  1258. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  1259. {
  1260. if (add_prefix)
  1261. {
  1262. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  1263. }
  1264. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  1265. }
  1266. else
  1267. {
  1268. if (add_prefix)
  1269. {
  1270. oa->write_character(to_char_type('H')); // high-precision number
  1271. }
  1272. const auto number = BasicJsonType(n).dump();
  1273. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  1274. for (std::size_t i = 0; i < number.size(); ++i)
  1275. {
  1276. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  1277. }
  1278. }
  1279. }
  1280. // UBJSON: write number (signed integer)
  1281. template < typename NumberType, typename std::enable_if <
  1282. std::is_signed<NumberType>::value&&
  1283. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  1284. void write_number_with_ubjson_prefix(const NumberType n,
  1285. const bool add_prefix,
  1286. const bool use_bjdata)
  1287. {
  1288. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  1289. {
  1290. if (add_prefix)
  1291. {
  1292. oa->write_character(to_char_type('i')); // int8
  1293. }
  1294. write_number(static_cast<std::int8_t>(n), use_bjdata);
  1295. }
  1296. else if (static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::max)()))
  1297. {
  1298. if (add_prefix)
  1299. {
  1300. oa->write_character(to_char_type('U')); // uint8
  1301. }
  1302. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  1303. }
  1304. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  1305. {
  1306. if (add_prefix)
  1307. {
  1308. oa->write_character(to_char_type('I')); // int16
  1309. }
  1310. write_number(static_cast<std::int16_t>(n), use_bjdata);
  1311. }
  1312. else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::max)())))
  1313. {
  1314. if (add_prefix)
  1315. {
  1316. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  1317. }
  1318. write_number(static_cast<uint16_t>(n), use_bjdata);
  1319. }
  1320. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  1321. {
  1322. if (add_prefix)
  1323. {
  1324. oa->write_character(to_char_type('l')); // int32
  1325. }
  1326. write_number(static_cast<std::int32_t>(n), use_bjdata);
  1327. }
  1328. else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::max)())))
  1329. {
  1330. if (add_prefix)
  1331. {
  1332. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  1333. }
  1334. write_number(static_cast<uint32_t>(n), use_bjdata);
  1335. }
  1336. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  1337. {
  1338. if (add_prefix)
  1339. {
  1340. oa->write_character(to_char_type('L')); // int64
  1341. }
  1342. write_number(static_cast<std::int64_t>(n), use_bjdata);
  1343. }
  1344. // LCOV_EXCL_START
  1345. else
  1346. {
  1347. if (add_prefix)
  1348. {
  1349. oa->write_character(to_char_type('H')); // high-precision number
  1350. }
  1351. const auto number = BasicJsonType(n).dump();
  1352. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  1353. for (std::size_t i = 0; i < number.size(); ++i)
  1354. {
  1355. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  1356. }
  1357. }
  1358. // LCOV_EXCL_STOP
  1359. }
  1360. /*!
  1361. @brief determine the type prefix of container values
  1362. */
  1363. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  1364. {
  1365. switch (j.type())
  1366. {
  1367. case value_t::null:
  1368. return 'Z';
  1369. case value_t::boolean:
  1370. return j.m_data.m_value.boolean ? 'T' : 'F';
  1371. case value_t::number_integer:
  1372. {
  1373. if ((std::numeric_limits<std::int8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  1374. {
  1375. return 'i';
  1376. }
  1377. if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  1378. {
  1379. return 'U';
  1380. }
  1381. if ((std::numeric_limits<std::int16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  1382. {
  1383. return 'I';
  1384. }
  1385. if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
  1386. {
  1387. return 'u';
  1388. }
  1389. if ((std::numeric_limits<std::int32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  1390. {
  1391. return 'l';
  1392. }
  1393. if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
  1394. {
  1395. return 'm';
  1396. }
  1397. if ((std::numeric_limits<std::int64_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  1398. {
  1399. return 'L';
  1400. }
  1401. // anything else is treated as high-precision number
  1402. return 'H'; // LCOV_EXCL_LINE
  1403. }
  1404. case value_t::number_unsigned:
  1405. {
  1406. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  1407. {
  1408. return 'i';
  1409. }
  1410. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  1411. {
  1412. return 'U';
  1413. }
  1414. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  1415. {
  1416. return 'I';
  1417. }
  1418. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  1419. {
  1420. return 'u';
  1421. }
  1422. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  1423. {
  1424. return 'l';
  1425. }
  1426. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  1427. {
  1428. return 'm';
  1429. }
  1430. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  1431. {
  1432. return 'L';
  1433. }
  1434. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  1435. {
  1436. return 'M';
  1437. }
  1438. // anything else is treated as high-precision number
  1439. return 'H'; // LCOV_EXCL_LINE
  1440. }
  1441. case value_t::number_float:
  1442. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  1443. case value_t::string:
  1444. return 'S';
  1445. case value_t::array: // fallthrough
  1446. case value_t::binary:
  1447. return '[';
  1448. case value_t::object:
  1449. return '{';
  1450. case value_t::discarded:
  1451. default: // discarded values
  1452. return 'N';
  1453. }
  1454. }
  1455. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  1456. {
  1457. return 'd'; // float 32
  1458. }
  1459. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  1460. {
  1461. return 'D'; // float 64
  1462. }
  1463. /*!
  1464. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  1465. */
  1466. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  1467. {
  1468. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  1469. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  1470. };
  1471. string_t key = "_ArrayType_";
  1472. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  1473. if (it == bjdtype.end())
  1474. {
  1475. return true;
  1476. }
  1477. CharType dtype = it->second;
  1478. key = "_ArraySize_";
  1479. std::size_t len = (value.at(key).empty() ? 0 : 1);
  1480. for (const auto& el : value.at(key))
  1481. {
  1482. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  1483. }
  1484. key = "_ArrayData_";
  1485. if (value.at(key).size() != len)
  1486. {
  1487. return true;
  1488. }
  1489. oa->write_character('[');
  1490. oa->write_character('$');
  1491. oa->write_character(dtype);
  1492. oa->write_character('#');
  1493. key = "_ArraySize_";
  1494. write_ubjson(value.at(key), use_count, use_type, true, true);
  1495. key = "_ArrayData_";
  1496. if (dtype == 'U' || dtype == 'C')
  1497. {
  1498. for (const auto& el : value.at(key))
  1499. {
  1500. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  1501. }
  1502. }
  1503. else if (dtype == 'i')
  1504. {
  1505. for (const auto& el : value.at(key))
  1506. {
  1507. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  1508. }
  1509. }
  1510. else if (dtype == 'u')
  1511. {
  1512. for (const auto& el : value.at(key))
  1513. {
  1514. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  1515. }
  1516. }
  1517. else if (dtype == 'I')
  1518. {
  1519. for (const auto& el : value.at(key))
  1520. {
  1521. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  1522. }
  1523. }
  1524. else if (dtype == 'm')
  1525. {
  1526. for (const auto& el : value.at(key))
  1527. {
  1528. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  1529. }
  1530. }
  1531. else if (dtype == 'l')
  1532. {
  1533. for (const auto& el : value.at(key))
  1534. {
  1535. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  1536. }
  1537. }
  1538. else if (dtype == 'M')
  1539. {
  1540. for (const auto& el : value.at(key))
  1541. {
  1542. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  1543. }
  1544. }
  1545. else if (dtype == 'L')
  1546. {
  1547. for (const auto& el : value.at(key))
  1548. {
  1549. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  1550. }
  1551. }
  1552. else if (dtype == 'd')
  1553. {
  1554. for (const auto& el : value.at(key))
  1555. {
  1556. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  1557. }
  1558. }
  1559. else if (dtype == 'D')
  1560. {
  1561. for (const auto& el : value.at(key))
  1562. {
  1563. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  1564. }
  1565. }
  1566. return false;
  1567. }
  1568. ///////////////////////
  1569. // Utility functions //
  1570. ///////////////////////
  1571. /*
  1572. @brief write a number to output input
  1573. @param[in] n number of type @a NumberType
  1574. @param[in] OutputIsLittleEndian Set to true if output data is
  1575. required to be little endian
  1576. @tparam NumberType the type of the number
  1577. @note This function needs to respect the system's endianness, because bytes
  1578. in CBOR, MessagePack, and UBJSON are stored in network order (big
  1579. endian) and therefore need reordering on little endian systems.
  1580. On the other hand, BSON and BJData use little endian and should reorder
  1581. on big endian systems.
  1582. */
  1583. template<typename NumberType>
  1584. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  1585. {
  1586. // step 1: write number to array of length NumberType
  1587. std::array<CharType, sizeof(NumberType)> vec{};
  1588. std::memcpy(vec.data(), &n, sizeof(NumberType));
  1589. // step 2: write array to output (with possible reordering)
  1590. if (is_little_endian != OutputIsLittleEndian)
  1591. {
  1592. // reverse byte order prior to conversion if necessary
  1593. std::reverse(vec.begin(), vec.end());
  1594. }
  1595. oa->write_characters(vec.data(), sizeof(NumberType));
  1596. }
  1597. void write_compact_float(const number_float_t n, detail::input_format_t format)
  1598. {
  1599. #ifdef __GNUC__
  1600. #pragma GCC diagnostic push
  1601. #pragma GCC diagnostic ignored "-Wfloat-equal"
  1602. #endif
  1603. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  1604. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  1605. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  1606. {
  1607. oa->write_character(format == detail::input_format_t::cbor
  1608. ? get_cbor_float_prefix(static_cast<float>(n))
  1609. : get_msgpack_float_prefix(static_cast<float>(n)));
  1610. write_number(static_cast<float>(n));
  1611. }
  1612. else
  1613. {
  1614. oa->write_character(format == detail::input_format_t::cbor
  1615. ? get_cbor_float_prefix(n)
  1616. : get_msgpack_float_prefix(n));
  1617. write_number(n);
  1618. }
  1619. #ifdef __GNUC__
  1620. #pragma GCC diagnostic pop
  1621. #endif
  1622. }
  1623. public:
  1624. // The following to_char_type functions are implement the conversion
  1625. // between uint8_t and CharType. In case CharType is not unsigned,
  1626. // such a conversion is required to allow values greater than 128.
  1627. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  1628. template < typename C = CharType,
  1629. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  1630. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  1631. {
  1632. return *reinterpret_cast<char*>(&x);
  1633. }
  1634. template < typename C = CharType,
  1635. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  1636. static CharType to_char_type(std::uint8_t x) noexcept
  1637. {
  1638. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  1639. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  1640. CharType result;
  1641. std::memcpy(&result, &x, sizeof(x));
  1642. return result;
  1643. }
  1644. template<typename C = CharType,
  1645. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  1646. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  1647. {
  1648. return x;
  1649. }
  1650. template < typename InputCharType, typename C = CharType,
  1651. enable_if_t <
  1652. std::is_signed<C>::value &&
  1653. std::is_signed<char>::value &&
  1654. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  1655. > * = nullptr >
  1656. static constexpr CharType to_char_type(InputCharType x) noexcept
  1657. {
  1658. return x;
  1659. }
  1660. private:
  1661. /// whether we can assume little endianness
  1662. const bool is_little_endian = little_endianness();
  1663. /// the output
  1664. output_adapter_t<CharType> oa = nullptr;
  1665. };
  1666. } // namespace detail
  1667. NLOHMANN_JSON_NAMESPACE_END