Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
nlohmann::detail::binary_reader< BasicJsonType, SAX > Class Template Reference

deserialization of CBOR, MessagePack, and UBJSON values More...

#include <json.hpp>

Public Member Functions

 binary_reader (input_adapter_t adapter)
 create a binary reader More...
 
bool sax_parse (const input_format_t format, json_sax_t *sax_, const bool strict=true)
 

Static Public Member Functions

static constexpr bool little_endianess (int num=1) noexcept
 determine system byte order More...
 

Private Types

using number_integer_t = typename BasicJsonType::number_integer_t
 
using number_unsigned_t = typename BasicJsonType::number_unsigned_t
 
using number_float_t = typename BasicJsonType::number_float_t
 
using string_t = typename BasicJsonType::string_t
 
using json_sax_t = SAX
 

Private Member Functions

bool parse_bson_internal ()
 Reads in a BSON-object and passes it to the SAX-parser. More...
 
bool get_bson_cstr (string_t &result)
 Parses a C-style string from the BSON input. More...
 
template<typename NumberType >
bool get_bson_string (const NumberType len, string_t &result)
 Parses a zero-terminated string of length len from the BSON input. More...
 
bool parse_bson_element_internal (const int element_type, const std::size_t element_type_parse_position)
 Read a BSON document element of the given element_type. More...
 
bool parse_bson_element_list (const bool is_array)
 Read a BSON element list (as specified in the BSON-spec) More...
 
bool parse_bson_array ()
 Reads an array from the BSON input and passes it to the SAX-parser. More...
 
bool parse_cbor_internal (const bool get_char=true)
 
bool get_cbor_string (string_t &result)
 reads a CBOR string More...
 
bool get_cbor_array (const std::size_t len)
 
bool get_cbor_object (const std::size_t len)
 
bool parse_msgpack_internal ()
 
bool get_msgpack_string (string_t &result)
 reads a MessagePack string More...
 
bool get_msgpack_array (const std::size_t len)
 
bool get_msgpack_object (const std::size_t len)
 
bool parse_ubjson_internal (const bool get_char=true)
 
bool get_ubjson_string (string_t &result, const bool get_char=true)
 reads a UBJSON string More...
 
bool get_ubjson_size_value (std::size_t &result)
 
bool get_ubjson_size_type (std::pair< std::size_t, int > &result)
 determine the type and size for a container More...
 
bool get_ubjson_value (const int prefix)
 
bool get_ubjson_array ()
 
bool get_ubjson_object ()
 
int get ()
 get next character from the input More...
 
int get_ignore_noop ()
 
template<typename NumberType , bool InputIsLittleEndian = false>
bool get_number (const input_format_t format, NumberType &result)
 
template<typename NumberType >
bool get_string (const input_format_t format, const NumberType len, string_t &result)
 create a string by reading characters from the input More...
 
bool unexpect_eof (const input_format_t format, const char *context) const
 
std::string get_token_string () const
 
std::string exception_message (const input_format_t format, const std::string &detail, const std::string &context) const
 

Private Attributes

input_adapter_t ia = nullptr
 input adapter More...
 
int current = std::char_traits<char>::eof()
 the current character More...
 
std::size_t chars_read = 0
 the number of characters read More...
 
const bool is_little_endian = little_endianess()
 whether we can assume little endianess More...
 
json_sax_tsax = nullptr
 the SAX parser More...
 

Detailed Description

template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class nlohmann::detail::binary_reader< BasicJsonType, SAX >

deserialization of CBOR, MessagePack, and UBJSON values

Definition at line 6370 of file json.hpp.

Member Typedef Documentation

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
using nlohmann::detail::binary_reader< BasicJsonType, SAX >::json_sax_t = SAX
private

Definition at line 6376 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
using nlohmann::detail::binary_reader< BasicJsonType, SAX >::number_float_t = typename BasicJsonType::number_float_t
private

Definition at line 6374 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
using nlohmann::detail::binary_reader< BasicJsonType, SAX >::number_integer_t = typename BasicJsonType::number_integer_t
private

Definition at line 6372 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
using nlohmann::detail::binary_reader< BasicJsonType, SAX >::number_unsigned_t = typename BasicJsonType::number_unsigned_t
private

Definition at line 6373 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
using nlohmann::detail::binary_reader< BasicJsonType, SAX >::string_t = typename BasicJsonType::string_t
private

Definition at line 6375 of file json.hpp.

Constructor & Destructor Documentation

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
nlohmann::detail::binary_reader< BasicJsonType, SAX >::binary_reader ( input_adapter_t  adapter)
inlineexplicit

create a binary reader

Parameters
[in]adapterinput adapter to read from

Definition at line 6384 of file json.hpp.

References nlohmann::detail::void().

6384  : ia(std::move(adapter))
6385  {
6386  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
6387  assert(ia);
6388  }
input_adapter_t ia
input adapter
Definition: json.hpp:8301
j template void())
Definition: json.hpp:1424

Member Function Documentation

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
std::string nlohmann::detail::binary_reader< BasicJsonType, SAX >::exception_message ( const input_format_t  format,
const std::string detail,
const std::string context 
) const
inlineprivate
Parameters
[in]formatthe current format
[in]detaila detailed error message
[in]contextfurther contect information
Returns
a message string to use in the parse_error exceptions

Definition at line 8266 of file json.hpp.

References nlohmann::detail::bson, nlohmann::detail::cbor, nlohmann::detail::msgpack, nlohmann::detail::string, and nlohmann::detail::ubjson.

8269  {
8270  std::string error_msg = "syntax error while parsing ";
8271 
8272  switch (format)
8273  {
8274  case input_format_t::cbor:
8275  error_msg += "CBOR";
8276  break;
8277 
8279  error_msg += "MessagePack";
8280  break;
8281 
8283  error_msg += "UBJSON";
8284  break;
8285 
8286  case input_format_t::bson:
8287  error_msg += "BSON";
8288  break;
8289 
8290  // LCOV_EXCL_START
8291  default:
8292  assert(false);
8293  // LCOV_EXCL_STOP
8294  }
8295 
8296  return error_msg + " " + context + ": " + detail;
8297  }
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
int nlohmann::detail::binary_reader< BasicJsonType, SAX >::get ( )
inlineprivate

get next character from the input

This function provides the interface to the used input adapter. It does not throw in case the input reached EOF, but returns a -'ve valued std::char_traits<char>::eof() in that case.

Returns
character read from the input

Definition at line 8141 of file json.hpp.

8142  {
8143  ++chars_read;
8144  return (current = ia->get_character());
8145  }
input_adapter_t ia
input adapter
Definition: json.hpp:8301
int current
the current character
Definition: json.hpp:8304
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_bson_cstr ( string_t result)
inlineprivate

Parses a C-style string from the BSON input.

