TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ parse_cbor_internal()

template<typename BasicJsonType , typename InputAdapterType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, InputAdapterType, SAX >::parse_cbor_internal ( const bool  get_char,
const cbor_tag_handler_t  tag_handler 
)
inlineprivate
Parameters
[in]get_charwhether a new character should be retrieved from the input (true) or whether the last read character should be considered instead (false)
[in]tag_handlerhow CBOR tags should be treated
Returns
whether a valid CBOR value was passed to the SAX parser

Definition at line 8794 of file json.hpp.

8796 {
8797 switch (get_char ? get() : current)
8798 {
8799 // EOF
8800 case std::char_traits<char_type>::eof():
8801 return unexpect_eof(input_format_t::cbor, "value");
8802
8803 // Integer 0x00..0x17 (0..23)
8804 case 0x00:
8805 case 0x01:
8806 case 0x02:
8807 case 0x03:
8808 case 0x04:
8809 case 0x05:
8810 case 0x06:
8811 case 0x07:
8812 case 0x08:
8813 case 0x09:
8814 case 0x0A:
8815 case 0x0B:
8816 case 0x0C:
8817 case 0x0D:
8818 case 0x0E:
8819 case 0x0F:
8820 case 0x10:
8821 case 0x11:
8822 case 0x12:
8823 case 0x13:
8824 case 0x14:
8825 case 0x15:
8826 case 0x16:
8827 case 0x17:
8828 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
8829
8830 case 0x18: // Unsigned integer (one-byte uint8_t follows)
8831 {
8832 std::uint8_t number{};
8833 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8834 }
8835
8836 case 0x19: // Unsigned integer (two-byte uint16_t follows)
8837 {
8838 std::uint16_t number{};
8839 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8840 }
8841
8842 case 0x1A: // Unsigned integer (four-byte uint32_t follows)
8843 {
8844 std::uint32_t number{};
8845 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8846 }
8847
8848 case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
8849 {
8850 std::uint64_t number{};
8851 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8852 }
8853
8854 // Negative integer -1-0x00..-1-0x17 (-1..-24)
8855 case 0x20:
8856 case 0x21:
8857 case 0x22:
8858 case 0x23:
8859 case 0x24:
8860 case 0x25:
8861 case 0x26:
8862 case 0x27:
8863 case 0x28:
8864 case 0x29:
8865 case 0x2A:
8866 case 0x2B:
8867 case 0x2C:
8868 case 0x2D:
8869 case 0x2E:
8870 case 0x2F:
8871 case 0x30:
8872 case 0x31:
8873 case 0x32:
8874 case 0x33:
8875 case 0x34:
8876 case 0x35:
8877 case 0x36:
8878 case 0x37:
8879 return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
8880
8881 case 0x38: // Negative integer (one-byte uint8_t follows)
8882 {
8883 std::uint8_t number{};
8884 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8885 }
8886
8887 case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
8888 {
8889 std::uint16_t number{};
8890 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8891 }
8892
8893 case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
8894 {
8895 std::uint32_t number{};
8896 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8897 }
8898
8899 case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
8900 {
8901 std::uint64_t number{};
8902 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
8903 - static_cast<number_integer_t>(number));
8904 }
8905
8906 // Binary data (0x00..0x17 bytes follow)
8907 case 0x40:
8908 case 0x41:
8909 case 0x42:
8910 case 0x43:
8911 case 0x44:
8912 case 0x45:
8913 case 0x46:
8914 case 0x47:
8915 case 0x48:
8916 case 0x49:
8917 case 0x4A:
8918 case 0x4B:
8919 case 0x4C:
8920 case 0x4D:
8921 case 0x4E:
8922 case 0x4F:
8923 case 0x50:
8924 case 0x51:
8925 case 0x52:
8926 case 0x53:
8927 case 0x54:
8928 case 0x55:
8929 case 0x56:
8930 case 0x57:
8931 case 0x58: // Binary data (one-byte uint8_t for n follows)
8932 case 0x59: // Binary data (two-byte uint16_t for n follow)
8933 case 0x5A: // Binary data (four-byte uint32_t for n follow)
8934 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
8935 case 0x5F: // Binary data (indefinite length)
8936 {
8937 binary_t b;
8938 return get_cbor_binary(b) && sax->binary(b);
8939 }
8940
8941 // UTF-8 string (0x00..0x17 bytes follow)
8942 case 0x60:
8943 case 0x61:
8944 case 0x62:
8945 case 0x63:
8946 case 0x64:
8947 case 0x65:
8948 case 0x66:
8949 case 0x67:
8950 case 0x68:
8951 case 0x69:
8952 case 0x6A:
8953 case 0x6B:
8954 case 0x6C:
8955 case 0x6D:
8956 case 0x6E:
8957 case 0x6F:
8958 case 0x70:
8959 case 0x71:
8960 case 0x72:
8961 case 0x73:
8962 case 0x74:
8963 case 0x75:
8964 case 0x76:
8965 case 0x77:
8966 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
8967 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
8968 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
8969 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
8970 case 0x7F: // UTF-8 string (indefinite length)
8971 {
8972 string_t s;
8973 return get_cbor_string(s) && sax->string(s);
8974 }
8975
8976 // array (0x00..0x17 data items follow)
8977 case 0x80:
8978 case 0x81:
8979 case 0x82:
8980 case 0x83:
8981 case 0x84:
8982 case 0x85:
8983 case 0x86:
8984 case 0x87:
8985 case 0x88:
8986 case 0x89:
8987 case 0x8A:
8988 case 0x8B:
8989 case 0x8C:
8990 case 0x8D:
8991 case 0x8E:
8992 case 0x8F:
8993 case 0x90:
8994 case 0x91:
8995 case 0x92:
8996 case 0x93:
8997 case 0x94:
8998 case 0x95:
8999 case 0x96:
9000 case 0x97:
9001 return get_cbor_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
9002
9003 case 0x98: // array (one-byte uint8_t for n follows)
9004 {
9005 std::uint8_t len{};
9006 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
9007 }
9008
9009 case 0x99: // array (two-byte uint16_t for n follow)
9010 {
9011 std::uint16_t len{};
9012 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
9013 }
9014
9015 case 0x9A: // array (four-byte uint32_t for n follow)
9016 {
9017 std::uint32_t len{};
9018 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
9019 }
9020
9021 case 0x9B: // array (eight-byte uint64_t for n follow)
9022 {
9023 std::uint64_t len{};
9024 return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast<std::size_t>(len), tag_handler);
9025 }
9026
9027 case 0x9F: // array (indefinite length)
9028 return get_cbor_array(std::size_t(-1), tag_handler);
9029
9030 // map (0x00..0x17 pairs of data items follow)
9031 case 0xA0:
9032 case 0xA1:
9033 case 0xA2:
9034 case 0xA3:
9035 case 0xA4:
9036 case 0xA5:
9037 case 0xA6:
9038 case 0xA7:
9039 case 0xA8:
9040 case 0xA9:
9041 case 0xAA:
9042 case 0xAB:
9043 case 0xAC:
9044 case 0xAD:
9045 case 0xAE:
9046 case 0xAF:
9047 case 0xB0:
9048 case 0xB1:
9049 case 0xB2:
9050 case 0xB3:
9051 case 0xB4:
9052 case 0xB5:
9053 case 0xB6:
9054 case 0xB7:
9055 return get_cbor_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
9056
9057 case 0xB8: // map (one-byte uint8_t for n follows)
9058 {
9059 std::uint8_t len{};
9060 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
9061 }
9062
9063 case 0xB9: // map (two-byte uint16_t for n follow)
9064 {
9065 std::uint16_t len{};
9066 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
9067 }
9068
9069 case 0xBA: // map (four-byte uint32_t for n follow)
9070 {
9071 std::uint32_t len{};
9072 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
9073 }
9074
9075 case 0xBB: // map (eight-byte uint64_t for n follow)
9076 {
9077 std::uint64_t len{};
9078 return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast<std::size_t>(len), tag_handler);
9079 }
9080
9081 case 0xBF: // map (indefinite length)
9082 return get_cbor_object(std::size_t(-1), tag_handler);
9083
9084 case 0xC6: // tagged item
9085 case 0xC7:
9086 case 0xC8:
9087 case 0xC9:
9088 case 0xCA:
9089 case 0xCB:
9090 case 0xCC:
9091 case 0xCD:
9092 case 0xCE:
9093 case 0xCF:
9094 case 0xD0:
9095 case 0xD1:
9096 case 0xD2:
9097 case 0xD3:
9098 case 0xD4:
9099 case 0xD8: // tagged item (1 bytes follow)
9100 case 0xD9: // tagged item (2 bytes follow)
9101 case 0xDA: // tagged item (4 bytes follow)
9102 case 0xDB: // tagged item (8 bytes follow)
9103 {
9104 switch (tag_handler)
9105 {
9107 {
9108 auto last_token = get_token_string();
9109 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
9110 }
9111
9113 {
9114 // ignore binary subtype
9115 switch (current)
9116 {
9117 case 0xD8:
9118 {
9119 std::uint8_t subtype_to_ignore{};
9120 get_number(input_format_t::cbor, subtype_to_ignore);
9121 break;
9122 }
9123
9124 case 0xD9:
9125 {
9126 std::uint16_t subtype_to_ignore{};
9127 get_number(input_format_t::cbor, subtype_to_ignore);
9128 break;
9129 }
9130
9131 case 0xDA:
9132 {
9133 std::uint32_t subtype_to_ignore{};
9134 get_number(input_format_t::cbor, subtype_to_ignore);
9135 break;
9136 }
9137
9138 case 0xDB:
9139 {
9140 std::uint64_t subtype_to_ignore{};
9141 get_number(input_format_t::cbor, subtype_to_ignore);
9142 break;
9143 }
9144
9145 default:
9146 break;
9147 }
9148
9149 return parse_cbor_internal(true, tag_handler);
9150 }
9151
9153 {
9154 binary_t b;
9155
9156 // use binary subtype and store in binary container
9157 switch (current)
9158 {
9159 case 0xD8:
9160 {
9161 std::uint8_t subtype{};
9162 get_number(input_format_t::cbor, subtype);
9163 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
9164 break;
9165 }
9166
9167 case 0xD9:
9168 {
9169 std::uint16_t subtype{};
9170 get_number(input_format_t::cbor, subtype);
9171 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
9172 break;
9173 }
9174
9175 case 0xDA:
9176 {
9177 std::uint32_t subtype{};
9178 get_number(input_format_t::cbor, subtype);
9179 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
9180 break;
9181 }
9182
9183 case 0xDB:
9184 {
9185 std::uint64_t subtype{};
9186 get_number(input_format_t::cbor, subtype);
9187 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
9188 break;
9189 }
9190
9191 default:
9192 return parse_cbor_internal(true, tag_handler);
9193 }
9194
9195 get();
9196 return get_cbor_binary(b) && sax->binary(b);
9197 }
9198
9199 default: // LCOV_EXCL_LINE
9200 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
9201 return false; // LCOV_EXCL_LINE
9202 }
9203 }
9204
9205 case 0xF4: // false
9206 return sax->boolean(false);
9207
9208 case 0xF5: // true
9209 return sax->boolean(true);
9210
9211 case 0xF6: // null
9212 return sax->null();
9213
9214 case 0xF9: // Half-Precision Float (two-byte IEEE 754)
9215 {
9216 const auto byte1_raw = get();
9217
9218 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
9219 {
9220 return false;
9221 }
9222
9223 const auto byte2_raw = get();
9224
9225 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
9226 {
9227 return false;
9228 }
9229
9230 const auto byte1 = static_cast<unsigned char>(byte1_raw);
9231 const auto byte2 = static_cast<unsigned char>(byte2_raw);
9232 // code from RFC 7049, Appendix D, Figure 3:
9233 // As half-precision floating-point numbers were only added
9234 // to IEEE 754 in 2008, today's programming platforms often
9235 // still only have limited support for them. It is very
9236 // easy to include at least decoding support for them even
9237 // without such support. An example of a small decoder for
9238 // half-precision floating-point numbers in the C language
9239 // is shown in Fig. 3.
9240 const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
9241 const double val = [&half]
9242 {
9243 const int exp = (half >> 10u) & 0x1Fu;
9244 const unsigned int mant = half & 0x3FFu;
9245 JSON_ASSERT(0 <= exp &&exp <= 32);
9246 JSON_ASSERT(mant <= 1024);
9247
9248 switch (exp)
9249 {
9250 case 0:
9251 return std::ldexp(mant, -24);
9252
9253 case 31:
9254 return (mant == 0)
9255 ? std::numeric_limits<double>::infinity()
9256 : std::numeric_limits<double>::quiet_NaN();
9257
9258 default:
9259 return std::ldexp(mant + 1024, exp - 25);
9260 }
9261 }();
9262 return sax->number_float((half & 0x8000u) != 0
9263 ? static_cast<number_float_t>(-val)
9264 : static_cast<number_float_t>(val), "");
9265 }
9266
9267 case 0xFA: // Single-Precision Float (four-byte IEEE 754)
9268 {
9269 float number{};
9270 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
9271 }
9272
9273 case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
9274 {
9275 double number{};
9276 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
9277 }
9278
9279 default: // anything else (0xFF is handled inside the other types)
9280 {
9281 auto last_token = get_token_string();
9282 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
9283 }
9284 }
9285 }
bool get_cbor_array(const std::size_t len, const cbor_tag_handler_t tag_handler)
Definition: json.hpp:9493
bool get_cbor_object(const std::size_t len, const cbor_tag_handler_t tag_handler)
Definition: json.hpp:9532
bool parse_cbor_internal(const bool get_char, const cbor_tag_handler_t tag_handler)
Definition: json.hpp:8794
bool get_cbor_string(string_t &result)
reads a CBOR string
Definition: json.hpp:9298
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:10914
std::string get_token_string() const
Definition: json.hpp:10928
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:10941
std::size_t chars_read
the number of characters read
Definition: json.hpp:10981
char_int_type current
the current character
Definition: json.hpp:10978
json_sax_t * sax
the SAX parser
Definition: json.hpp:10987
bool get_cbor_binary(binary_t &result)
reads a CBOR byte array
Definition: json.hpp:9396
char_int_type get()
get next character from the input
Definition: json.hpp:10769
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, const BasicJsonType &context)
create a parse error exception
Definition: json.hpp:2800
@ store
store tags as binary type
@ error
throw a parse_error exception in case of a tag
input_format_t
the supported input formats
Definition: json.hpp:5382
STL namespace.