14575 {
14576 switch (j.type())
14577 {
14579 {
14580 if (add_prefix)
14581 {
14582 oa->write_character(to_char_type(
'Z'));
14583 }
14584
14585 break;
14586 }
14587
14589 {
14590 if (add_prefix)
14591 {
14592 oa->write_character(j.m_value.boolean
14593 ? to_char_type('T')
14594 : to_char_type('F'));
14595 }
14596
14597 break;
14598 }
14599
14601 {
14602 write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
14603 break;
14604 }
14605
14607 {
14608 write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
14609 break;
14610 }
14611
14613 {
14614 write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
14615 break;
14616 }
14617
14619 {
14620 if (add_prefix)
14621 {
14622 oa->write_character(to_char_type(
'S'));
14623 }
14624
14625 write_number_with_ubjson_prefix(j.m_value.string->size(), true);
14626 oa->write_characters(
14627 reinterpret_cast<const CharType *>(j.m_value.string->c_str()),
14628 j.m_value.string->size());
14629 break;
14630 }
14631
14633 {
14634 if (add_prefix)
14635 {
14636 oa->write_character(to_char_type(
'['));
14637 }
14638
14639 bool prefix_required = true;
14640
14641 if (use_type && !j.m_value.array->empty())
14642 {
14643 JSON_ASSERT(use_count);
14645 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
14646 [this, first_prefix](const BasicJsonType & v)
14647 {
14648 return ubjson_prefix(v) == first_prefix;
14649 });
14650
14651 if (same_prefix)
14652 {
14653 prefix_required = false;
14654 oa->write_character(to_char_type(
'$'));
14655 oa->write_character(first_prefix);
14656 }
14657 }
14658
14659 if (use_count)
14660 {
14661 oa->write_character(to_char_type(
'#'));
14662 write_number_with_ubjson_prefix(j.m_value.array->size(), true);
14663 }
14664
14665 for (const auto &el : *j.m_value.array)
14666 {
14667 write_ubjson(el, use_count, use_type, prefix_required);
14668 }
14669
14670 if (!use_count)
14671 {
14672 oa->write_character(to_char_type(
']'));
14673 }
14674
14675 break;
14676 }
14677
14679 {
14680 if (add_prefix)
14681 {
14682 oa->write_character(to_char_type(
'['));
14683 }
14684
14685 if (use_type && !j.m_value.binary->empty())
14686 {
14687 JSON_ASSERT(use_count);
14688 oa->write_character(to_char_type(
'$'));
14689 oa->write_character(
'U');
14690 }
14691
14692 if (use_count)
14693 {
14694 oa->write_character(to_char_type(
'#'));
14695 write_number_with_ubjson_prefix(j.m_value.binary->size(), true);
14696 }
14697
14698 if (use_type)
14699 {
14700 oa->write_characters(
14701 reinterpret_cast<const CharType *>(j.m_value.binary->data()),
14702 j.m_value.binary->size());
14703 }
14704
14705 else
14706 {
14707 for (size_t i = 0; i < j.m_value.binary->size(); ++i)
14708 {
14709 oa->write_character(to_char_type(
'U'));
14710 oa->write_character(j.m_value.binary->data()[i]);
14711 }
14712 }
14713
14714 if (!use_count)
14715 {
14716 oa->write_character(to_char_type(
']'));
14717 }
14718
14719 break;
14720 }
14721
14723 {
14724 if (add_prefix)
14725 {
14726 oa->write_character(to_char_type(
'{'));
14727 }
14728
14729 bool prefix_required = true;
14730
14731 if (use_type && !j.m_value.object->empty())
14732 {
14733 JSON_ASSERT(use_count);
14735 const bool same_prefix = std::all_of(j.begin(), j.end(),
14736 [this, first_prefix](const BasicJsonType & v)
14737 {
14738 return ubjson_prefix(v) == first_prefix;
14739 });
14740
14741 if (same_prefix)
14742 {
14743 prefix_required = false;
14744 oa->write_character(to_char_type(
'$'));
14745 oa->write_character(first_prefix);
14746 }
14747 }
14748
14749 if (use_count)
14750 {
14751 oa->write_character(to_char_type(
'#'));
14752 write_number_with_ubjson_prefix(j.m_value.object->size(), true);
14753 }
14754
14755 for (const auto &el : *j.m_value.object)
14756 {
14757 write_number_with_ubjson_prefix(el.first.size(), true);
14758 oa->write_characters(
14759 reinterpret_cast<const CharType *>(el.first.c_str()),
14760 el.first.size());
14761 write_ubjson(el.second, use_count, use_type, prefix_required);
14762 }
14763
14764 if (!use_count)
14765 {
14766 oa->write_character(to_char_type(
'}'));
14767 }
14768
14769 break;
14770 }
14771
14773 default:
14774 break;
14775 }
14776 }
void write_ubjson(const BasicJsonType &j, const bool use_count, const bool use_type, const bool add_prefix=true)
CharType ubjson_prefix(const BasicJsonType &j) const noexcept
determine the type prefix of container values
output_adapter_t< CharType > oa
the output
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)