48 using string_t =
typename BasicJsonType::string_t;
49 using binary_t =
typename BasicJsonType::binary_t;
50 using number_float_t =
typename BasicJsonType::number_float_t;
73 write_bson_object(*j.m_data.m_value.object);
88 JSON_THROW(type_error::create(317, concat(
"to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
102 oa->write_character(to_char_type(0xF6));
108 oa->write_character(j.m_data.m_value.boolean
110 : to_char_type(0xF4));
116 if (j.m_data.m_value.number_integer >= 0)
121 if (j.m_data.m_value.number_integer <= 0x17)
123 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
125 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
127 oa->write_character(to_char_type(0x18));
128 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
130 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
132 oa->write_character(to_char_type(0x19));
133 write_number(
static_cast<std::uint16_t
>(j.m_data.m_value.number_integer));
135 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
137 oa->write_character(to_char_type(0x1A));
138 write_number(
static_cast<std::uint32_t
>(j.m_data.m_value.number_integer));
142 oa->write_character(to_char_type(0x1B));
143 write_number(
static_cast<std::uint64_t
>(j.m_data.m_value.number_integer));
150 const auto positive_number = -1 - j.m_data.m_value.number_integer;
151 if (j.m_data.m_value.number_integer >= -24)
153 write_number(
static_cast<std::uint8_t
>(0x20 + positive_number));
155 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
157 oa->write_character(to_char_type(0x38));
158 write_number(
static_cast<std::uint8_t
>(positive_number));
160 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
162 oa->write_character(to_char_type(0x39));
163 write_number(
static_cast<std::uint16_t
>(positive_number));
165 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
167 oa->write_character(to_char_type(0x3A));
168 write_number(
static_cast<std::uint32_t
>(positive_number));
172 oa->write_character(to_char_type(0x3B));
173 write_number(
static_cast<std::uint64_t
>(positive_number));
181 if (j.m_data.m_value.number_unsigned <= 0x17)
183 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_unsigned));
185 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
187 oa->write_character(to_char_type(0x18));
188 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_unsigned));
190 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
192 oa->write_character(to_char_type(0x19));
193 write_number(
static_cast<std::uint16_t
>(j.m_data.m_value.number_unsigned));
195 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
197 oa->write_character(to_char_type(0x1A));
198 write_number(
static_cast<std::uint32_t
>(j.m_data.m_value.number_unsigned));
202 oa->write_character(to_char_type(0x1B));
203 write_number(
static_cast<std::uint64_t
>(j.m_data.m_value.number_unsigned));
210 if (std::isnan(j.m_data.m_value.number_float))
213 oa->write_character(to_char_type(0xF9));
214 oa->write_character(to_char_type(0x7E));
215 oa->write_character(to_char_type(0x00));
217 else if (std::isinf(j.m_data.m_value.number_float))
220 oa->write_character(to_char_type(0xf9));
221 oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
222 oa->write_character(to_char_type(0x00));
226 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
234 const auto N = j.m_data.m_value.string->size();
237 write_number(
static_cast<std::uint8_t
>(0x60 + N));
239 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
241 oa->write_character(to_char_type(0x78));
242 write_number(
static_cast<std::uint8_t
>(N));
244 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
246 oa->write_character(to_char_type(0x79));
247 write_number(
static_cast<std::uint16_t
>(N));
249 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
251 oa->write_character(to_char_type(0x7A));
252 write_number(
static_cast<std::uint32_t
>(N));
255 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
257 oa->write_character(to_char_type(0x7B));
258 write_number(
static_cast<std::uint64_t
>(N));
263 oa->write_characters(
264 reinterpret_cast<const CharType*
>(j.m_data.m_value.string->c_str()),
265 j.m_data.m_value.string->size());
272 const auto N = j.m_data.m_value.array->size();
275 write_number(
static_cast<std::uint8_t
>(0x80 + N));
277 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
279 oa->write_character(to_char_type(0x98));
280 write_number(
static_cast<std::uint8_t
>(N));
282 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
284 oa->write_character(to_char_type(0x99));
285 write_number(
static_cast<std::uint16_t
>(N));
287 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
289 oa->write_character(to_char_type(0x9A));
290 write_number(
static_cast<std::uint32_t
>(N));
293 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
295 oa->write_character(to_char_type(0x9B));
296 write_number(
static_cast<std::uint64_t
>(N));
301 for (
const auto& el : *j.m_data.m_value.array)
310 if (j.m_data.m_value.binary->has_subtype())
312 if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
314 write_number(
static_cast<std::uint8_t
>(0xd8));
315 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.binary->subtype()));
317 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
319 write_number(
static_cast<std::uint8_t
>(0xd9));
320 write_number(
static_cast<std::uint16_t
>(j.m_data.m_value.binary->subtype()));
322 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
324 write_number(
static_cast<std::uint8_t
>(0xda));
325 write_number(
static_cast<std::uint32_t
>(j.m_data.m_value.binary->subtype()));
327 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
329 write_number(
static_cast<std::uint8_t
>(0xdb));
330 write_number(
static_cast<std::uint64_t
>(j.m_data.m_value.binary->subtype()));
335 const auto N = j.m_data.m_value.binary->size();
338 write_number(
static_cast<std::uint8_t
>(0x40 + N));
340 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
342 oa->write_character(to_char_type(0x58));
343 write_number(
static_cast<std::uint8_t
>(N));
345 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
347 oa->write_character(to_char_type(0x59));
348 write_number(
static_cast<std::uint16_t
>(N));
350 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
352 oa->write_character(to_char_type(0x5A));
353 write_number(
static_cast<std::uint32_t
>(N));
356 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
358 oa->write_character(to_char_type(0x5B));
359 write_number(
static_cast<std::uint64_t
>(N));
364 oa->write_characters(
365 reinterpret_cast<const CharType*
>(j.m_data.m_value.binary->data()),
374 const auto N = j.m_data.m_value.object->size();
377 write_number(
static_cast<std::uint8_t
>(0xA0 + N));
379 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
381 oa->write_character(to_char_type(0xB8));
382 write_number(
static_cast<std::uint8_t
>(N));
384 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
386 oa->write_character(to_char_type(0xB9));
387 write_number(
static_cast<std::uint16_t
>(N));
389 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
391 oa->write_character(to_char_type(0xBA));
392 write_number(
static_cast<std::uint32_t
>(N));
395 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
397 oa->write_character(to_char_type(0xBB));
398 write_number(
static_cast<std::uint64_t
>(N));
403 for (
const auto& el : *j.m_data.m_value.object)
426 oa->write_character(to_char_type(0xC0));
432 oa->write_character(j.m_data.m_value.boolean
434 : to_char_type(0xC2));
440 if (j.m_data.m_value.number_integer >= 0)
445 if (j.m_data.m_value.number_unsigned < 128)
448 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
450 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
453 oa->write_character(to_char_type(0xCC));
454 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
456 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
459 oa->write_character(to_char_type(0xCD));
460 write_number(
static_cast<std::uint16_t
>(j.m_data.m_value.number_integer));
462 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
465 oa->write_character(to_char_type(0xCE));
466 write_number(
static_cast<std::uint32_t
>(j.m_data.m_value.number_integer));
468 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
471 oa->write_character(to_char_type(0xCF));
472 write_number(
static_cast<std::uint64_t
>(j.m_data.m_value.number_integer));
477 if (j.m_data.m_value.number_integer >= -32)
480 write_number(
static_cast<std::int8_t
>(j.m_data.m_value.number_integer));
482 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
483 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
486 oa->write_character(to_char_type(0xD0));
487 write_number(
static_cast<std::int8_t
>(j.m_data.m_value.number_integer));
489 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
490 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
493 oa->write_character(to_char_type(0xD1));
494 write_number(
static_cast<std::int16_t
>(j.m_data.m_value.number_integer));
496 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
497 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
500 oa->write_character(to_char_type(0xD2));
501 write_number(
static_cast<std::int32_t
>(j.m_data.m_value.number_integer));
503 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
504 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
507 oa->write_character(to_char_type(0xD3));
508 write_number(
static_cast<std::int64_t
>(j.m_data.m_value.number_integer));
516 if (j.m_data.m_value.number_unsigned < 128)
519 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
521 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
524 oa->write_character(to_char_type(0xCC));
525 write_number(
static_cast<std::uint8_t
>(j.m_data.m_value.number_integer));
527 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
530 oa->write_character(to_char_type(0xCD));
531 write_number(
static_cast<std::uint16_t
>(j.m_data.m_value.number_integer));
533 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
536 oa->write_character(to_char_type(0xCE));
537 write_number(
static_cast<std::uint32_t
>(j.m_data.m_value.number_integer));
539 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
542 oa->write_character(to_char_type(0xCF));
543 write_number(
static_cast<std::uint64_t
>(j.m_data.m_value.number_integer));
550 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
557 const auto N = j.m_data.m_value.string->size();
561 write_number(
static_cast<std::uint8_t
>(0xA0 | N));
563 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
566 oa->write_character(to_char_type(0xD9));
567 write_number(
static_cast<std::uint8_t
>(N));
569 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
572 oa->write_character(to_char_type(0xDA));
573 write_number(
static_cast<std::uint16_t
>(N));
575 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
578 oa->write_character(to_char_type(0xDB));
579 write_number(
static_cast<std::uint32_t
>(N));
583 oa->write_characters(
584 reinterpret_cast<const CharType*
>(j.m_data.m_value.string->c_str()),
585 j.m_data.m_value.string->size());
592 const auto N = j.m_data.m_value.array->size();
596 write_number(
static_cast<std::uint8_t
>(0x90 | N));
598 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
601 oa->write_character(to_char_type(0xDC));
602 write_number(
static_cast<std::uint16_t
>(N));
604 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
607 oa->write_character(to_char_type(0xDD));
608 write_number(
static_cast<std::uint32_t
>(N));
612 for (
const auto& el : *j.m_data.m_value.array)
623 const bool use_ext = j.m_data.m_value.binary->has_subtype();
626 const auto N = j.m_data.m_value.binary->size();
627 if (N <= (std::numeric_limits<std::uint8_t>::max)())
629 std::uint8_t output_type{};
663 oa->write_character(to_char_type(output_type));
666 write_number(
static_cast<std::uint8_t
>(N));
669 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
671 const std::uint8_t output_type = use_ext
675 oa->write_character(to_char_type(output_type));
676 write_number(
static_cast<std::uint16_t
>(N));
678 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
680 const std::uint8_t output_type = use_ext
684 oa->write_character(to_char_type(output_type));
685 write_number(
static_cast<std::uint32_t
>(N));
691 write_number(
static_cast<std::int8_t
>(j.m_data.m_value.binary->subtype()));
695 oa->write_characters(
696 reinterpret_cast<const CharType*
>(j.m_data.m_value.binary->data()),
705 const auto N = j.m_data.m_value.object->size();
709 write_number(
static_cast<std::uint8_t
>(0x80 | (N & 0xF)));
711 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
714 oa->write_character(to_char_type(0xDE));
715 write_number(
static_cast<std::uint16_t
>(N));
717 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
720 oa->write_character(to_char_type(0xDF));
721 write_number(
static_cast<std::uint32_t
>(N));
725 for (
const auto& el : *j.m_data.m_value.object)
748 const bool use_type,
const bool add_prefix =
true,
749 const bool use_bjdata =
false,
const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
751 const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
759 oa->write_character(to_char_type(
'Z'));
768 oa->write_character(j.m_data.m_value.boolean
770 : to_char_type(
'F'));
777 write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
783 write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
789 write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
797 oa->write_character(to_char_type(
'S'));
799 write_number_with_ubjson_prefix(j.m_data.m_value.string->size(),
true, use_bjdata);
800 oa->write_characters(
801 reinterpret_cast<const CharType*
>(j.m_data.m_value.string->c_str()),
802 j.m_data.m_value.string->size());
810 oa->write_character(to_char_type(
'['));
813 bool prefix_required =
true;
814 if (use_type && !j.m_data.m_value.array->empty())
816 JSON_ASSERT(use_count);
817 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
818 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
819 [
this, first_prefix, use_bjdata](
const BasicJsonType & v)
821 return ubjson_prefix(v, use_bjdata) == first_prefix;
824 std::vector<CharType> bjdx = {
'[',
'{',
'S',
'H',
'T',
'F',
'N',
'Z'};
826 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
828 prefix_required =
false;
829 oa->write_character(to_char_type(
'$'));
830 oa->write_character(first_prefix);
836 oa->write_character(to_char_type(
'#'));
837 write_number_with_ubjson_prefix(j.m_data.m_value.array->size(),
true, use_bjdata);
840 for (
const auto& el : *j.m_data.m_value.array)
842 write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
847 oa->write_character(to_char_type(
']'));
857 oa->write_character(to_char_type(
'['));
860 if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
862 JSON_ASSERT(use_count);
863 oa->write_character(to_char_type(
'$'));
864 oa->write_character(bjdata_draft3 ?
'B' :
'U');
869 oa->write_character(to_char_type(
'#'));
870 write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(),
true, use_bjdata);
875 oa->write_characters(
876 reinterpret_cast<const CharType*
>(j.m_data.m_value.binary->data()),
877 j.m_data.m_value.binary->size());
881 for (
size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
883 oa->write_character(to_char_type(bjdata_draft3 ?
'B' :
'U'));
884 oa->write_character(j.m_data.m_value.binary->data()[i]);
890 oa->write_character(to_char_type(
']'));
898 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())
900 if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type, bjdata_version))
908 oa->write_character(to_char_type(
'{'));
911 bool prefix_required =
true;
912 if (use_type && !j.m_data.m_value.object->empty())
914 JSON_ASSERT(use_count);
915 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
916 const bool same_prefix = std::all_of(j.begin(), j.end(),
917 [
this, first_prefix, use_bjdata](
const BasicJsonType & v)
919 return ubjson_prefix(v, use_bjdata) == first_prefix;
922 std::vector<CharType> bjdx = {
'[',
'{',
'S',
'H',
'T',
'F',
'N',
'Z'};
924 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
926 prefix_required =
false;
927 oa->write_character(to_char_type(
'$'));
928 oa->write_character(first_prefix);
934 oa->write_character(to_char_type(
'#'));
935 write_number_with_ubjson_prefix(j.m_data.m_value.object->size(),
true, use_bjdata);
938 for (
const auto& el : *j.m_data.m_value.object)
940 write_number_with_ubjson_prefix(el.first.size(),
true, use_bjdata);
941 oa->write_characters(
942 reinterpret_cast<const CharType*
>(el.first.c_str()),
944 write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
949 oa->write_character(to_char_type(
'}'));
970 static std::size_t calc_bson_entry_header_size(
const string_t& name,
const BasicJsonType& j)
972 const auto it = name.find(
static_cast<typename string_t::value_type
>(0));
973 if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
975 JSON_THROW(out_of_range::create(409, concat(
"BSON key cannot contain code point U+0000 (at byte ", std::to_string(it),
")"), &j));
978 static_cast<void>(j);
979 return 1ul + name.size() + 1u;
985 void write_bson_entry_header(
const string_t& name,
986 const std::uint8_t element_type)
988 oa->write_character(to_char_type(element_type));
989 oa->write_characters(
990 reinterpret_cast<const CharType*
>(name.c_str()),
997 void write_bson_boolean(
const string_t& name,
1000 write_bson_entry_header(name, 0x08);
1001 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
1007 void write_bson_double(
const string_t& name,
1010 write_bson_entry_header(name, 0x01);
1011 write_number<double>(value,
true);
1017 static std::size_t calc_bson_string_size(
const string_t& value)
1019 return sizeof(std::int32_t) + value.size() + 1ul;
1025 void write_bson_string(
const string_t& name,
1026 const string_t& value)
1028 write_bson_entry_header(name, 0x02);
1030 write_number<std::int32_t>(
static_cast<std::int32_t
>(value.size() + 1ul),
true);
1031 oa->write_characters(
1032 reinterpret_cast<const CharType*
>(value.c_str()),
1039 void write_bson_null(
const string_t& name)
1041 write_bson_entry_header(name, 0x0A);
1047 static std::size_t calc_bson_integer_size(
const std::int64_t value)
1049 return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
1050 ?
sizeof(std::int32_t)
1051 :
sizeof(std::int64_t);
1057 void write_bson_integer(
const string_t& name,
1058 const std::int64_t value)
1060 if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
1062 write_bson_entry_header(name, 0x10);
1063 write_number<std::int32_t>(
static_cast<std::int32_t
>(value),
true);
1067 write_bson_entry_header(name, 0x12);
1068 write_number<std::int64_t>(
static_cast<std::int64_t
>(value),
true);
1075 static constexpr std::size_t calc_bson_unsigned_size(
const std::uint64_t value)
noexcept
1077 return (value <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int32_t>::max)()))
1078 ?
sizeof(std::int32_t)
1079 : sizeof(std::int64_t);
1085 void write_bson_unsigned(
const string_t& name,
1086 const BasicJsonType& j)
1088 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int32_t>::max)()))
1090 write_bson_entry_header(name, 0x10 );
1091 write_number<std::int32_t>(
static_cast<std::int32_t
>(j.m_data.m_value.number_unsigned),
true);
1093 else if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int64_t>::max)()))
1095 write_bson_entry_header(name, 0x12 );
1096 write_number<std::int64_t>(
static_cast<std::int64_t
>(j.m_data.m_value.number_unsigned),
true);
1100 write_bson_entry_header(name, 0x11 );
1101 write_number<std::uint64_t>(
static_cast<std::uint64_t
>(j.m_data.m_value.number_unsigned),
true);
1108 void write_bson_object_entry(
const string_t& name,
1109 const typename BasicJsonType::object_t& value)
1111 write_bson_entry_header(name, 0x03);
1112 write_bson_object(value);
1118 static std::size_t calc_bson_array_size(
const typename BasicJsonType::array_t& value)
1120 std::size_t array_index = 0ul;
1122 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)
1124 return result + calc_bson_element_size(std::to_string(array_index++), el);
1127 return sizeof(std::int32_t) + embedded_document_size + 1ul;
1133 static std::size_t calc_bson_binary_size(
const typename BasicJsonType::binary_t& value)
1135 return sizeof(std::int32_t) + value.size() + 1ul;
1141 void write_bson_array(
const string_t& name,
1142 const typename BasicJsonType::array_t& value)
1144 write_bson_entry_header(name, 0x04);
1145 write_number<std::int32_t>(
static_cast<std::int32_t
>(calc_bson_array_size(value)),
true);
1147 std::size_t array_index = 0ul;
1149 for (
const auto& el : value)
1151 write_bson_element(std::to_string(array_index++), el);
1154 oa->write_character(to_char_type(0x00));
1160 void write_bson_binary(
const string_t& name,
1161 const binary_t& value)
1163 write_bson_entry_header(name, 0x05);
1165 write_number<std::int32_t>(
static_cast<std::int32_t
>(value.size()),
true);
1166 write_number(value.has_subtype() ?
static_cast<std::uint8_t
>(value.subtype()) :
static_cast<std::uint8_t
>(0x00));
1168 oa->write_characters(
reinterpret_cast<const CharType*
>(value.data()), value.size());
1175 static std::size_t calc_bson_element_size(
const string_t& name,
1176 const BasicJsonType& j)
1178 const auto header_size = calc_bson_entry_header_size(name, j);
1182 return header_size + calc_bson_object_size(*j.m_data.m_value.object);
1185 return header_size + calc_bson_array_size(*j.m_data.m_value.array);
1188 return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
1191 return header_size + 1ul;
1194 return header_size + 8ul;
1197 return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
1200 return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
1203 return header_size + calc_bson_string_size(*j.m_data.m_value.string);
1206 return header_size + 0ul;
1223 void write_bson_element(
const string_t& name,
1224 const BasicJsonType& j)
1229 return write_bson_object_entry(name, *j.m_data.m_value.object);
1232 return write_bson_array(name, *j.m_data.m_value.array);
1235 return write_bson_binary(name, *j.m_data.m_value.binary);
1238 return write_bson_boolean(name, j.m_data.m_value.boolean);
1241 return write_bson_double(name, j.m_data.m_value.number_float);
1244 return write_bson_integer(name, j.m_data.m_value.number_integer);
1247 return write_bson_unsigned(name, j);
1250 return write_bson_string(name, *j.m_data.m_value.string);
1253 return write_bson_null(name);
1270 static std::size_t calc_bson_object_size(
const typename BasicJsonType::object_t& value)
1272 const std::size_t document_size = std::accumulate(value.begin(), value.end(),
static_cast<std::size_t
>(0),
1273 [](
size_t result,
const typename BasicJsonType::object_t::value_type & el)
1275 return result += calc_bson_element_size(el.first, el.second);
1278 return sizeof(std::int32_t) + document_size + 1ul;
1285 void write_bson_object(
const typename BasicJsonType::object_t& value)
1287 write_number<std::int32_t>(
static_cast<std::int32_t
>(calc_bson_object_size(value)),
true);
1289 for (
const auto& el : value)
1291 write_bson_element(el.first, el.second);
1294 oa->write_character(to_char_type(0x00));
1301 static constexpr CharType get_cbor_float_prefix(
float )
1303 return to_char_type(0xFA);
1306 static constexpr CharType get_cbor_float_prefix(
double )
1308 return to_char_type(0xFB);
1315 static constexpr CharType get_msgpack_float_prefix(
float )
1317 return to_char_type(0xCA);
1320 static constexpr CharType get_msgpack_float_prefix(
double )
1322 return to_char_type(0xCB);
1330 template<
typename NumberType,
typename std::enable_if<
1331 std::is_floating_point<NumberType>::value,
int>::type = 0>
1332 void write_number_with_ubjson_prefix(
const NumberType n,
1333 const bool add_prefix,
1334 const bool use_bjdata)
1338 oa->write_character(get_ubjson_float_prefix(n));
1340 write_number(n, use_bjdata);
1344 template<
typename NumberType,
typename std::enable_if<
1345 std::is_unsigned<NumberType>::value,
int>::type = 0>
1346 void write_number_with_ubjson_prefix(
const NumberType n,
1347 const bool add_prefix,
1348 const bool use_bjdata)
1350 if (n <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int8_t>::max)()))
1354 oa->write_character(to_char_type(
'i'));
1356 write_number(
static_cast<std::uint8_t
>(n), use_bjdata);
1358 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
1362 oa->write_character(to_char_type(
'U'));
1364 write_number(
static_cast<std::uint8_t
>(n), use_bjdata);
1366 else if (n <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int16_t>::max)()))
1370 oa->write_character(to_char_type(
'I'));
1372 write_number(
static_cast<std::int16_t
>(n), use_bjdata);
1374 else if (use_bjdata && n <=
static_cast<uint64_t
>((std::numeric_limits<uint16_t>::max)()))
1378 oa->write_character(to_char_type(
'u'));
1380 write_number(
static_cast<std::uint16_t
>(n), use_bjdata);
1382 else if (n <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int32_t>::max)()))
1386 oa->write_character(to_char_type(
'l'));
1388 write_number(
static_cast<std::int32_t
>(n), use_bjdata);
1390 else if (use_bjdata && n <=
static_cast<uint64_t
>((std::numeric_limits<uint32_t>::max)()))
1394 oa->write_character(to_char_type(
'm'));
1396 write_number(
static_cast<std::uint32_t
>(n), use_bjdata);
1398 else if (n <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int64_t>::max)()))
1402 oa->write_character(to_char_type(
'L'));
1404 write_number(
static_cast<std::int64_t
>(n), use_bjdata);
1406 else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
1410 oa->write_character(to_char_type(
'M'));
1412 write_number(
static_cast<std::uint64_t
>(n), use_bjdata);
1418 oa->write_character(to_char_type(
'H'));
1421 const auto number = BasicJsonType(n).dump();
1422 write_number_with_ubjson_prefix(number.size(),
true, use_bjdata);
1423 for (std::size_t i = 0; i < number.size(); ++i)
1425 oa->write_character(to_char_type(
static_cast<std::uint8_t
>(number[i])));
1431 template <
typename NumberType,
typename std::enable_if <
1432 std::is_signed<NumberType>::value&&
1433 !std::is_floating_point<NumberType>::value,
int >::type = 0 >
1434 void write_number_with_ubjson_prefix(
const NumberType n,
1435 const bool add_prefix,
1436 const bool use_bjdata)
1438 if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
1442 oa->write_character(to_char_type(
'i'));
1444 write_number(
static_cast<std::int8_t
>(n), use_bjdata);
1446 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)()))
1450 oa->write_character(to_char_type(
'U'));
1452 write_number(
static_cast<std::uint8_t
>(n), use_bjdata);
1454 else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
1458 oa->write_character(to_char_type(
'I'));
1460 write_number(
static_cast<std::int16_t
>(n), use_bjdata);
1462 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)())))
1466 oa->write_character(to_char_type(
'u'));
1468 write_number(
static_cast<uint16_t
>(n), use_bjdata);
1470 else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
1474 oa->write_character(to_char_type(
'l'));
1476 write_number(
static_cast<std::int32_t
>(n), use_bjdata);
1478 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)())))
1482 oa->write_character(to_char_type(
'm'));
1484 write_number(
static_cast<uint32_t
>(n), use_bjdata);
1486 else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
1490 oa->write_character(to_char_type(
'L'));
1492 write_number(
static_cast<std::int64_t
>(n), use_bjdata);
1499 oa->write_character(to_char_type(
'H'));
1502 const auto number = BasicJsonType(n).dump();
1503 write_number_with_ubjson_prefix(number.size(),
true, use_bjdata);
1504 for (std::size_t i = 0; i < number.size(); ++i)
1506 oa->write_character(to_char_type(
static_cast<std::uint8_t
>(number[i])));
1515 CharType ubjson_prefix(
const BasicJsonType& j,
const bool use_bjdata)
const noexcept
1523 return j.m_data.m_value.boolean ?
'T' :
'F';
1527 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)())
1531 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)())
1535 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)())
1539 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)()))
1543 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)())
1547 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)()))
1551 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)())
1561 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int8_t>::max)()))
1565 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::uint8_t>::max)()))
1569 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int16_t>::max)()))
1573 if (use_bjdata && j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::uint16_t>::max)()))
1577 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int32_t>::max)()))
1581 if (use_bjdata && j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::uint32_t>::max)()))
1585 if (j.m_data.m_value.number_unsigned <=
static_cast<std::uint64_t
>((std::numeric_limits<std::int64_t>::max)()))
1589 if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
1598 return get_ubjson_float_prefix(j.m_data.m_value.number_float);
1616 static constexpr CharType get_ubjson_float_prefix(
float )
1621 static constexpr CharType get_ubjson_float_prefix(
double )
1629 bool write_bjdata_ndarray(
const typename BasicJsonType::object_t& value,
const bool use_count,
const bool use_type,
const bjdata_version_t bjdata_version)
1631 std::map<string_t, CharType> bjdtype = {{
"uint8",
'U'}, {
"int8",
'i'}, {
"uint16",
'u'}, {
"int16",
'I'},
1632 {
"uint32",
'm'}, {
"int32",
'l'}, {
"uint64",
'M'}, {
"int64",
'L'}, {
"single",
'd'}, {
"double",
'D'},
1633 {
"char",
'C'}, {
"byte",
'B'}
1636 string_t
key =
"_ArrayType_";
1637 auto it = bjdtype.find(
static_cast<string_t
>(value.at(
key)));
1638 if (it == bjdtype.end())
1642 CharType dtype = it->second;
1644 key =
"_ArraySize_";
1645 std::size_t len = (value.at(
key).empty() ? 0 : 1);
1646 for (
const auto& el : value.at(
key))
1648 len *=
static_cast<std::size_t
>(el.m_data.m_value.number_unsigned);
1651 key =
"_ArrayData_";
1652 if (value.at(
key).size() != len)
1657 oa->write_character(
'[');
1658 oa->write_character(
'$');
1659 oa->write_character(dtype);
1660 oa->write_character(
'#');
1662 key =
"_ArraySize_";
1663 write_ubjson(value.at(
key), use_count, use_type,
true,
true, bjdata_version);
1665 key =
"_ArrayData_";
1666 if (dtype ==
'U' || dtype ==
'C' || dtype ==
'B')
1668 for (
const auto& el : value.at(
key))
1670 write_number(
static_cast<std::uint8_t
>(el.m_data.m_value.number_unsigned),
true);
1673 else if (dtype ==
'i')
1675 for (
const auto& el : value.at(
key))
1677 write_number(
static_cast<std::int8_t
>(el.m_data.m_value.number_integer),
true);
1680 else if (dtype ==
'u')
1682 for (
const auto& el : value.at(
key))
1684 write_number(
static_cast<std::uint16_t
>(el.m_data.m_value.number_unsigned),
true);
1687 else if (dtype ==
'I')
1689 for (
const auto& el : value.at(
key))
1691 write_number(
static_cast<std::int16_t
>(el.m_data.m_value.number_integer),
true);
1694 else if (dtype ==
'm')
1696 for (
const auto& el : value.at(
key))
1698 write_number(
static_cast<std::uint32_t
>(el.m_data.m_value.number_unsigned),
true);
1701 else if (dtype ==
'l')
1703 for (
const auto& el : value.at(
key))
1705 write_number(
static_cast<std::int32_t
>(el.m_data.m_value.number_integer),
true);
1708 else if (dtype ==
'M')
1710 for (
const auto& el : value.at(
key))
1712 write_number(
static_cast<std::uint64_t
>(el.m_data.m_value.number_unsigned),
true);
1715 else if (dtype ==
'L')
1717 for (
const auto& el : value.at(
key))
1719 write_number(
static_cast<std::int64_t
>(el.m_data.m_value.number_integer),
true);
1722 else if (dtype ==
'd')
1724 for (
const auto& el : value.at(
key))
1726 write_number(
static_cast<float>(el.m_data.m_value.number_float),
true);
1729 else if (dtype ==
'D')
1731 for (
const auto& el : value.at(
key))
1733 write_number(
static_cast<double>(el.m_data.m_value.number_float),
true);
1756 template<
typename NumberType>
1757 void write_number(
const NumberType n,
const bool OutputIsLittleEndian =
false)
1760 std::array<CharType,
sizeof(NumberType)> vec{};
1761 std::memcpy(vec.data(), &n,
sizeof(NumberType));
1764 if (is_little_endian != OutputIsLittleEndian)
1767 std::reverse(vec.begin(), vec.end());
1770 oa->write_characters(vec.data(),
sizeof(NumberType));
1776#pragma GCC diagnostic push
1777#pragma GCC diagnostic ignored "-Wfloat-equal"
1779 if (!std::isfinite(n) || ((
static_cast<double>(n) >=
static_cast<double>(std::numeric_limits<float>::lowest()) &&
1780 static_cast<double>(n) <=
static_cast<double>((std::numeric_limits<float>::max)()) &&
1781 static_cast<double>(
static_cast<float>(n)) ==
static_cast<double>(n))))
1783 oa->write_character(format == detail::input_format_t::cbor
1784 ? get_cbor_float_prefix(
static_cast<float>(n))
1785 : get_msgpack_float_prefix(
static_cast<float>(n)));
1786 write_number(
static_cast<float>(n));
1790 oa->write_character(format == detail::input_format_t::cbor
1791 ? get_cbor_float_prefix(n)
1792 : get_msgpack_float_prefix(n));
1796#pragma GCC diagnostic pop
1805 template <
typename C = CharType,
1806 enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * =
nullptr >
1807 static constexpr CharType to_char_type(std::uint8_t x)
noexcept
1809 return *
reinterpret_cast<char*
>(&x);
1812 template <
typename C = CharType,
1813 enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * =
nullptr >
1814 static CharType to_char_type(std::uint8_t x)
noexcept
1823#ifdef JSON_HAS_CPP_26
1824 static_assert(std::is_trivially_copyable<CharType>::value,
"CharType must be trivially copyable");
1825 static_assert(std::is_trivially_default_constructible<CharType>::value,
"CharType must be trivially default constructible");
1827 static_assert(std::is_trivial<CharType>::value,
"CharType must be trivial");
1830 static_assert(
sizeof(std::uint8_t) ==
sizeof(CharType),
"size of CharType must be equal to std::uint8_t");
1832 std::memcpy(&result, &x,
sizeof(x));
1836 template<
typename C = CharType,
1837 enable_if_t<std::is_unsigned<C>::value>* =
nullptr>
1838 static constexpr CharType to_char_type(std::uint8_t x)
noexcept
1843 template <
typename InputCharType,
typename C = CharType,
1845 std::is_signed<C>::value &&
1846 std::is_signed<char>::value &&
1847 std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
1849 static constexpr CharType to_char_type(InputCharType x)
noexcept