Parameters
[in,out]resultA reference to the string variable where the read string is to be stored.
Returns
true if the -byte indicating the end of the string was encountered before the EOF; false` indicates an unexpected EOF.

Definition at line 6496 of file json.hpp.

References nlohmann::detail::bson, and JSON_UNLIKELY.

6497  {
6498  auto out = std::back_inserter(result);
6499  while (true)
6500  {
6501  get();
6502  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
6503  {
6504  return false;
6505  }
6506  if (current == 0x00)
6507  {
6508  return true;
6509  }
6510  *out++ = static_cast<char>(current);
6511  }
6512 
6513  return true;
6514  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
template<typename NumberType >
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_bson_string ( const NumberType  len,
string_t result 
)
inlineprivate

Parses a zero-terminated string of length len from the BSON input.

Parameters
[in]lenThe length (including the zero-byte at the end) of the string to be read.
[in,out]resultA reference to the string variable where the read string is to be stored.
Template Parameters
NumberTypeThe type of the length len
Precondition
len >= 1
Returns
true if the string was successfully parsed

Definition at line 6528 of file json.hpp.

References nlohmann::detail::bson, nlohmann::detail::parse_error::create(), and JSON_UNLIKELY.

6529  {
6530  if (JSON_UNLIKELY(len < 1))
6531  {
6532  auto last_token = get_token_string();
6533  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string")));
6534  }
6535 
6536  return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) and get() != std::char_traits<char>::eof();
6537  }
Definition: json.hpp:20160
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
int get()
get next character from the input
Definition: json.hpp:8141
std::string get_token_string() const
Definition: json.hpp:8253
bool get_string(const input_format_t format, const NumberType len, string_t &result)
create a string by reading characters from the input
Definition: json.hpp:8218
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_cbor_array ( const std::size_t  len)
inlineprivate
Parameters
[in]lenthe length of the array or std::size_t(-1) for an array of indefinite size
Returns
whether array creation completed

Definition at line 7117 of file json.hpp.

References JSON_UNLIKELY.

7118  {
7119  if (JSON_UNLIKELY(not sax->start_array(len)))
7120  {
7121  return false;
7122  }
7123 
7124  if (len != std::size_t(-1))
7125  {
7126  for (std::size_t i = 0; i < len; ++i)
7127  {
7129  {
7130  return false;
7131  }
7132  }
7133  }
7134  else
7135  {
7136  while (get() != 0xFF)
7137  {
7138  if (JSON_UNLIKELY(not parse_cbor_internal(false)))
7139  {
7140  return false;
7141  }
7142  }
7143  }
7144 
7145  return sax->end_array();
7146  }
bool parse_cbor_internal(const bool get_char=true)
Definition: json.hpp:6686
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_cbor_object ( const std::size_t  len)
inlineprivate
Parameters
[in]lenthe length of the object or std::size_t(-1) for an object of indefinite size
Returns
whether object creation completed

Definition at line 7153 of file json.hpp.

References JSON_UNLIKELY.

7154  {
7155  if (not JSON_UNLIKELY(sax->start_object(len)))
7156  {
7157  return false;
7158  }
7159 
7160  string_t key;
7161  if (len != std::size_t(-1))
7162  {
7163  for (std::size_t i = 0; i < len; ++i)
7164  {
7165  get();
7166  if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
7167  {
7168  return false;
7169  }
7170 
7172  {
7173  return false;
7174  }
7175  key.clear();
7176  }
7177  }
7178  else
7179  {
7180  while (get() != 0xFF)
7181  {
7182  if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
7183  {
7184  return false;
7185  }
7186 
7188  {
7189  return false;
7190  }
7191  key.clear();
7192  }
7193  }
7194 
7195  return sax->end_object();
7196  }
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
bool parse_cbor_internal(const bool get_char=true)
Definition: json.hpp:6686
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_cbor_string(string_t &result)
reads a CBOR string
Definition: json.hpp:7028
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_cbor_string ( string_t result)
inlineprivate

reads a CBOR string

This function first reads starting bytes to determine the expected string length and then copies this number of bytes into a string. Additionally, CBOR's strings with indefinite lengths are supported.

Parameters
[out]resultcreated string
Returns
whether string creation completed

Definition at line 7028 of file json.hpp.

References nlohmann::detail::cbor, nlohmann::detail::parse_error::create(), and JSON_UNLIKELY.

7029  {
7030  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
7031  {
7032  return false;
7033  }
7034 
7035  switch (current)
7036  {
7037  // UTF-8 string (0x00..0x17 bytes follow)
7038  case 0x60:
7039  case 0x61:
7040  case 0x62:
7041  case 0x63:
7042  case 0x64:
7043  case 0x65:
7044  case 0x66:
7045  case 0x67:
7046  case 0x68:
7047  case 0x69:
7048  case 0x6A:
7049  case 0x6B:
7050  case 0x6C:
7051  case 0x6D:
7052  case 0x6E:
7053  case 0x6F:
7054  case 0x70:
7055  case 0x71:
7056  case 0x72:
7057  case 0x73:
7058  case 0x74:
7059  case 0x75:
7060  case 0x76:
7061  case 0x77:
7062  {
7063  return get_string(input_format_t::cbor, current & 0x1F, result);
7064  }
7065 
7066  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
7067  {
7068  uint8_t len;
7069  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7070  }
7071 
7072  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
7073  {
7074  uint16_t len;
7075  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7076  }
7077 
7078  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
7079  {
7080  uint32_t len;
7081  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7082  }
7083 
7084  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
7085  {
7086  uint64_t len;
7087  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7088  }
7089 
7090  case 0x7F: // UTF-8 string (indefinite length)
7091  {
7092  while (get() != 0xFF)
7093  {
7094  string_t chunk;
7095  if (not get_cbor_string(chunk))
7096  {
7097  return false;
7098  }
7099  result.append(chunk);
7100  }
7101  return true;
7102  }
7103 
7104  default:
7105  {
7106  auto last_token = get_token_string();
7107  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")));
7108  }
7109  }
7110  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
std::string get_token_string() const
Definition: json.hpp:8253
bool get_string(const input_format_t format, const NumberType len, string_t &result)
create a string by reading characters from the input
Definition: json.hpp:8218
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
bool get_cbor_string(string_t &result)
reads a CBOR string
Definition: json.hpp:7028
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
int nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ignore_noop ( )
inlineprivate
Returns
character read from the input after ignoring all 'N' entries

Definition at line 8150 of file json.hpp.

8151  {
8152  do
8153  {
8154  get();
8155  }
8156  while (current == 'N');
8157 
8158  return current;
8159  }
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_msgpack_array ( const std::size_t  len)
inlineprivate
Parameters
[in]lenthe length of the array
Returns
whether array creation completed

Definition at line 7650 of file json.hpp.

References JSON_UNLIKELY.

7651  {
7652  if (JSON_UNLIKELY(not sax->start_array(len)))
7653  {
7654  return false;
7655  }
7656 
7657  for (std::size_t i = 0; i < len; ++i)
7658  {
7660  {
7661  return false;
7662  }
7663  }
7664 
7665  return sax->end_array();
7666  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_msgpack_object ( const std::size_t  len)
inlineprivate
Parameters
[in]lenthe length of the object
Returns
whether object creation completed

Definition at line 7672 of file json.hpp.

References JSON_UNLIKELY.

7673  {
7674  if (JSON_UNLIKELY(not sax->start_object(len)))
7675  {
7676  return false;
7677  }
7678 
7679  string_t key;
7680  for (std::size_t i = 0; i < len; ++i)
7681  {
7682  get();
7683  if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
7684  {
7685  return false;
7686  }
7687 
7689  {
7690  return false;
7691  }
7692  key.clear();
7693  }
7694 
7695  return sax->end_object();
7696  }
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_msgpack_string(string_t &result)
reads a MessagePack string
Definition: json.hpp:7574
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_msgpack_string ( string_t result)
inlineprivate

reads a MessagePack string

This function first reads starting bytes to determine the expected string length and then copies this number of bytes into a string.

Parameters
[out]resultcreated string
Returns
whether string creation completed

Definition at line 7574 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_UNLIKELY, and nlohmann::detail::msgpack.

7575  {
7577  {
7578  return false;
7579  }
7580 
7581  switch (current)
7582  {
7583  // fixstr
7584  case 0xA0:
7585  case 0xA1:
7586  case 0xA2:
7587  case 0xA3:
7588  case 0xA4:
7589  case 0xA5:
7590  case 0xA6:
7591  case 0xA7:
7592  case 0xA8:
7593  case 0xA9:
7594  case 0xAA:
7595  case 0xAB:
7596  case 0xAC:
7597  case 0xAD:
7598  case 0xAE:
7599  case 0xAF:
7600  case 0xB0:
7601  case 0xB1:
7602  case 0xB2:
7603  case 0xB3:
7604  case 0xB4:
7605  case 0xB5:
7606  case 0xB6:
7607  case 0xB7:
7608  case 0xB8:
7609  case 0xB9:
7610  case 0xBA:
7611  case 0xBB:
7612  case 0xBC:
7613  case 0xBD:
7614  case 0xBE:
7615  case 0xBF:
7616  {
7617  return get_string(input_format_t::msgpack, current & 0x1F, result);
7618  }
7619 
7620  case 0xD9: // str 8
7621  {
7622  uint8_t len;
7623  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7624  }
7625 
7626  case 0xDA: // str 16
7627  {
7628  uint16_t len;
7629  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7630  }
7631 
7632  case 0xDB: // str 32
7633  {
7634  uint32_t len;
7635  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7636  }
7637 
7638  default:
7639  {
7640  auto last_token = get_token_string();
7641  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")));
7642  }
7643  }
7644  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
std::string get_token_string() const
Definition: json.hpp:8253
bool get_string(const input_format_t format, const NumberType len, string_t &result)
create a string by reading characters from the input
Definition: json.hpp:8218
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
template<typename NumberType , bool InputIsLittleEndian = false>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_number ( const input_format_t  format,
NumberType &  result 
)
inlineprivate

Definition at line 8175 of file json.hpp.

References JSON_UNLIKELY.

8176  {
8177  // step 1: read input into array with system's byte order
8178  std::array<uint8_t, sizeof(NumberType)> vec;
8179  for (std::size_t i = 0; i < sizeof(NumberType); ++i)
8180  {
8181  get();
8182  if (JSON_UNLIKELY(not unexpect_eof(format, "number")))
8183  {
8184  return false;
8185  }
8186 
8187  // reverse byte order prior to conversion if necessary
8188  if (is_little_endian && !InputIsLittleEndian)
8189  {
8190  vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
8191  }
8192  else
8193  {
8194  vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
8195  }
8196  }
8197 
8198  // step 2: convert array into number of type T and return
8199  std::memcpy(&result, vec.data(), sizeof(NumberType));
8200  return true;
8201  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
const bool is_little_endian
whether we can assume little endianess
Definition: json.hpp:8310
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
template<typename NumberType >
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_string ( const input_format_t  format,
const NumberType  len,
string_t result 
)
inlineprivate

create a string by reading characters from the input

Template Parameters
NumberTypethe type of the number
Parameters
[in]formatthe current format (for diagnostics)
[in]lennumber of characters to read
[out]resultstring created by reading len bytes
Returns
whether string creation completed
Note
We can not reserve len bytes for the result, because len may be too large. Usually, unexpect_eof() detects the end of the input before we run out of string memory.

Definition at line 8218 of file json.hpp.

References JSON_UNLIKELY.

8221  {
8222  bool success = true;
8223  std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
8224  {
8225  get();
8226  if (JSON_UNLIKELY(not unexpect_eof(format, "string")))
8227  {
8228  success = false;
8229  }
8230  return static_cast<char>(current);
8231  });
8232  return success;
8233  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
std::string nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_token_string ( ) const
inlineprivate
Returns
a string representation of the last read byte

Definition at line 8253 of file json.hpp.

References nlohmann::detail::string.

8254  {
8255  char cr[3];
8256  snprintf(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
8257  return std::string{cr};
8258  }
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_array ( )
inlineprivate
Returns
whether array creation completed

Definition at line 7994 of file json.hpp.

References JSON_UNLIKELY.

7995  {
7996  std::pair<std::size_t, int> size_and_type;
7997  if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
7998  {
7999  return false;
8000  }
8001 
8002  if (size_and_type.first != string_t::npos)
8003  {
8004  if (JSON_UNLIKELY(not sax->start_array(size_and_type.first)))
8005  {
8006  return false;
8007  }
8008 
8009  if (size_and_type.second != 0)
8010  {
8011  if (size_and_type.second != 'N')
8012  {
8013  for (std::size_t i = 0; i < size_and_type.first; ++i)
8014  {
8015  if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
8016  {
8017  return false;
8018  }
8019  }
8020  }
8021  }
8022  else
8023  {
8024  for (std::size_t i = 0; i < size_and_type.first; ++i)
8025  {
8027  {
8028  return false;
8029  }
8030  }
8031  }
8032  }
8033  else
8034  {
8035  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
8036  {
8037  return false;
8038  }
8039 
8040  while (current != ']')
8041  {
8042  if (JSON_UNLIKELY(not parse_ubjson_internal(false)))
8043  {
8044  return false;
8045  }
8046  get_ignore_noop();
8047  }
8048  }
8049 
8050  return sax->end_array();
8051  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool parse_ubjson_internal(const bool get_char=true)
Definition: json.hpp:7709
bool get_ubjson_value(const int prefix)
Definition: json.hpp:7898
int current
the current character
Definition: json.hpp:8304
bool get_ubjson_size_type(std::pair< std::size_t, int > &result)
determine the type and size for a container
Definition: json.hpp:7859
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_object ( )
inlineprivate
Returns
whether object creation completed

Definition at line 8056 of file json.hpp.

References JSON_UNLIKELY.

8057  {
8058  std::pair<std::size_t, int> size_and_type;
8059  if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
8060  {
8061  return false;
8062  }
8063 
8064  string_t key;
8065  if (size_and_type.first != string_t::npos)
8066  {
8067  if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
8068  {
8069  return false;
8070  }
8071 
8072  if (size_and_type.second != 0)
8073  {
8074  for (std::size_t i = 0; i < size_and_type.first; ++i)
8075  {
8076  if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
8077  {
8078  return false;
8079  }
8080  if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
8081  {
8082  return false;
8083  }
8084  key.clear();
8085  }
8086  }
8087  else
8088  {
8089  for (std::size_t i = 0; i < size_and_type.first; ++i)
8090  {
8091  if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
8092  {
8093  return false;
8094  }
8096  {
8097  return false;
8098  }
8099  key.clear();
8100  }
8101  }
8102  }
8103  else
8104  {
8105  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
8106  {
8107  return false;
8108  }
8109 
8110  while (current != '}')
8111  {
8112  if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
8113  {
8114  return false;
8115  }
8117  {
8118  return false;
8119  }
8120  get_ignore_noop();
8121  key.clear();
8122  }
8123  }
8124 
8125  return sax->end_object();
8126  }
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
bool get_ubjson_string(string_t &result, const bool get_char=true)
reads a UBJSON string
Definition: json.hpp:7728
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool parse_ubjson_internal(const bool get_char=true)
Definition: json.hpp:7709
bool get_ubjson_value(const int prefix)
Definition: json.hpp:7898
int current
the current character
Definition: json.hpp:8304
bool get_ubjson_size_type(std::pair< std::size_t, int > &result)
determine the type and size for a container
Definition: json.hpp:7859
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_size_type ( std::pair< std::size_t, int > &  result)
inlineprivate

determine the type and size for a container

In the optimized UBJSON format, a type and a size can be provided to allow for a more compact representation.

Parameters
[out]resultpair of the size and the type
Returns
whether pair creation completed

Definition at line 7859 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_UNLIKELY, and nlohmann::detail::ubjson.

7860  {
7861  result.first = string_t::npos; // size
7862  result.second = 0; // type
7863 
7864  get_ignore_noop();
7865 
7866  if (current == '$')
7867  {
7868  result.second = get(); // must not ignore 'N', because 'N' maybe the type
7870  {
7871  return false;
7872  }
7873 
7874  get_ignore_noop();
7875  if (JSON_UNLIKELY(current != '#'))
7876  {
7878  {
7879  return false;
7880  }
7881  auto last_token = get_token_string();
7882  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")));
7883  }
7884 
7885  return get_ubjson_size_value(result.first);
7886  }
7887  else if (current == '#')
7888  {
7889  return get_ubjson_size_value(result.first);
7890  }
7891  return true;
7892  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
std::string get_token_string() const
Definition: json.hpp:8253
bool get_ubjson_size_value(std::size_t &result)
Definition: json.hpp:7782
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_size_value ( std::size_t &  result)
inlineprivate
Parameters
[out]resultdetermined size
Returns
whether size determination completed

Definition at line 7782 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_UNLIKELY, and nlohmann::detail::ubjson.

7783  {
7784  switch (get_ignore_noop())
7785  {
7786  case 'U':
7787  {
7788  uint8_t number;
7790  {
7791  return false;
7792  }
7793  result = static_cast<std::size_t>(number);
7794  return true;
7795  }
7796 
7797  case 'i':
7798  {
7799  int8_t number;
7801  {
7802  return false;
7803  }
7804  result = static_cast<std::size_t>(number);
7805  return true;
7806  }
7807 
7808  case 'I':
7809  {
7810  int16_t number;
7812  {
7813  return false;
7814  }
7815  result = static_cast<std::size_t>(number);
7816  return true;
7817  }
7818 
7819  case 'l':
7820  {
7821  int32_t number;
7823  {
7824  return false;
7825  }
7826  result = static_cast<std::size_t>(number);
7827  return true;
7828  }
7829 
7830  case 'L':
7831  {
7832  int64_t number;
7834  {
7835  return false;
7836  }
7837  result = static_cast<std::size_t>(number);
7838  return true;
7839  }
7840 
7841  default:
7842  {
7843  auto last_token = get_token_string();
7844  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")));
7845  }
7846  }
7847  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
std::string get_token_string() const
Definition: json.hpp:8253
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_string ( string_t result,
const bool  get_char = true 
)
inlineprivate

reads a UBJSON string

This function is either called after reading the 'S' byte explicitly indicating a string, or in case of an object key where the 'S' byte can be left out.

Parameters
[out]resultcreated string
[in]get_charwhether a new character should be retrieved from the input (true, default) or whether the last read character should be considered instead
Returns
whether string creation completed

Definition at line 7728 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_UNLIKELY, and nlohmann::detail::ubjson.

7729  {
7730  if (get_char)
7731  {
7732  get(); // TODO: may we ignore N here?
7733  }
7734 
7736  {
7737  return false;
7738  }
7739 
7740  switch (current)
7741  {
7742  case 'U':
7743  {
7744  uint8_t len;
7745  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7746  }
7747 
7748  case 'i':
7749  {
7750  int8_t len;
7751  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7752  }
7753 
7754  case 'I':
7755  {
7756  int16_t len;
7757  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7758  }
7759 
7760  case 'l':
7761  {
7762  int32_t len;
7763  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7764  }
7765 
7766  case 'L':
7767  {
7768  int64_t len;
7769  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7770  }
7771 
7772  default:
7773  auto last_token = get_token_string();
7774  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")));
7775  }
7776  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
static CCL_BEGIN_DECLS double x[111][8]
std::string get_token_string() const
Definition: json.hpp:8253
bool get_string(const input_format_t format, const NumberType len, string_t &result)
create a string by reading characters from the input
Definition: json.hpp:8218
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::get_ubjson_value ( const int  prefix)
inlineprivate
Parameters
prefixthe previously read or set type prefix
Returns
whether value creation completed

Definition at line 7898 of file json.hpp.

References nlohmann::detail::parse_error::create(), JSON_UNLIKELY, and nlohmann::detail::ubjson.

7899  {
7900  switch (prefix)
7901  {
7902  case std::char_traits<char>::eof(): // EOF
7903  return unexpect_eof(input_format_t::ubjson, "value");
7904 
7905  case 'T': // true
7906  return sax->boolean(true);
7907  case 'F': // false
7908  return sax->boolean(false);
7909 
7910  case 'Z': // null
7911  return sax->null();
7912 
7913  case 'U':
7914  {
7915  uint8_t number;
7916  return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
7917  }
7918 
7919  case 'i':
7920  {
7921  int8_t number;
7922  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7923  }
7924 
7925  case 'I':
7926  {
7927  int16_t number;
7928  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7929  }
7930 
7931  case 'l':
7932  {
7933  int32_t number;
7934  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7935  }
7936 
7937  case 'L':
7938  {
7939  int64_t number;
7940  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7941  }
7942 
7943  case 'd':
7944  {
7945  float number;
7946  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
7947  }
7948 
7949  case 'D':
7950  {
7951  double number;
7952  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
7953  }
7954 
7955  case 'C': // char
7956  {
7957  get();
7959  {
7960  return false;
7961  }
7962  if (JSON_UNLIKELY(current > 127))
7963  {
7964  auto last_token = get_token_string();
7965  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")));
7966  }
7967  string_t s(1, static_cast<char>(current));
7968  return sax->string(s);
7969  }
7970 
7971  case 'S': // string
7972  {
7973  string_t s;
7974  return get_ubjson_string(s) and sax->string(s);
7975  }
7976 
7977  case '[': // array
7978  return get_ubjson_array();
7979 
7980  case '{': // object
7981  return get_ubjson_object();
7982 
7983  default: // anything else
7984  {
7985  auto last_token = get_token_string();
7986  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")));
7987  }
7988  }
7989  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
bool get_ubjson_string(string_t &result, const bool get_char=true)
reads a UBJSON string
Definition: json.hpp:7728
number value (signed integer)
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
std::string get_token_string() const
Definition: json.hpp:8253
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
number value (unsigned integer)
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
number value (floating-point)
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
typename BasicJsonType::number_float_t number_float_t
Definition: json.hpp:6374
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
static constexpr bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::little_endianess ( int  num = 1)
inlinestaticnoexcept

determine system byte order

Returns
true if and only if system's byte order is little endian
Note
from http://stackoverflow.com/a/1001328/266378

Definition at line 6457 of file json.hpp.

6458  {
6459  return (*reinterpret_cast<char*>(&num) == 1);
6460  }
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_bson_array ( )
inlineprivate

Reads an array from the BSON input and passes it to the SAX-parser.

Returns
whether a valid BSON-array was passed to the SAX parser

Definition at line 6657 of file json.hpp.

References nlohmann::detail::bson, and JSON_UNLIKELY.

6658  {
6659  std::int32_t document_size;
6660  get_number<std::int32_t, true>(input_format_t::bson, document_size);
6661 
6662  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
6663  {
6664  return false;
6665  }
6666 
6667  if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/true)))
6668  {
6669  return false;
6670  }
6671 
6672  return sax->end_array();
6673  }
bool parse_bson_element_list(const bool is_array)
Read a BSON element list (as specified in the BSON-spec)
Definition: json.hpp:6620
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_bson_element_internal ( const int  element_type,
const std::size_t  element_type_parse_position 
)
inlineprivate

Read a BSON document element of the given element_type.

Parameters
[in]element_typeThe BSON element type, c.f. http://bsonspec.org/spec.html
[in]element_type_parse_positionThe position in the input stream, where the element_type was read.
Warning
Not all BSON element types are supported yet. An unsupported element_type will give rise to a parse_error.114: Unsupported BSON record type 0x...
Returns
whether a valid BSON-object/array was passed to the SAX parser

Definition at line 6549 of file json.hpp.

References nlohmann::detail::bson, nlohmann::detail::parse_error::create(), nlohmann::detail::string, and Catch::Generators::value().

6551  {
6552  switch (element_type)
6553  {
6554  case 0x01: // double
6555  {
6556  double number;
6557  return get_number<double, true>(input_format_t::bson, number) and sax->number_float(static_cast<number_float_t>(number), "");
6558  }
6559 
6560  case 0x02: // string
6561  {
6562  std::int32_t len;
6563  string_t value;
6564  return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value);
6565  }
6566 
6567  case 0x03: // object
6568  {
6569  return parse_bson_internal();
6570  }
6571 
6572  case 0x04: // array
6573  {
6574  return parse_bson_array();
6575  }
6576 
6577  case 0x08: // boolean
6578  {
6579  return sax->boolean(static_cast<bool>(get()));
6580  }
6581 
6582  case 0x0A: // null
6583  {
6584  return sax->null();
6585  }
6586 
6587  case 0x10: // int32
6588  {
6589  std::int32_t value;
6590  return get_number<std::int32_t, true>(input_format_t::bson, value) and sax->number_integer(value);
6591  }
6592 
6593  case 0x12: // int64
6594  {
6595  std::int64_t value;
6596  return get_number<std::int64_t, true>(input_format_t::bson, value) and sax->number_integer(value);
6597  }
6598 
6599  default: // anything else not supported (yet)
6600  {
6601  char cr[3];
6602  snprintf(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
6603  return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr)));
6604  }
6605  }
6606  }
bool get_bson_string(const NumberType len, string_t &result)
Parses a zero-terminated string of length len from the BSON input.
Definition: json.hpp:6528
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
bool parse_bson_array()
Reads an array from the BSON input and passes it to the SAX-parser.
Definition: json.hpp:6657
bool parse_bson_internal()
Reads in a BSON-object and passes it to the SAX-parser.
Definition: json.hpp:6471
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_bson_element_list ( const bool  is_array)
inlineprivate

Read a BSON element list (as specified in the BSON-spec)

The same binary layout is used for objects and arrays, hence it must be indicated with the argument is_array which one is expected (true –> array, false –> object).

Parameters
[in]is_arrayDetermines if the element list being read is to be treated as an object (is_array == false), or as an array (is_array == true).
Returns
whether a valid BSON-object/array was passed to the SAX parser

Definition at line 6620 of file json.hpp.

References nlohmann::detail::bson, and JSON_UNLIKELY.

6621  {
6622  string_t key;
6623  while (int element_type = get())
6624  {
6625  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
6626  {
6627  return false;
6628  }
6629 
6630  const std::size_t element_type_parse_position = chars_read;
6631  if (JSON_UNLIKELY(not get_bson_cstr(key)))
6632  {
6633  return false;
6634  }
6635 
6636  if (not is_array)
6637  {
6638  sax->key(key);
6639  }
6640 
6641  if (JSON_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position)))
6642  {
6643  return false;
6644  }
6645 
6646  // get_bson_cstr only appends
6647  key.clear();
6648  }
6649 
6650  return true;
6651  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_bson_cstr(string_t &result)
Parses a C-style string from the BSON input.
Definition: json.hpp:6496
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
bool parse_bson_element_internal(const int element_type, const std::size_t element_type_parse_position)
Read a BSON document element of the given element_type.
Definition: json.hpp:6549
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_bson_internal ( )
inlineprivate

Reads in a BSON-object and passes it to the SAX-parser.

Returns
whether a valid BSON-value was passed to the SAX parser

Definition at line 6471 of file json.hpp.

References nlohmann::detail::bson, and JSON_UNLIKELY.

6472  {
6473  std::int32_t document_size;
6474  get_number<std::int32_t, true>(input_format_t::bson, document_size);
6475 
6476  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
6477  {
6478  return false;
6479  }
6480 
6481  if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/false)))
6482  {
6483  return false;
6484  }
6485 
6486  return sax->end_object();
6487  }
bool parse_bson_element_list(const bool is_array)
Read a BSON element list (as specified in the BSON-spec)
Definition: json.hpp:6620
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_cbor_internal ( const bool  get_char = true)
inlineprivate
Parameters
[in]get_charwhether a new character should be retrieved from the input (true, default) or whether the last read character should be considered instead
Returns
whether a valid CBOR value was passed to the SAX parser

Definition at line 6686 of file json.hpp.

References nlohmann::detail::cbor, nlohmann::detail::parse_error::create(), and JSON_UNLIKELY.

6687  {
6688  switch (get_char ? get() : current)
6689  {
6690  // EOF
6691  case std::char_traits<char>::eof():
6692  return unexpect_eof(input_format_t::cbor, "value");
6693 
6694  // Integer 0x00..0x17 (0..23)
6695  case 0x00:
6696  case 0x01:
6697  case 0x02:
6698  case 0x03:
6699  case 0x04:
6700  case 0x05:
6701  case 0x06:
6702  case 0x07:
6703  case 0x08:
6704  case 0x09:
6705  case 0x0A:
6706  case 0x0B:
6707  case 0x0C:
6708  case 0x0D:
6709  case 0x0E:
6710  case 0x0F:
6711  case 0x10:
6712  case 0x11:
6713  case 0x12:
6714  case 0x13:
6715  case 0x14:
6716  case 0x15:
6717  case 0x16:
6718  case 0x17:
6719  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
6720 
6721  case 0x18: // Unsigned integer (one-byte uint8_t follows)
6722  {
6723  uint8_t number;
6724  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6725  }
6726 
6727  case 0x19: // Unsigned integer (two-byte uint16_t follows)
6728  {
6729  uint16_t number;
6730  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6731  }
6732 
6733  case 0x1A: // Unsigned integer (four-byte uint32_t follows)
6734  {
6735  uint32_t number;
6736  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6737  }
6738 
6739  case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
6740  {
6741  uint64_t number;
6742  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6743  }
6744 
6745  // Negative integer -1-0x00..-1-0x17 (-1..-24)
6746  case 0x20:
6747  case 0x21:
6748  case 0x22:
6749  case 0x23:
6750  case 0x24:
6751  case 0x25:
6752  case 0x26:
6753  case 0x27:
6754  case 0x28:
6755  case 0x29:
6756  case 0x2A:
6757  case 0x2B:
6758  case 0x2C:
6759  case 0x2D:
6760  case 0x2E:
6761  case 0x2F:
6762  case 0x30:
6763  case 0x31:
6764  case 0x32:
6765  case 0x33:
6766  case 0x34:
6767  case 0x35:
6768  case 0x36:
6769  case 0x37:
6770  return sax->number_integer(static_cast<int8_t>(0x20 - 1 - current));
6771 
6772  case 0x38: // Negative integer (one-byte uint8_t follows)
6773  {
6774  uint8_t number;
6775  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6776  }
6777 
6778  case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
6779  {
6780  uint16_t number;
6781  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6782  }
6783 
6784  case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
6785  {
6786  uint32_t number;
6787  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6788  }
6789 
6790  case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
6791  {
6792  uint64_t number;
6793  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
6794  - static_cast<number_integer_t>(number));
6795  }
6796 
6797  // UTF-8 string (0x00..0x17 bytes follow)
6798  case 0x60:
6799  case 0x61:
6800  case 0x62:
6801  case 0x63:
6802  case 0x64:
6803  case 0x65:
6804  case 0x66:
6805  case 0x67:
6806  case 0x68:
6807  case 0x69:
6808  case 0x6A:
6809  case 0x6B:
6810  case 0x6C:
6811  case 0x6D:
6812  case 0x6E:
6813  case 0x6F:
6814  case 0x70:
6815  case 0x71:
6816  case 0x72:
6817  case 0x73:
6818  case 0x74:
6819  case 0x75:
6820  case 0x76:
6821  case 0x77:
6822  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
6823  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
6824  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
6825  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
6826  case 0x7F: // UTF-8 string (indefinite length)
6827  {
6828  string_t s;
6829  return get_cbor_string(s) and sax->string(s);
6830  }
6831 
6832  // array (0x00..0x17 data items follow)
6833  case 0x80:
6834  case 0x81:
6835  case 0x82:
6836  case 0x83:
6837  case 0x84:
6838  case 0x85:
6839  case 0x86:
6840  case 0x87:
6841  case 0x88:
6842  case 0x89:
6843  case 0x8A:
6844  case 0x8B:
6845  case 0x8C:
6846  case 0x8D:
6847  case 0x8E:
6848  case 0x8F:
6849  case 0x90:
6850  case 0x91:
6851  case 0x92:
6852  case 0x93:
6853  case 0x94:
6854  case 0x95:
6855  case 0x96:
6856  case 0x97:
6857  return get_cbor_array(static_cast<std::size_t>(current & 0x1F));
6858 
6859  case 0x98: // array (one-byte uint8_t for n follows)
6860  {
6861  uint8_t len;
6862  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6863  }
6864 
6865  case 0x99: // array (two-byte uint16_t for n follow)
6866  {
6867  uint16_t len;
6868  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6869  }
6870 
6871  case 0x9A: // array (four-byte uint32_t for n follow)
6872  {
6873  uint32_t len;
6874  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6875  }
6876 
6877  case 0x9B: // array (eight-byte uint64_t for n follow)
6878  {
6879  uint64_t len;
6880  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6881  }
6882 
6883  case 0x9F: // array (indefinite length)
6884  return get_cbor_array(std::size_t(-1));
6885 
6886  // map (0x00..0x17 pairs of data items follow)
6887  case 0xA0:
6888  case 0xA1:
6889  case 0xA2:
6890  case 0xA3:
6891  case 0xA4:
6892  case 0xA5:
6893  case 0xA6:
6894  case 0xA7:
6895  case 0xA8:
6896  case 0xA9:
6897  case 0xAA:
6898  case 0xAB:
6899  case 0xAC:
6900  case 0xAD:
6901  case 0xAE:
6902  case 0xAF:
6903  case 0xB0:
6904  case 0xB1:
6905  case 0xB2:
6906  case 0xB3:
6907  case 0xB4:
6908  case 0xB5:
6909  case 0xB6:
6910  case 0xB7:
6911  return get_cbor_object(static_cast<std::size_t>(current & 0x1F));
6912 
6913  case 0xB8: // map (one-byte uint8_t for n follows)
6914  {
6915  uint8_t len;
6916  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6917  }
6918 
6919  case 0xB9: // map (two-byte uint16_t for n follow)
6920  {
6921  uint16_t len;
6922  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6923  }
6924 
6925  case 0xBA: // map (four-byte uint32_t for n follow)
6926  {
6927  uint32_t len;
6928  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6929  }
6930 
6931  case 0xBB: // map (eight-byte uint64_t for n follow)
6932  {
6933  uint64_t len;
6934  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6935  }
6936 
6937  case 0xBF: // map (indefinite length)
6938  return get_cbor_object(std::size_t(-1));
6939 
6940  case 0xF4: // false
6941  return sax->boolean(false);
6942 
6943  case 0xF5: // true
6944  return sax->boolean(true);
6945 
6946  case 0xF6: // null
6947  return sax->null();
6948 
6949  case 0xF9: // Half-Precision Float (two-byte IEEE 754)
6950  {
6951  const int byte1_raw = get();
6952  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
6953  {
6954  return false;
6955  }
6956  const int byte2_raw = get();
6957  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
6958  {
6959  return false;
6960  }
6961 
6962  const auto byte1 = static_cast<unsigned char>(byte1_raw);
6963  const auto byte2 = static_cast<unsigned char>(byte2_raw);
6964 
6965  // code from RFC 7049, Appendix D, Figure 3:
6966  // As half-precision floating-point numbers were only added
6967  // to IEEE 754 in 2008, today's programming platforms often
6968  // still only have limited support for them. It is very
6969  // easy to include at least decoding support for them even
6970  // without such support. An example of a small decoder for
6971  // half-precision floating-point numbers in the C language
6972  // is shown in Fig. 3.
6973  const int half = (byte1 << 8) + byte2;
6974  const double val = [&half]
6975  {
6976  const int exp = (half >> 10) & 0x1F;
6977  const int mant = half & 0x3FF;
6978  assert(0 <= exp and exp <= 32);
6979  assert(0 <= mant and mant <= 1024);
6980  switch (exp)
6981  {
6982  case 0:
6983  return std::ldexp(mant, -24);
6984  case 31:
6985  return (mant == 0)
6986  ? std::numeric_limits<double>::infinity()
6987  : std::numeric_limits<double>::quiet_NaN();
6988  default:
6989  return std::ldexp(mant + 1024, exp - 25);
6990  }
6991  }();
6992  return sax->number_float((half & 0x8000) != 0
6993  ? static_cast<number_float_t>(-val)
6994  : static_cast<number_float_t>(val), "");
6995  }
6996 
6997  case 0xFA: // Single-Precision Float (four-byte IEEE 754)
6998  {
6999  float number;
7000  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
7001  }
7002 
7003  case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
7004  {
7005  double number;
7006  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
7007  }
7008 
7009  default: // anything else (0xFF is handled inside the other types)
7010  {
7011  auto last_token = get_token_string();
7012  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")));
7013  }
7014  }
7015  }
double double
Definition: precision.hpp:19
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
number value (signed integer)
Definition: json.hpp:20160
typename BasicJsonType::number_integer_t number_integer_t
Definition: json.hpp:6372
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
bool get_cbor_object(const std::size_t len)
Definition: json.hpp:7153
bool get_cbor_array(const std::size_t len)
Definition: json.hpp:7117
std::string get_token_string() const
Definition: json.hpp:8253
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
number value (unsigned integer)
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
number value (floating-point)
bool get_cbor_string(string_t &result)
reads a CBOR string
Definition: json.hpp:7028
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
typename BasicJsonType::number_float_t number_float_t
Definition: json.hpp:6374
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_msgpack_internal ( )
inlineprivate
Returns
whether a valid MessagePack value was passed to the SAX parser

Definition at line 7205 of file json.hpp.

References nlohmann::detail::parse_error::create(), and nlohmann::detail::msgpack.

7206  {
7207  switch (get())
7208  {
7209  // EOF
7210  case std::char_traits<char>::eof():
7211  return unexpect_eof(input_format_t::msgpack, "value");
7212 
7213  // positive fixint
7214  case 0x00:
7215  case 0x01:
7216  case 0x02:
7217  case 0x03:
7218  case 0x04:
7219  case 0x05:
7220  case 0x06:
7221  case 0x07:
7222  case 0x08:
7223  case 0x09:
7224  case 0x0A:
7225  case 0x0B:
7226  case 0x0C:
7227  case 0x0D:
7228  case 0x0E:
7229  case 0x0F:
7230  case 0x10:
7231  case 0x11:
7232  case 0x12:
7233  case 0x13:
7234  case 0x14:
7235  case 0x15:
7236  case 0x16:
7237  case 0x17:
7238  case 0x18:
7239  case 0x19:
7240  case 0x1A:
7241  case 0x1B:
7242  case 0x1C:
7243  case 0x1D:
7244  case 0x1E:
7245  case 0x1F:
7246  case 0x20:
7247  case 0x21:
7248  case 0x22:
7249  case 0x23:
7250  case 0x24:
7251  case 0x25:
7252  case 0x26:
7253  case 0x27:
7254  case 0x28:
7255  case 0x29:
7256  case 0x2A:
7257  case 0x2B:
7258  case 0x2C:
7259  case 0x2D:
7260  case 0x2E:
7261  case 0x2F:
7262  case 0x30:
7263  case 0x31:
7264  case 0x32:
7265  case 0x33:
7266  case 0x34:
7267  case 0x35:
7268  case 0x36:
7269  case 0x37:
7270  case 0x38:
7271  case 0x39:
7272  case 0x3A:
7273  case 0x3B:
7274  case 0x3C:
7275  case 0x3D:
7276  case 0x3E:
7277  case 0x3F:
7278  case 0x40:
7279  case 0x41:
7280  case 0x42:
7281  case 0x43:
7282  case 0x44:
7283  case 0x45:
7284  case 0x46:
7285  case 0x47:
7286  case 0x48:
7287  case 0x49:
7288  case 0x4A:
7289  case 0x4B:
7290  case 0x4C:
7291  case 0x4D:
7292  case 0x4E:
7293  case 0x4F:
7294  case 0x50:
7295  case 0x51:
7296  case 0x52:
7297  case 0x53:
7298  case 0x54:
7299  case 0x55:
7300  case 0x56:
7301  case 0x57:
7302  case 0x58:
7303  case 0x59:
7304  case 0x5A:
7305  case 0x5B:
7306  case 0x5C:
7307  case 0x5D:
7308  case 0x5E:
7309  case 0x5F:
7310  case 0x60:
7311  case 0x61:
7312  case 0x62:
7313  case 0x63:
7314  case 0x64:
7315  case 0x65:
7316  case 0x66:
7317  case 0x67:
7318  case 0x68:
7319  case 0x69:
7320  case 0x6A:
7321  case 0x6B:
7322  case 0x6C:
7323  case 0x6D:
7324  case 0x6E:
7325  case 0x6F:
7326  case 0x70:
7327  case 0x71:
7328  case 0x72:
7329  case 0x73:
7330  case 0x74:
7331  case 0x75:
7332  case 0x76:
7333  case 0x77:
7334  case 0x78:
7335  case 0x79:
7336  case 0x7A:
7337  case 0x7B:
7338  case 0x7C:
7339  case 0x7D:
7340  case 0x7E:
7341  case 0x7F:
7342  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
7343 
7344  // fixmap
7345  case 0x80:
7346  case 0x81:
7347  case 0x82:
7348  case 0x83:
7349  case 0x84:
7350  case 0x85:
7351  case 0x86:
7352  case 0x87:
7353  case 0x88:
7354  case 0x89:
7355  case 0x8A:
7356  case 0x8B:
7357  case 0x8C:
7358  case 0x8D:
7359  case 0x8E:
7360  case 0x8F:
7361  return get_msgpack_object(static_cast<std::size_t>(current & 0x0F));
7362 
7363  // fixarray
7364  case 0x90:
7365  case 0x91:
7366  case 0x92:
7367  case 0x93:
7368  case 0x94:
7369  case 0x95:
7370  case 0x96:
7371  case 0x97:
7372  case 0x98:
7373  case 0x99:
7374  case 0x9A:
7375  case 0x9B:
7376  case 0x9C:
7377  case 0x9D:
7378  case 0x9E:
7379  case 0x9F:
7380  return get_msgpack_array(static_cast<std::size_t>(current & 0x0F));
7381 
7382  // fixstr
7383  case 0xA0:
7384  case 0xA1:
7385  case 0xA2:
7386  case 0xA3:
7387  case 0xA4:
7388  case 0xA5:
7389  case 0xA6:
7390  case 0xA7:
7391  case 0xA8:
7392  case 0xA9:
7393  case 0xAA:
7394  case 0xAB:
7395  case 0xAC:
7396  case 0xAD:
7397  case 0xAE:
7398  case 0xAF:
7399  case 0xB0:
7400  case 0xB1:
7401  case 0xB2:
7402  case 0xB3:
7403  case 0xB4:
7404  case 0xB5:
7405  case 0xB6:
7406  case 0xB7:
7407  case 0xB8:
7408  case 0xB9:
7409  case 0xBA:
7410  case 0xBB:
7411  case 0xBC:
7412  case 0xBD:
7413  case 0xBE:
7414  case 0xBF:
7415  {
7416  string_t s;
7417  return get_msgpack_string(s) and sax->string(s);
7418  }
7419 
7420  case 0xC0: // nil
7421  return sax->null();
7422 
7423  case 0xC2: // false
7424  return sax->boolean(false);
7425 
7426  case 0xC3: // true
7427  return sax->boolean(true);
7428 
7429  case 0xCA: // float 32
7430  {
7431  float number;
7432  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
7433  }
7434 
7435  case 0xCB: // float 64
7436  {
7437  double number;
7438  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
7439  }
7440 
7441  case 0xCC: // uint 8
7442  {
7443  uint8_t number;
7444  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7445  }
7446 
7447  case 0xCD: // uint 16
7448  {
7449  uint16_t number;
7450  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7451  }
7452 
7453  case 0xCE: // uint 32
7454  {
7455  uint32_t number;
7456  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7457  }
7458 
7459  case 0xCF: // uint 64
7460  {
7461  uint64_t number;
7462  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7463  }
7464 
7465  case 0xD0: // int 8
7466  {
7467  int8_t number;
7468  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7469  }
7470 
7471  case 0xD1: // int 16
7472  {
7473  int16_t number;
7474  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7475  }
7476 
7477  case 0xD2: // int 32
7478  {
7479  int32_t number;
7480  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7481  }
7482 
7483  case 0xD3: // int 64
7484  {
7485  int64_t number;
7486  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7487  }
7488 
7489  case 0xD9: // str 8
7490  case 0xDA: // str 16
7491  case 0xDB: // str 32
7492  {
7493  string_t s;
7494  return get_msgpack_string(s) and sax->string(s);
7495  }
7496 
7497  case 0xDC: // array 16
7498  {
7499  uint16_t len;
7500  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
7501  }
7502 
7503  case 0xDD: // array 32
7504  {
7505  uint32_t len;
7506  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
7507  }
7508 
7509  case 0xDE: // map 16
7510  {
7511  uint16_t len;
7512  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
7513  }
7514 
7515  case 0xDF: // map 32
7516  {
7517  uint32_t len;
7518  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
7519  }
7520 
7521  // negative fixint
7522  case 0xE0:
7523  case 0xE1:
7524  case 0xE2:
7525  case 0xE3:
7526  case 0xE4:
7527  case 0xE5:
7528  case 0xE6:
7529  case 0xE7:
7530  case 0xE8:
7531  case 0xE9:
7532  case 0xEA:
7533  case 0xEB:
7534  case 0xEC:
7535  case 0xED:
7536  case 0xEE:
7537  case 0xEF:
7538  case 0xF0:
7539  case 0xF1:
7540  case 0xF2:
7541  case 0xF3:
7542  case 0xF4:
7543  case 0xF5:
7544  case 0xF6:
7545  case 0xF7:
7546  case 0xF8:
7547  case 0xF9:
7548  case 0xFA:
7549  case 0xFB:
7550  case 0xFC:
7551  case 0xFD:
7552  case 0xFE:
7553  case 0xFF:
7554  return sax->number_integer(static_cast<int8_t>(current));
7555 
7556  default: // anything else
7557  {
7558  auto last_token = get_token_string();
7559  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")));
7560  }
7561  }
7562  }
bool unexpect_eof(const input_format_t format, const char *context) const
Definition: json.hpp:8240
typename BasicJsonType::string_t string_t
Definition: json.hpp:6375
number value (signed integer)
Definition: json.hpp:20160
bool get_msgpack_object(const std::size_t len)
Definition: json.hpp:7672
bool get_number(const input_format_t format, NumberType &result)
Definition: json.hpp:8175
std::string get_token_string() const
Definition: json.hpp:8253
bool get_msgpack_string(string_t &result)
reads a MessagePack string
Definition: json.hpp:7574
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
input_format_t
the supported input formats
Definition: json.hpp:2070
number value (unsigned integer)
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
number value (floating-point)
bool get_msgpack_array(const std::size_t len)
Definition: json.hpp:7650
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
typename BasicJsonType::number_float_t number_float_t
Definition: json.hpp:6374
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::parse_ubjson_internal ( const bool  get_char = true)
inlineprivate
Parameters
[in]get_charwhether a new character should be retrieved from the input (true, default) or whether the last read character should be considered instead
Returns
whether a valid UBJSON value was passed to the SAX parser

Definition at line 7709 of file json.hpp.

7710  {
7711  return get_ubjson_value(get_char ? get_ignore_noop() : current);
7712  }
bool get_ubjson_value(const int prefix)
Definition: json.hpp:7898
int current
the current character
Definition: json.hpp:8304
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::sax_parse ( const input_format_t  format,
json_sax_t sax_,
const bool  strict = true 
)
inline
Parameters
[in]formatthe binary format to parse
[in]sax_a SAX event processor
[in]strictwhether to expect the input to be consumed completed
Returns

Definition at line 6397 of file json.hpp.

References nlohmann::detail::bson, nlohmann::detail::cbor, nlohmann::detail::parse_error::create(), JSON_UNLIKELY, nlohmann::detail::msgpack, nlohmann::detail::strict, and nlohmann::detail::ubjson.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_bson(), nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_cbor(), nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_msgpack(), and nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_ubjson().

6400  {
6401  sax = sax_;
6402  bool result = false;
6403 
6404  switch (format)
6405  {
6406  case input_format_t::bson:
6407  result = parse_bson_internal();
6408  break;
6409 
6410  case input_format_t::cbor:
6411  result = parse_cbor_internal();
6412  break;
6413 
6415  result = parse_msgpack_internal();
6416  break;
6417 
6419  result = parse_ubjson_internal();
6420  break;
6421 
6422  // LCOV_EXCL_START
6423  default:
6424  assert(false);
6425  // LCOV_EXCL_STOP
6426  }
6427 
6428  // strict mode: next byte must be EOF
6429  if (result and strict)
6430  {
6431  if (format == input_format_t::ubjson)
6432  {
6433  get_ignore_noop();
6434  }
6435  else
6436  {
6437  get();
6438  }
6439 
6440  if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
6441  {
6442  return sax->parse_error(chars_read, get_token_string(),
6443  parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
6444  }
6445  }
6446 
6447  return result;
6448  }
bool parse_cbor_internal(const bool get_char=true)
Definition: json.hpp:6686
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
throw a type_error exception in case of invalid UTF-8
bool parse_ubjson_internal(const bool get_char=true)
Definition: json.hpp:7709
bool parse_bson_internal()
Reads in a BSON-object and passes it to the SAX-parser.
Definition: json.hpp:6471
std::string get_token_string() const
Definition: json.hpp:8253
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313
template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::unexpect_eof ( const input_format_t  format,
const char *  context 
) const
inlineprivate
Parameters
[in]formatthe current format (for diagnostics)
[in]contextfurther context information (for diagnostics)
Returns
whether the last read character is not EOF

Definition at line 8240 of file json.hpp.

References nlohmann::detail::parse_error::create(), and JSON_UNLIKELY.

8241  {
8242  if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
8243  {
8244  return sax->parse_error(chars_read, "<end of file>",
8245  parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
8246  }
8247  return true;
8248  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
std::string exception_message(const input_format_t format, const std::string &detail, const std::string &context) const
Definition: json.hpp:8266
int current
the current character
Definition: json.hpp:8304
std::size_t chars_read
the number of characters read
Definition: json.hpp:8307
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:894
json_sax_t * sax
the SAX parser
Definition: json.hpp:8313

Member Data Documentation

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
std::size_t nlohmann::detail::binary_reader< BasicJsonType, SAX >::chars_read = 0
private

the number of characters read

Definition at line 8307 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
int nlohmann::detail::binary_reader< BasicJsonType, SAX >::current = std::char_traits<char>::eof()
private

the current character

Definition at line 8304 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
input_adapter_t nlohmann::detail::binary_reader< BasicJsonType, SAX >::ia = nullptr
private

input adapter

Definition at line 8301 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
const bool nlohmann::detail::binary_reader< BasicJsonType, SAX >::is_little_endian = little_endianess()
private

whether we can assume little endianess

Definition at line 8310 of file json.hpp.

template<typename BasicJsonType , typename SAX = json_sax_dom_parser<BasicJsonType>>
json_sax_t* nlohmann::detail::binary_reader< BasicJsonType, SAX >::sax = nullptr
private

the SAX parser

Definition at line 8313 of file json.hpp.


The documentation for this class was generated from the following file: