Merge branch 'develop' of https://github.com/nlohmann/json into feature/bson
Conflicts: include/nlohmann/detail/input/binary_reader.hpp single_include/nlohmann/json.hpp src/unit-bson.cpp
This commit is contained in:
commit
2a63869159
110 changed files with 2497 additions and 1418 deletions
|
|
@ -104,7 +104,8 @@ class binary_reader
|
|||
|
||||
if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
|
||||
{
|
||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, "expected end of input"));
|
||||
return sax->parse_error(chars_read, get_token_string(),
|
||||
parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -139,7 +140,7 @@ class binary_reader
|
|||
while (true)
|
||||
{
|
||||
get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -165,7 +166,7 @@ class binary_reader
|
|||
template <typename NumberType>
|
||||
bool get_bson_string(const NumberType len, string_t& result)
|
||||
{
|
||||
return get_string(len - static_cast<NumberType>(1), result)
|
||||
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result)
|
||||
&& get() != std::char_traits<char>::eof();
|
||||
}
|
||||
|
||||
|
|
@ -195,14 +196,14 @@ class binary_reader
|
|||
case 0x01: // double
|
||||
{
|
||||
double number;
|
||||
return get_number<double, true>(number)
|
||||
return get_number<double, true>(input_format_t::bson, number)
|
||||
&& sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
case 0x02: // string
|
||||
{
|
||||
std::int32_t len;
|
||||
string_t value;
|
||||
return get_number<std::int32_t, true>(len)
|
||||
return get_number<std::int32_t, true>(input_format_t::bson, len)
|
||||
&& get_bson_string(len, value)
|
||||
&& sax->string(value);
|
||||
}
|
||||
|
|
@ -213,13 +214,13 @@ class binary_reader
|
|||
case 0x10: // int32
|
||||
{
|
||||
std::int32_t value;
|
||||
return get_number<std::int32_t, true>(value)
|
||||
return get_number<std::int32_t, true>(input_format_t::bson, value)
|
||||
&& sax->number_integer(static_cast<std::int32_t>(value));
|
||||
}
|
||||
case 0x12: // int64
|
||||
{
|
||||
std::int64_t value;
|
||||
return get_number<std::int64_t, true>(value)
|
||||
return get_number<std::int64_t, true>(input_format_t::bson, value)
|
||||
&& sax->number_integer(static_cast<std::int64_t>(value));
|
||||
}
|
||||
case 0x0A: // null
|
||||
|
|
@ -256,7 +257,7 @@ class binary_reader
|
|||
{
|
||||
while (auto element_type = get())
|
||||
{
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -288,7 +289,7 @@ class binary_reader
|
|||
bool parse_bson_array()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number<std::int32_t, true>(documentSize);
|
||||
get_number<std::int32_t, true>(input_format_t::bson, documentSize);
|
||||
|
||||
if (JSON_UNLIKELY(not sax->start_array(-1)))
|
||||
{
|
||||
|
|
@ -310,7 +311,7 @@ class binary_reader
|
|||
bool parse_bson_internal()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number<std::int32_t, true>(documentSize);
|
||||
get_number<std::int32_t, true>(input_format_t::bson, documentSize);
|
||||
|
||||
if (JSON_UNLIKELY(not sax->start_object(-1)))
|
||||
{
|
||||
|
|
@ -338,7 +339,7 @@ class binary_reader
|
|||
{
|
||||
// EOF
|
||||
case std::char_traits<char>::eof():
|
||||
return unexpect_eof();
|
||||
return unexpect_eof(input_format_t::cbor, "value");
|
||||
|
||||
// Integer 0x00..0x17 (0..23)
|
||||
case 0x00:
|
||||
|
|
@ -370,25 +371,25 @@ class binary_reader
|
|||
case 0x18: // Unsigned integer (one-byte uint8_t follows)
|
||||
{
|
||||
uint8_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0x19: // Unsigned integer (two-byte uint16_t follows)
|
||||
{
|
||||
uint16_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0x1A: // Unsigned integer (four-byte uint32_t follows)
|
||||
{
|
||||
uint32_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
|
||||
{
|
||||
uint64_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
// Negative integer -1-0x00..-1-0x17 (-1..-24)
|
||||
|
|
@ -421,25 +422,25 @@ class binary_reader
|
|||
case 0x38: // Negative integer (one-byte uint8_t follows)
|
||||
{
|
||||
uint8_t number;
|
||||
return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
}
|
||||
|
||||
case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
|
||||
{
|
||||
uint16_t number;
|
||||
return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
}
|
||||
|
||||
case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
|
||||
{
|
||||
uint32_t number;
|
||||
return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
|
||||
}
|
||||
|
||||
case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
|
||||
{
|
||||
uint64_t number;
|
||||
return get_number(number) and sax->number_integer(static_cast<number_integer_t>(-1)
|
||||
return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
|
||||
- static_cast<number_integer_t>(number));
|
||||
}
|
||||
|
||||
|
|
@ -508,25 +509,25 @@ class binary_reader
|
|||
case 0x98: // array (one-byte uint8_t for n follows)
|
||||
{
|
||||
uint8_t len;
|
||||
return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0x99: // array (two-byte uint16_t for n follow)
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||
{
|
||||
uint64_t len;
|
||||
return get_number(len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0x9F: // array (indefinite length)
|
||||
|
|
@ -562,25 +563,25 @@ class binary_reader
|
|||
case 0xB8: // map (one-byte uint8_t for n follows)
|
||||
{
|
||||
uint8_t len;
|
||||
return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xB9: // map (two-byte uint16_t for n follow)
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||
{
|
||||
uint64_t len;
|
||||
return get_number(len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xBF: // map (indefinite length)
|
||||
|
|
@ -597,17 +598,20 @@ class binary_reader
|
|||
|
||||
case 0xF9: // Half-Precision Float (two-byte IEEE 754)
|
||||
{
|
||||
const int byte1 = get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
const int byte1_raw = get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const int byte2 = get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
const int byte2_raw = get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||
const auto byte2 = static_cast<unsigned char>(byte2_raw);
|
||||
|
||||
// code from RFC 7049, Appendix D, Figure 3:
|
||||
// As half-precision floating-point numbers were only added
|
||||
// to IEEE 754 in 2008, today's programming platforms often
|
||||
|
|
@ -643,19 +647,19 @@ class binary_reader
|
|||
case 0xFA: // Single-Precision Float (four-byte IEEE 754)
|
||||
{
|
||||
float number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
|
||||
{
|
||||
double number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
default: // anything else (0xFF is handled inside the other types)
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading CBOR; last byte: 0x" + last_token));
|
||||
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")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -669,7 +673,7 @@ class binary_reader
|
|||
{
|
||||
// EOF
|
||||
case std::char_traits<char>::eof():
|
||||
return unexpect_eof();
|
||||
return unexpect_eof(input_format_t::msgpack, "value");
|
||||
|
||||
// positive fixint
|
||||
case 0x00:
|
||||
|
|
@ -890,61 +894,61 @@ class binary_reader
|
|||
case 0xCA: // float 32
|
||||
{
|
||||
float number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
case 0xCB: // float 64
|
||||
{
|
||||
double number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
case 0xCC: // uint 8
|
||||
{
|
||||
uint8_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0xCD: // uint 16
|
||||
{
|
||||
uint16_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0xCE: // uint 32
|
||||
{
|
||||
uint32_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0xCF: // uint 64
|
||||
{
|
||||
uint64_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 0xD0: // int 8
|
||||
{
|
||||
int8_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 0xD1: // int 16
|
||||
{
|
||||
int16_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 0xD2: // int 32
|
||||
{
|
||||
int32_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 0xD3: // int 64
|
||||
{
|
||||
int64_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 0xD9: // str 8
|
||||
|
|
@ -958,25 +962,25 @@ class binary_reader
|
|||
case 0xDC: // array 16
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xDD: // array 32
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_msgpack_array(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xDE: // map 16
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
case 0xDF: // map 32
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_msgpack_object(static_cast<std::size_t>(len));
|
||||
return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
|
||||
}
|
||||
|
||||
// negative fixint
|
||||
|
|
@ -1017,7 +1021,7 @@ class binary_reader
|
|||
default: // anything else
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading MessagePack; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1067,6 +1071,7 @@ class binary_reader
|
|||
@brief read a number from the input
|
||||
|
||||
@tparam NumberType the type of the number
|
||||
@param[in] format the current format (for diagnostics)
|
||||
@param[out] result number of type @a NumberType
|
||||
|
||||
@return whether conversion completed
|
||||
|
|
@ -1076,14 +1081,14 @@ class binary_reader
|
|||
(big endian) and therefore need reordering on little endian systems.
|
||||
*/
|
||||
template<typename NumberType, bool InputIsLittleEndian = false>
|
||||
bool get_number(NumberType& result)
|
||||
bool get_number(const input_format_t format, NumberType& result)
|
||||
{
|
||||
// step 1: read input into array with system's byte order
|
||||
std::array<uint8_t, sizeof(NumberType)> vec;
|
||||
for (std::size_t i = 0; i < sizeof(NumberType); ++i)
|
||||
{
|
||||
get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(format, "number")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1109,8 +1114,9 @@ class binary_reader
|
|||
@brief create a string by reading characters from the input
|
||||
|
||||
@tparam NumberType the type of the number
|
||||
@param[in] format the current format (for diagnostics)
|
||||
@param[in] len number of characters to read
|
||||
@param[out] string created by reading @a len bytes
|
||||
@param[out] result string created by reading @a len bytes
|
||||
|
||||
@return whether string creation completed
|
||||
|
||||
|
|
@ -1119,13 +1125,13 @@ class binary_reader
|
|||
the input before we run out of string memory.
|
||||
*/
|
||||
template<typename NumberType>
|
||||
bool get_string(const NumberType len, string_t& result)
|
||||
bool get_string(const input_format_t format, const NumberType len, string_t& result)
|
||||
{
|
||||
bool success = true;
|
||||
std::generate_n(std::back_inserter(result), len, [this, &success]()
|
||||
std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
|
||||
{
|
||||
get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(format, "string")))
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
|
@ -1147,7 +1153,7 @@ class binary_reader
|
|||
*/
|
||||
bool get_cbor_string(string_t& result)
|
||||
{
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1180,31 +1186,31 @@ class binary_reader
|
|||
case 0x76:
|
||||
case 0x77:
|
||||
{
|
||||
return get_string(current & 0x1F, result);
|
||||
return get_string(input_format_t::cbor, current & 0x1F, result);
|
||||
}
|
||||
|
||||
case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
|
||||
{
|
||||
uint8_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
|
||||
}
|
||||
|
||||
case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
|
||||
}
|
||||
|
||||
case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
|
||||
}
|
||||
|
||||
case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
|
||||
{
|
||||
uint64_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
|
||||
}
|
||||
|
||||
case 0x7F: // UTF-8 string (indefinite length)
|
||||
|
|
@ -1224,7 +1230,7 @@ class binary_reader
|
|||
default:
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a CBOR string; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1242,6 +1248,7 @@ class binary_reader
|
|||
}
|
||||
|
||||
if (len != std::size_t(-1))
|
||||
{
|
||||
for (std::size_t i = 0; i < len; ++i)
|
||||
{
|
||||
if (JSON_UNLIKELY(not parse_cbor_internal()))
|
||||
|
|
@ -1249,6 +1256,7 @@ class binary_reader
|
|||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (get() != 0xFF)
|
||||
|
|
@ -1325,7 +1333,7 @@ class binary_reader
|
|||
*/
|
||||
bool get_msgpack_string(string_t& result)
|
||||
{
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1366,31 +1374,31 @@ class binary_reader
|
|||
case 0xBE:
|
||||
case 0xBF:
|
||||
{
|
||||
return get_string(current & 0x1F, result);
|
||||
return get_string(input_format_t::msgpack, current & 0x1F, result);
|
||||
}
|
||||
|
||||
case 0xD9: // str 8
|
||||
{
|
||||
uint8_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
|
||||
}
|
||||
|
||||
case 0xDA: // str 16
|
||||
{
|
||||
uint16_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
|
||||
}
|
||||
|
||||
case 0xDB: // str 32
|
||||
{
|
||||
uint32_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a MessagePack string; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1468,7 +1476,7 @@ class binary_reader
|
|||
get(); // TODO: may we ignore N here?
|
||||
}
|
||||
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1478,36 +1486,36 @@ class binary_reader
|
|||
case 'U':
|
||||
{
|
||||
uint8_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
|
||||
}
|
||||
|
||||
case 'i':
|
||||
{
|
||||
int8_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
|
||||
}
|
||||
|
||||
case 'I':
|
||||
{
|
||||
int16_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
|
||||
}
|
||||
|
||||
case 'l':
|
||||
{
|
||||
int32_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
|
||||
}
|
||||
|
||||
case 'L':
|
||||
{
|
||||
int64_t len;
|
||||
return get_number(len) and get_string(len, result);
|
||||
return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
|
||||
}
|
||||
|
||||
default:
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "expected a UBJSON string; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1522,7 +1530,7 @@ class binary_reader
|
|||
case 'U':
|
||||
{
|
||||
uint8_t number;
|
||||
if (JSON_UNLIKELY(not get_number(number)))
|
||||
if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1533,7 +1541,7 @@ class binary_reader
|
|||
case 'i':
|
||||
{
|
||||
int8_t number;
|
||||
if (JSON_UNLIKELY(not get_number(number)))
|
||||
if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1544,7 +1552,7 @@ class binary_reader
|
|||
case 'I':
|
||||
{
|
||||
int16_t number;
|
||||
if (JSON_UNLIKELY(not get_number(number)))
|
||||
if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1555,7 +1563,7 @@ class binary_reader
|
|||
case 'l':
|
||||
{
|
||||
int32_t number;
|
||||
if (JSON_UNLIKELY(not get_number(number)))
|
||||
if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1566,7 +1574,7 @@ class binary_reader
|
|||
case 'L':
|
||||
{
|
||||
int64_t number;
|
||||
if (JSON_UNLIKELY(not get_number(number)))
|
||||
if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1577,7 +1585,7 @@ class binary_reader
|
|||
default:
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after '#' must denote a number type; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1602,7 +1610,7 @@ class binary_reader
|
|||
if (current == '$')
|
||||
{
|
||||
result.second = get(); // must not ignore 'N', because 'N' maybe the type
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1610,12 +1618,12 @@ class binary_reader
|
|||
get_ignore_noop();
|
||||
if (JSON_UNLIKELY(current != '#'))
|
||||
{
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "expected '#' after UBJSON type information; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size")));
|
||||
}
|
||||
|
||||
return get_ubjson_size_value(result.first);
|
||||
|
|
@ -1636,7 +1644,7 @@ class binary_reader
|
|||
switch (prefix)
|
||||
{
|
||||
case std::char_traits<char>::eof(): // EOF
|
||||
return unexpect_eof();
|
||||
return unexpect_eof(input_format_t::ubjson, "value");
|
||||
|
||||
case 'T': // true
|
||||
return sax->boolean(true);
|
||||
|
|
@ -1649,56 +1657,56 @@ class binary_reader
|
|||
case 'U':
|
||||
{
|
||||
uint8_t number;
|
||||
return get_number(number) and sax->number_unsigned(number);
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
|
||||
}
|
||||
|
||||
case 'i':
|
||||
{
|
||||
int8_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 'I':
|
||||
{
|
||||
int16_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 'l':
|
||||
{
|
||||
int32_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 'L':
|
||||
{
|
||||
int64_t number;
|
||||
return get_number(number) and sax->number_integer(number);
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
|
||||
}
|
||||
|
||||
case 'd':
|
||||
{
|
||||
float number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
case 'D':
|
||||
{
|
||||
double number;
|
||||
return get_number(number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
|
||||
case 'C': // char
|
||||
{
|
||||
get();
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char")))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (JSON_UNLIKELY(current > 127))
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char")));
|
||||
}
|
||||
string_t s(1, static_cast<char>(current));
|
||||
return sax->string(s);
|
||||
|
|
@ -1719,7 +1727,7 @@ class binary_reader
|
|||
default: // anything else
|
||||
{
|
||||
auto last_token = get_token_string();
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, "error reading UBJSON; last byte: 0x" + last_token));
|
||||
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1862,13 +1870,16 @@ class binary_reader
|
|||
}
|
||||
|
||||
/*!
|
||||
@param[in] format the current format (for diagnostics)
|
||||
@param[in] context further context information (for diagnostics)
|
||||
@return whether the last read character is not EOF
|
||||
*/
|
||||
bool unexpect_eof() const
|
||||
bool unexpect_eof(const input_format_t format, const char* context) const
|
||||
{
|
||||
if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
|
||||
{
|
||||
return sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, "unexpected end of input"));
|
||||
return sax->parse_error(chars_read, "<end of file>",
|
||||
parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1884,6 +1895,45 @@ class binary_reader
|
|||
}
|
||||
|
||||
private:
|
||||
/*!
|
||||
@param[in] format the current format
|
||||
@param[in] detail a detailed error message
|
||||
@param[in] context further contect information
|
||||
@return a message string to use in the parse_error exceptions
|
||||
*/
|
||||
std::string exception_message(const input_format_t format,
|
||||
const std::string& detail,
|
||||
const std::string& context) const
|
||||
{
|
||||
std::string error_msg = "syntax error while parsing ";
|
||||
|
||||
switch (format)
|
||||
{
|
||||
case input_format_t::cbor:
|
||||
error_msg += "CBOR";
|
||||
break;
|
||||
|
||||
case input_format_t::msgpack:
|
||||
error_msg += "MessagePack";
|
||||
break;
|
||||
|
||||
case input_format_t::ubjson:
|
||||
error_msg += "UBJSON";
|
||||
break;
|
||||
|
||||
case input_format_t::bson:
|
||||
error_msg += "BSON";
|
||||
break;
|
||||
|
||||
// LCOV_EXCL_START
|
||||
default:
|
||||
assert(false);
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
return error_msg + " " + context + ": " + detail;
|
||||
}
|
||||
|
||||
/// input adapter
|
||||
input_adapter_t ia = nullptr;
|
||||
|
||||
|
|
@ -1899,5 +1949,5 @@ class binary_reader
|
|||
/// the SAX parser
|
||||
json_sax_t* sax = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace nlohmann
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue