TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ write_cbor()

template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_cbor ( const BasicJsonType &  j)
inline
Parameters
[in]jJSON value to serialize

Definition at line 13846 of file json.hpp.

13847 {
13848 switch (j.type())
13849 {
13850 case value_t::null:
13851 {
13852 oa->write_character(to_char_type(0xF6));
13853 break;
13854 }
13855
13856 case value_t::boolean:
13857 {
13858 oa->write_character(j.m_value.boolean
13859 ? to_char_type(0xF5)
13860 : to_char_type(0xF4));
13861 break;
13862 }
13863
13865 {
13866 if (j.m_value.number_integer >= 0)
13867 {
13868 // CBOR does not differentiate between positive signed
13869 // integers and unsigned integers. Therefore, we used the
13870 // code from the value_t::number_unsigned case here.
13871 if (j.m_value.number_integer <= 0x17)
13872 {
13873 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13874 }
13875
13876 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
13877 {
13878 oa->write_character(to_char_type(0x18));
13879 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13880 }
13881
13882 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
13883 {
13884 oa->write_character(to_char_type(0x19));
13885 write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
13886 }
13887
13888 else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
13889 {
13890 oa->write_character(to_char_type(0x1A));
13891 write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
13892 }
13893
13894 else
13895 {
13896 oa->write_character(to_char_type(0x1B));
13897 write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
13898 }
13899 }
13900
13901 else
13902 {
13903 // The conversions below encode the sign in the first
13904 // byte, and the value is converted to a positive number.
13905 const auto positive_number = -1 - j.m_value.number_integer;
13906
13907 if (j.m_value.number_integer >= -24)
13908 {
13909 write_number(static_cast<std::uint8_t>(0x20 + positive_number));
13910 }
13911
13912 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
13913 {
13914 oa->write_character(to_char_type(0x38));
13915 write_number(static_cast<std::uint8_t>(positive_number));
13916 }
13917
13918 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
13919 {
13920 oa->write_character(to_char_type(0x39));
13921 write_number(static_cast<std::uint16_t>(positive_number));
13922 }
13923
13924 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
13925 {
13926 oa->write_character(to_char_type(0x3A));
13927 write_number(static_cast<std::uint32_t>(positive_number));
13928 }
13929
13930 else
13931 {
13932 oa->write_character(to_char_type(0x3B));
13933 write_number(static_cast<std::uint64_t>(positive_number));
13934 }
13935 }
13936
13937 break;
13938 }
13939
13941 {
13942 if (j.m_value.number_unsigned <= 0x17)
13943 {
13944 write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
13945 }
13946
13947 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
13948 {
13949 oa->write_character(to_char_type(0x18));
13950 write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
13951 }
13952
13953 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
13954 {
13955 oa->write_character(to_char_type(0x19));
13956 write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
13957 }
13958
13959 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
13960 {
13961 oa->write_character(to_char_type(0x1A));
13962 write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
13963 }
13964
13965 else
13966 {
13967 oa->write_character(to_char_type(0x1B));
13968 write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
13969 }
13970
13971 break;
13972 }
13973
13975 {
13976 if (std::isnan(j.m_value.number_float))
13977 {
13978 // NaN is 0xf97e00 in CBOR
13979 oa->write_character(to_char_type(0xF9));
13980 oa->write_character(to_char_type(0x7E));
13981 oa->write_character(to_char_type(0x00));
13982 }
13983
13984 else if (std::isinf(j.m_value.number_float))
13985 {
13986 // Infinity is 0xf97c00, -Infinity is 0xf9fc00
13987 oa->write_character(to_char_type(0xf9));
13988 oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
13989 oa->write_character(to_char_type(0x00));
13990 }
13991
13992 else
13993 {
13994 write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
13995 }
13996
13997 break;
13998 }
13999
14000 case value_t::string:
14001 {
14002 // step 1: write control byte and the string length
14003 const auto N = j.m_value.string->size();
14004
14005 if (N <= 0x17)
14006 {
14007 write_number(static_cast<std::uint8_t>(0x60 + N));
14008 }
14009
14010 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
14011 {
14012 oa->write_character(to_char_type(0x78));
14013 write_number(static_cast<std::uint8_t>(N));
14014 }
14015
14016 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14017 {
14018 oa->write_character(to_char_type(0x79));
14019 write_number(static_cast<std::uint16_t>(N));
14020 }
14021
14022 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14023 {
14024 oa->write_character(to_char_type(0x7A));
14025 write_number(static_cast<std::uint32_t>(N));
14026 }
14027
14028 // LCOV_EXCL_START
14029 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
14030 {
14031 oa->write_character(to_char_type(0x7B));
14032 write_number(static_cast<std::uint64_t>(N));
14033 }
14034
14035 // LCOV_EXCL_STOP
14036 // step 2: write the string
14037 oa->write_characters(
14038 reinterpret_cast<const CharType *>(j.m_value.string->c_str()),
14039 j.m_value.string->size());
14040 break;
14041 }
14042
14043 case value_t::array:
14044 {
14045 // step 1: write control byte and the array size
14046 const auto N = j.m_value.array->size();
14047
14048 if (N <= 0x17)
14049 {
14050 write_number(static_cast<std::uint8_t>(0x80 + N));
14051 }
14052
14053 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
14054 {
14055 oa->write_character(to_char_type(0x98));
14056 write_number(static_cast<std::uint8_t>(N));
14057 }
14058
14059 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14060 {
14061 oa->write_character(to_char_type(0x99));
14062 write_number(static_cast<std::uint16_t>(N));
14063 }
14064
14065 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14066 {
14067 oa->write_character(to_char_type(0x9A));
14068 write_number(static_cast<std::uint32_t>(N));
14069 }
14070
14071 // LCOV_EXCL_START
14072 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
14073 {
14074 oa->write_character(to_char_type(0x9B));
14075 write_number(static_cast<std::uint64_t>(N));
14076 }
14077
14078 // LCOV_EXCL_STOP
14079
14080 // step 2: write each element
14081 for (const auto &el : *j.m_value.array)
14082 {
14083 write_cbor(el);
14084 }
14085
14086 break;
14087 }
14088
14089 case value_t::binary:
14090 {
14091 if (j.m_value.binary->has_subtype())
14092 {
14093 if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
14094 {
14095 write_number(static_cast<std::uint8_t>(0xd8));
14096 write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
14097 }
14098
14099 else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
14100 {
14101 write_number(static_cast<std::uint8_t>(0xd9));
14102 write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
14103 }
14104
14105 else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
14106 {
14107 write_number(static_cast<std::uint8_t>(0xda));
14108 write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
14109 }
14110
14111 else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
14112 {
14113 write_number(static_cast<std::uint8_t>(0xdb));
14114 write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
14115 }
14116 }
14117
14118 // step 1: write control byte and the binary array size
14119 const auto N = j.m_value.binary->size();
14120
14121 if (N <= 0x17)
14122 {
14123 write_number(static_cast<std::uint8_t>(0x40 + N));
14124 }
14125
14126 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
14127 {
14128 oa->write_character(to_char_type(0x58));
14129 write_number(static_cast<std::uint8_t>(N));
14130 }
14131
14132 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14133 {
14134 oa->write_character(to_char_type(0x59));
14135 write_number(static_cast<std::uint16_t>(N));
14136 }
14137
14138 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14139 {
14140 oa->write_character(to_char_type(0x5A));
14141 write_number(static_cast<std::uint32_t>(N));
14142 }
14143
14144 // LCOV_EXCL_START
14145 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
14146 {
14147 oa->write_character(to_char_type(0x5B));
14148 write_number(static_cast<std::uint64_t>(N));
14149 }
14150
14151 // LCOV_EXCL_STOP
14152 // step 2: write each element
14153 oa->write_characters(
14154 reinterpret_cast<const CharType *>(j.m_value.binary->data()),
14155 N);
14156 break;
14157 }
14158
14159 case value_t::object:
14160 {
14161 // step 1: write control byte and the object size
14162 const auto N = j.m_value.object->size();
14163
14164 if (N <= 0x17)
14165 {
14166 write_number(static_cast<std::uint8_t>(0xA0 + N));
14167 }
14168
14169 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
14170 {
14171 oa->write_character(to_char_type(0xB8));
14172 write_number(static_cast<std::uint8_t>(N));
14173 }
14174
14175 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14176 {
14177 oa->write_character(to_char_type(0xB9));
14178 write_number(static_cast<std::uint16_t>(N));
14179 }
14180
14181 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14182 {
14183 oa->write_character(to_char_type(0xBA));
14184 write_number(static_cast<std::uint32_t>(N));
14185 }
14186
14187 // LCOV_EXCL_START
14188 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
14189 {
14190 oa->write_character(to_char_type(0xBB));
14191 write_number(static_cast<std::uint64_t>(N));
14192 }
14193
14194 // LCOV_EXCL_STOP
14195
14196 // step 2: write each element
14197 for (const auto &el : *j.m_value.object)
14198 {
14199 write_cbor(el.first);
14200 write_cbor(el.second);
14201 }
14202
14203 break;
14204 }
14205
14206 case value_t::discarded:
14207 default:
14208 break;
14209 }
14210 }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:15519
void write_cbor(const BasicJsonType &j)
Definition: json.hpp:13846
@ 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)