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

serialization to CBOR and MessagePack values More...

#include <json.hpp>

Public Member Functions

 binary_writer (output_adapter_t< CharType > adapter)
 create a binary writer More...
 
void write_bson (const BasicJsonType &j)
 
void write_cbor (const BasicJsonType &j)
 
void write_msgpack (const BasicJsonType &j)
 
void write_ubjson (const BasicJsonType &j, const bool use_count, const bool use_type, const bool add_prefix=true)
 

Static Public Member Functions

template<typename C = CharType, enable_if_t< std::is_signed< C >::value and std::is_signed< char >::value > * = nullptr>
static constexpr CharType to_char_type (std::uint8_t x) noexcept
 
template<typename C = CharType, enable_if_t< std::is_signed< C >::value and std::is_unsigned< char >::value > * = nullptr>
static CharType to_char_type (std::uint8_t x) noexcept
 
template<typename C = CharType, enable_if_t< std::is_unsigned< C >::value > * = nullptr>
static constexpr CharType to_char_type (std::uint8_t x) noexcept
 
template<typename InputCharType , typename C = CharType, enable_if_t< std::is_signed< C >::value andstd::is_signed< char >::value andstd::is_same< char, typename std::remove_cv< InputCharType >::type >::value > * = nullptr>
static constexpr CharType to_char_type (InputCharType x) noexcept
 

Private Types

using string_t = typename BasicJsonType::string_t
 

Private Member Functions

void write_bson_entry_header (const string_t &name, const std::uint8_t element_type)
 Writes the given element_type and name to the output adapter. More...
 
void write_bson_boolean (const string_t &name, const bool value)
 Writes a BSON element with key name and boolean value value. More...
 
void write_bson_double (const string_t &name, const double value)
 Writes a BSON element with key name and double value value. More...
 
void write_bson_string (const string_t &name, const string_t &value)
 Writes a BSON element with key name and string value value. More...
 
void write_bson_null (const string_t &name)
 Writes a BSON element with key name and null value. More...
 
void write_bson_integer (const string_t &name, const std::int64_t value)
 Writes a BSON element with key name and integer value. More...
 
void write_bson_unsigned (const string_t &name, const std::uint64_t value)
 Writes a BSON element with key name and unsigned value. More...
 
void write_bson_object_entry (const string_t &name, const typename BasicJsonType::object_t &value)
 Writes a BSON element with key name and object value. More...
 
void write_bson_array (const string_t &name, const typename BasicJsonType::array_t &value)
 Writes a BSON element with key name and array value. More...
 
void write_bson_element (const string_t &name, const BasicJsonType &j)
 Serializes the JSON value j to BSON and associates it with the key name. More...
 
void write_bson_object (const typename BasicJsonType::object_t &value)
 
template<typename NumberType , typename std::enable_if< std::is_floating_point< NumberType >::value, int >::type = 0>
void write_number_with_ubjson_prefix (const NumberType n, const bool add_prefix)
 
template<typename NumberType , typename std::enable_if< std::is_unsigned< NumberType >::value, int >::type = 0>
void write_number_with_ubjson_prefix (const NumberType n, const bool add_prefix)
 
template<typename NumberType , typename std::enable_if< std::is_signed< NumberType >::value andnot std::is_floating_point< NumberType >::value, int >::type = 0>
void write_number_with_ubjson_prefix (const NumberType n, const bool add_prefix)
 
CharType ubjson_prefix (const BasicJsonType &j) const noexcept
 determine the type prefix of container values More...
 
template<typename NumberType , bool OutputIsLittleEndian = false>
void write_number (const NumberType n)
 

Static Private Member Functions

static std::size_t calc_bson_entry_header_size (const string_t &name)
 
static std::size_t calc_bson_string_size (const string_t &value)
 
static std::size_t calc_bson_integer_size (const std::int64_t value)
 
static constexpr std::size_t calc_bson_unsigned_size (const std::uint64_t value) noexcept
 
static std::size_t calc_bson_array_size (const typename BasicJsonType::array_t &value)
 
static std::size_t calc_bson_element_size (const string_t &name, const BasicJsonType &j)
 Calculates the size necessary to serialize the JSON value j with its name. More...
 
static std::size_t calc_bson_object_size (const typename BasicJsonType::object_t &value)
 Calculates the size of the BSON serialization of the given JSON-object j. More...
 
static constexpr CharType get_cbor_float_prefix (float)
 
static constexpr CharType get_cbor_float_prefix (double)
 
static constexpr CharType get_msgpack_float_prefix (float)
 
static constexpr CharType get_msgpack_float_prefix (double)
 
static constexpr CharType get_ubjson_float_prefix (float)
 
static constexpr CharType get_ubjson_float_prefix (double)
 

Private Attributes

const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess()
 whether we can assume little endianess More...
 
output_adapter_t< CharType > oa = nullptr
 the output More...
 

Detailed Description

template<typename BasicJsonType, typename CharType>
class nlohmann::detail::binary_writer< BasicJsonType, CharType >

serialization to CBOR and MessagePack values

Definition at line 8344 of file json.hpp.

Member Typedef Documentation

template<typename BasicJsonType , typename CharType >
using nlohmann::detail::binary_writer< BasicJsonType, CharType >::string_t = typename BasicJsonType::string_t
private

Definition at line 8346 of file json.hpp.

Constructor & Destructor Documentation

template<typename BasicJsonType , typename CharType >
nlohmann::detail::binary_writer< BasicJsonType, CharType >::binary_writer ( output_adapter_t< CharType >  adapter)
inlineexplicit

create a binary writer

Parameters
[in]adapteroutput adapter to write to

Definition at line 8354 of file json.hpp.

8354  : oa(adapter)
8355  {
8356  assert(oa);
8357  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656

Member Function Documentation

template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_array_size ( const typename BasicJsonType::array_t &  value)
inlinestaticprivate
Returns
The size of the BSON-encoded array value

Definition at line 9185 of file json.hpp.

9186  {
9187  std::size_t embedded_document_size = 0ul;
9188  std::size_t array_index = 0ul;
9189 
9190  for (const auto& el : value)
9191  {
9192  embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el);
9193  }
9194 
9195  return sizeof(std::int32_t) + embedded_document_size + 1ul;
9196  }
static std::size_t calc_bson_element_size(const string_t &name, const BasicJsonType &j)
Calculates the size necessary to serialize the JSON value j with its name.
Definition: json.hpp:9221
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_element_size ( const string_t name,
const BasicJsonType &  j 
)
inlinestaticprivate

Calculates the size necessary to serialize the JSON value j with its name.

Returns
The calculated size for the BSON document entry for j with the given name.

Definition at line 9221 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

9223  {
9224  const auto header_size = calc_bson_entry_header_size(name);
9225  switch (j.type())
9226  {
9227  case value_t::object:
9228  return header_size + calc_bson_object_size(*j.m_value.object);
9229 
9230  case value_t::array:
9231  return header_size + calc_bson_array_size(*j.m_value.array);
9232 
9233  case value_t::boolean:
9234  return header_size + 1ul;
9235 
9236  case value_t::number_float:
9237  return header_size + 8ul;
9238 
9240  return header_size + calc_bson_integer_size(j.m_value.number_integer);
9241 
9243  return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
9244 
9245  case value_t::string:
9246  return header_size + calc_bson_string_size(*j.m_value.string);
9247 
9248  case value_t::null:
9249  return header_size + 0ul;
9250 
9251  // LCOV_EXCL_START
9252  default:
9253  assert(false);
9254  return 0ul;
9255  // LCOV_EXCL_STOP
9256  };
9257  }
static std::size_t calc_bson_string_size(const string_t &value)
Definition: json.hpp:9080
array (ordered collection of values)
number value (signed integer)
name
Definition: setup.py:39
static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
Definition: json.hpp:9143
object (unordered set of name/value pairs)
static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t &value)
Definition: json.hpp:9185
static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t &value)
Calculates the size of the BSON serialization of the given JSON-object j.
Definition: json.hpp:9309
static std::size_t calc_bson_entry_header_size(const string_t &name)
Definition: json.hpp:9033
static std::size_t calc_bson_integer_size(const std::int64_t value)
Definition: json.hpp:9110
number value (unsigned integer)
number value (floating-point)
template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_entry_header_size ( const string_t name)
inlinestaticprivate
Returns
The size of a BSON document entry header, including the id marker and the entry name size (and its null-terminator).

Definition at line 9033 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, and JSON_UNLIKELY.

9034  {
9035  const auto it = name.find(static_cast<typename string_t::value_type>(0));
9036  if (JSON_UNLIKELY(it != BasicJsonType::string_t::npos))
9037  {
9039  "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")"));
9040  }
9041 
9042  return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
9043  }
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
name
Definition: setup.py:39
#define JSON_THROW(exception)
Definition: json.hpp:162
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_integer_size ( const std::int64_t  value)
inlinestaticprivate
Returns
The size of the BSON-encoded integer value

Definition at line 9110 of file json.hpp.

References max(), and anonymous_namespace{chameleon.cpp}::min().

9111  {
9113  {
9114  return sizeof(std::int32_t);
9115  }
9116  else
9117  {
9118  return sizeof(std::int64_t);
9119  }
9120  }
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_object_size ( const typename BasicJsonType::object_t &  value)
inlinestaticprivate

Calculates the size of the BSON serialization of the given JSON-object j.

Parameters
[in]jJSON value to serialize
Precondition
j.type() == value_t::object

Definition at line 9309 of file json.hpp.

9310  {
9311  std::size_t document_size = std::accumulate(value.begin(), value.end(), 0ul,
9312  [](size_t result, const typename BasicJsonType::object_t::value_type & el)
9313  {
9314  return result += calc_bson_element_size(el.first, el.second);
9315  });
9316 
9317  return sizeof(std::int32_t) + document_size + 1ul;
9318  }
static std::size_t calc_bson_element_size(const string_t &name, const BasicJsonType &j)
Calculates the size necessary to serialize the JSON value j with its name.
Definition: json.hpp:9221
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
static std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_string_size ( const string_t value)
inlinestaticprivate
Returns
The size of the BSON-encoded string in value

Definition at line 9080 of file json.hpp.

9081  {
9082  return sizeof(std::int32_t) + value.size() + 1ul;
9083  }
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
static constexpr std::size_t nlohmann::detail::binary_writer< BasicJsonType, CharType >::calc_bson_unsigned_size ( const std::uint64_t  value)
inlinestaticprivatenoexcept
Returns
The size of the BSON-encoded unsigned integer in j

Definition at line 9143 of file json.hpp.

References max(), and Catch::Generators::value().

9144  {
9145  return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
9146  ? sizeof(std::int32_t)
9147  : sizeof(std::int64_t);
9148  }
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_cbor_float_prefix ( float  )
inlinestaticprivate

Definition at line 9340 of file json.hpp.

9341  {
9342  return to_char_type(0xFA); // Single-Precision Float
9343  }
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_cbor_float_prefix ( double  )
inlinestaticprivate

Definition at line 9345 of file json.hpp.

9346  {
9347  return to_char_type(0xFB); // Double-Precision Float
9348  }
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_msgpack_float_prefix ( float  )
inlinestaticprivate

Definition at line 9354 of file json.hpp.

9355  {
9356  return to_char_type(0xCA); // float 32
9357  }
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_msgpack_float_prefix ( double  )
inlinestaticprivate

Definition at line 9359 of file json.hpp.

References Catch::Generators::value().

9360  {
9361  return to_char_type(0xCB); // float 64
9362  }
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_ubjson_float_prefix ( float  )
inlinestaticprivate

Definition at line 9568 of file json.hpp.

9569  {
9570  return 'd'; // float 32
9571  }
template<typename BasicJsonType , typename CharType >
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::get_ubjson_float_prefix ( double  )
inlinestaticprivate

Definition at line 9573 of file json.hpp.

9574  {
9575  return 'D'; // float 64
9576  }
template<typename BasicJsonType , typename CharType >
template<typename C = CharType, enable_if_t< std::is_signed< C >::value and std::is_signed< char >::value > * = nullptr>
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::to_char_type ( std::uint8_t  x)
inlinestaticnoexcept

Definition at line 9617 of file json.hpp.

References Catch::Generators::value(), and x.

Referenced by nlohmann::detail::serializer< BasicJsonType >::dump_escaped().

9618  {
9619  return *reinterpret_cast<char*>(&x);
9620  }
static CCL_BEGIN_DECLS double x[111][8]
template<typename BasicJsonType , typename CharType >
template<typename C = CharType, enable_if_t< std::is_signed< C >::value and std::is_unsigned< char >::value > * = nullptr>
static CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::to_char_type ( std::uint8_t  x)
inlinestaticnoexcept

Definition at line 9624 of file json.hpp.

References Catch::Generators::value(), and x.

9625  {
9626  static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
9627  static_assert(std::is_pod<CharType>::value, "CharType must be POD");
9628  CharType result;
9629  std::memcpy(&result, &x, sizeof(x));
9630  return result;
9631  }
static CCL_BEGIN_DECLS double x[111][8]
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
template<typename C = CharType, enable_if_t< std::is_unsigned< C >::value > * = nullptr>
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::to_char_type ( std::uint8_t  x)
inlinestaticnoexcept

Definition at line 9635 of file json.hpp.

References Catch::Generators::value(), and x.

9636  {
9637  return x;
9638  }
static CCL_BEGIN_DECLS double x[111][8]
template<typename BasicJsonType , typename CharType >
template<typename InputCharType , typename C = CharType, enable_if_t< std::is_signed< C >::value andstd::is_signed< char >::value andstd::is_same< char, typename std::remove_cv< InputCharType >::type >::value > * = nullptr>
static constexpr CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::to_char_type ( InputCharType  x)
inlinestaticnoexcept

Definition at line 9646 of file json.hpp.

References x.

9647  {
9648  return x;
9649  }
static CCL_BEGIN_DECLS double x[111][8]
template<typename BasicJsonType , typename CharType >
CharType nlohmann::detail::binary_writer< BasicJsonType, CharType >::ubjson_prefix ( const BasicJsonType &  j) const
inlineprivatenoexcept

determine the type prefix of container values

Note
This function does not need to be 100% accurate when it comes to integer limits. In case a number exceeds the limits of int64_t, this will be detected by a later call to function write_number_with_ubjson_prefix. Therefore, we return 'L' for any value that does not fit the previous limits.

Definition at line 9497 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, max(), anonymous_namespace{chameleon.cpp}::min(), nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

9498  {
9499  switch (j.type())
9500  {
9501  case value_t::null:
9502  return 'Z';
9503 
9504  case value_t::boolean:
9505  return j.m_value.boolean ? 'T' : 'F';
9506 
9508  {
9509  if ((std::numeric_limits<int8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
9510  {
9511  return 'i';
9512  }
9513  if ((std::numeric_limits<uint8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
9514  {
9515  return 'U';
9516  }
9517  if ((std::numeric_limits<int16_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
9518  {
9519  return 'I';
9520  }
9521  if ((std::numeric_limits<int32_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
9522  {
9523  return 'l';
9524  }
9525  // no check and assume int64_t (see note above)
9526  return 'L';
9527  }
9528 
9530  {
9531  if (j.m_value.number_unsigned <= (std::numeric_limits<int8_t>::max)())
9532  {
9533  return 'i';
9534  }
9535  if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
9536  {
9537  return 'U';
9538  }
9539  if (j.m_value.number_unsigned <= (std::numeric_limits<int16_t>::max)())
9540  {
9541  return 'I';
9542  }
9543  if (j.m_value.number_unsigned <= (std::numeric_limits<int32_t>::max)())
9544  {
9545  return 'l';
9546  }
9547  // no check and assume int64_t (see note above)
9548  return 'L';
9549  }
9550 
9551  case value_t::number_float:
9552  return get_ubjson_float_prefix(j.m_value.number_float);
9553 
9554  case value_t::string:
9555  return 'S';
9556 
9557  case value_t::array:
9558  return '[';
9559 
9560  case value_t::object:
9561  return '{';
9562 
9563  default: // discarded values
9564  return 'N';
9565  }
9566  }
static constexpr CharType get_ubjson_float_prefix(float)
Definition: json.hpp:9568
array (ordered collection of values)
number value (signed integer)
object (unordered set of name/value pairs)
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
number value (unsigned integer)
number value (floating-point)
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson ( const BasicJsonType &  j)
inline
Parameters
[in]jJSON value to serialize
Precondition
j.type() == value_t::object

Definition at line 8363 of file json.hpp.

References nlohmann::detail::type_error::create(), JSON_THROW, nlohmann::detail::object, and nlohmann::detail::string.

8364  {
8365  switch (j.type())
8366  {
8367  case value_t::object:
8368  {
8369  write_bson_object(*j.m_value.object);
8370  break;
8371  }
8372 
8373  default:
8374  {
8375  JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
8376  }
8377  }
8378  }
void write_bson_object(const typename BasicJsonType::object_t &value)
Definition: json.hpp:9324
#define JSON_THROW(exception)
Definition: json.hpp:162
object (unordered set of name/value pairs)
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:1024
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_array ( const string_t name,
const typename BasicJsonType::array_t &  value 
)
inlineprivate

Writes a BSON element with key name and array value.

Definition at line 9201 of file json.hpp.

9203  {
9204  write_bson_entry_header(name, 0x04); // array
9205  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
9206 
9207  std::size_t array_index = 0ul;
9208 
9209  for (const auto& el : value)
9210  {
9211  write_bson_element(std::to_string(array_index++), el);
9212  }
9213 
9214  oa->write_character(to_char_type(0x00));
9215  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t &value)
Definition: json.hpp:9185
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
void write_bson_element(const string_t &name, const BasicJsonType &j)
Serializes the JSON value j to BSON and associates it with the key name.
Definition: json.hpp:9266
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_boolean ( const string_t name,
const bool  value 
)
inlineprivate

Writes a BSON element with key name and boolean value value.

Definition at line 9060 of file json.hpp.

9062  {
9064  oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
9065  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_double ( const string_t name,
const double  value 
)
inlineprivate

Writes a BSON element with key name and double value value.

Definition at line 9070 of file json.hpp.

References Catch::Generators::value().

9072  {
9074  write_number<double, true>(value);
9075  }
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_element ( const string_t name,
const BasicJsonType &  j 
)
inlineprivate

Serializes the JSON value j to BSON and associates it with the key name.

Parameters
nameThe name to associate with the JSON entity j within the current BSON document
Returns
The size of the BSON entry

Definition at line 9266 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

9268  {
9269  switch (j.type())
9270  {
9271  case value_t::object:
9272  return write_bson_object_entry(name, *j.m_value.object);
9273 
9274  case value_t::array:
9275  return write_bson_array(name, *j.m_value.array);
9276 
9277  case value_t::boolean:
9278  return write_bson_boolean(name, j.m_value.boolean);
9279 
9280  case value_t::number_float:
9281  return write_bson_double(name, j.m_value.number_float);
9282 
9284  return write_bson_integer(name, j.m_value.number_integer);
9285 
9287  return write_bson_unsigned(name, j.m_value.number_unsigned);
9288 
9289  case value_t::string:
9290  return write_bson_string(name, *j.m_value.string);
9291 
9292  case value_t::null:
9293  return write_bson_null(name);
9294 
9295  // LCOV_EXCL_START
9296  default:
9297  assert(false);
9298  return;
9299  // LCOV_EXCL_STOP
9300  };
9301  }
array (ordered collection of values)
void write_bson_double(const string_t &name, const double value)
Writes a BSON element with key name and double value value.
Definition: json.hpp:9070
void write_bson_object_entry(const string_t &name, const typename BasicJsonType::object_t &value)
Writes a BSON element with key name and object value.
Definition: json.hpp:9175
number value (signed integer)
name
Definition: setup.py:39
void write_bson_string(const string_t &name, const string_t &value)
Writes a BSON element with key name and string value value.
Definition: json.hpp:9088
void write_bson_null(const string_t &name)
Writes a BSON element with key name and null value.
Definition: json.hpp:9102
object (unordered set of name/value pairs)
void write_bson_array(const string_t &name, const typename BasicJsonType::array_t &value)
Writes a BSON element with key name and array value.
Definition: json.hpp:9201
void write_bson_integer(const string_t &name, const std::int64_t value)
Writes a BSON element with key name and integer value.
Definition: json.hpp:9125
void write_bson_boolean(const string_t &name, const bool value)
Writes a BSON element with key name and boolean value value.
Definition: json.hpp:9060
number value (unsigned integer)
void write_bson_unsigned(const string_t &name, const std::uint64_t value)
Writes a BSON element with key name and unsigned value.
Definition: json.hpp:9153
number value (floating-point)
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_entry_header ( const string_t name,
const std::uint8_t  element_type 
)
inlineprivate

Writes the given element_type and name to the output adapter.

Definition at line 9048 of file json.hpp.

9050  {
9051  oa->write_character(to_char_type(element_type)); // boolean
9052  oa->write_characters(
9053  reinterpret_cast<const CharType*>(name.c_str()),
9054  name.size() + 1u);
9055  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
name
Definition: setup.py:39
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_integer ( const string_t name,
const std::int64_t  value 
)
inlineprivate

Writes a BSON element with key name and integer value.

Definition at line 9125 of file json.hpp.

References max(), anonymous_namespace{chameleon.cpp}::min(), and Catch::Generators::value().

9127  {
9129  {
9130  write_bson_entry_header(name, 0x10); // int32
9131  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
9132  }
9133  else
9134  {
9135  write_bson_entry_header(name, 0x12); // int64
9136  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
9137  }
9138  }
name
Definition: setup.py:39
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_null ( const string_t name)
inlineprivate

Writes a BSON element with key name and null value.

Definition at line 9102 of file json.hpp.

9103  {
9105  }
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_object ( const typename BasicJsonType::object_t &  value)
inlineprivate
Parameters
[in]jJSON value to serialize
Precondition
j.type() == value_t::object

Definition at line 9324 of file json.hpp.

9325  {
9326  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
9327 
9328  for (const auto& el : value)
9329  {
9330  write_bson_element(el.first, el.second);
9331  }
9332 
9333  oa->write_character(to_char_type(0x00));
9334  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t &value)
Calculates the size of the BSON serialization of the given JSON-object j.
Definition: json.hpp:9309
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
void write_bson_element(const string_t &name, const BasicJsonType &j)
Serializes the JSON value j to BSON and associates it with the key name.
Definition: json.hpp:9266
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_object_entry ( const string_t name,
const typename BasicJsonType::object_t &  value 
)
inlineprivate

Writes a BSON element with key name and object value.

Definition at line 9175 of file json.hpp.

9177  {
9178  write_bson_entry_header(name, 0x03); // object
9180  }
void write_bson_object(const typename BasicJsonType::object_t &value)
Definition: json.hpp:9324
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_string ( const string_t name,
const string_t value 
)
inlineprivate

Writes a BSON element with key name and string value value.

Definition at line 9088 of file json.hpp.

9090  {
9092 
9093  write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
9094  oa->write_characters(
9095  reinterpret_cast<const CharType*>(value.c_str()),
9096  value.size() + 1);
9097  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
name
Definition: setup.py:39
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_bson_unsigned ( const string_t name,
const std::uint64_t  value 
)
inlineprivate

Writes a BSON element with key name and unsigned value.

Definition at line 9153 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, max(), and Catch::Generators::value().

9155  {
9156  if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
9157  {
9158  write_bson_entry_header(name, 0x10 /* int32 */);
9159  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
9160  }
9161  else if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
9162  {
9163  write_bson_entry_header(name, 0x12 /* int64 */);
9164  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
9165  }
9166  else
9167  {
9168  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64"));
9169  }
9170  }
name
Definition: setup.py:39
#define JSON_THROW(exception)
Definition: json.hpp:162
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
void write_bson_entry_header(const string_t &name, const std::uint8_t element_type)
Writes the given element_type and name to the output adapter.
Definition: json.hpp:9048
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_cbor ( const BasicJsonType &  j)
inline
Parameters
[in]jJSON value to serialize

Definition at line 8383 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, max(), nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

8384  {
8385  switch (j.type())
8386  {
8387  case value_t::null:
8388  {
8389  oa->write_character(to_char_type(0xF6));
8390  break;
8391  }
8392 
8393  case value_t::boolean:
8394  {
8395  oa->write_character(j.m_value.boolean
8396  ? to_char_type(0xF5)
8397  : to_char_type(0xF4));
8398  break;
8399  }
8400 
8402  {
8403  if (j.m_value.number_integer >= 0)
8404  {
8405  // CBOR does not differentiate between positive signed
8406  // integers and unsigned integers. Therefore, we used the
8407  // code from the value_t::number_unsigned case here.
8408  if (j.m_value.number_integer <= 0x17)
8409  {
8410  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8411  }
8412  else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
8413  {
8414  oa->write_character(to_char_type(0x18));
8415  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8416  }
8417  else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)())
8418  {
8419  oa->write_character(to_char_type(0x19));
8420  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8421  }
8422  else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)())
8423  {
8424  oa->write_character(to_char_type(0x1A));
8425  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8426  }
8427  else
8428  {
8429  oa->write_character(to_char_type(0x1B));
8430  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8431  }
8432  }
8433  else
8434  {
8435  // The conversions below encode the sign in the first
8436  // byte, and the value is converted to a positive number.
8437  const auto positive_number = -1 - j.m_value.number_integer;
8438  if (j.m_value.number_integer >= -24)
8439  {
8440  write_number(static_cast<uint8_t>(0x20 + positive_number));
8441  }
8442  else if (positive_number <= (std::numeric_limits<uint8_t>::max)())
8443  {
8444  oa->write_character(to_char_type(0x38));
8445  write_number(static_cast<uint8_t>(positive_number));
8446  }
8447  else if (positive_number <= (std::numeric_limits<uint16_t>::max)())
8448  {
8449  oa->write_character(to_char_type(0x39));
8450  write_number(static_cast<uint16_t>(positive_number));
8451  }
8452  else if (positive_number <= (std::numeric_limits<uint32_t>::max)())
8453  {
8454  oa->write_character(to_char_type(0x3A));
8455  write_number(static_cast<uint32_t>(positive_number));
8456  }
8457  else
8458  {
8459  oa->write_character(to_char_type(0x3B));
8460  write_number(static_cast<uint64_t>(positive_number));
8461  }
8462  }
8463  break;
8464  }
8465 
8467  {
8468  if (j.m_value.number_unsigned <= 0x17)
8469  {
8470  write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
8471  }
8472  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8473  {
8474  oa->write_character(to_char_type(0x18));
8475  write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
8476  }
8477  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8478  {
8479  oa->write_character(to_char_type(0x19));
8480  write_number(static_cast<uint16_t>(j.m_value.number_unsigned));
8481  }
8482  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8483  {
8484  oa->write_character(to_char_type(0x1A));
8485  write_number(static_cast<uint32_t>(j.m_value.number_unsigned));
8486  }
8487  else
8488  {
8489  oa->write_character(to_char_type(0x1B));
8490  write_number(static_cast<uint64_t>(j.m_value.number_unsigned));
8491  }
8492  break;
8493  }
8494 
8495  case value_t::number_float:
8496  {
8497  oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
8498  write_number(j.m_value.number_float);
8499  break;
8500  }
8501 
8502  case value_t::string:
8503  {
8504  // step 1: write control byte and the string length
8505  const auto N = j.m_value.string->size();
8506  if (N <= 0x17)
8507  {
8508  write_number(static_cast<uint8_t>(0x60 + N));
8509  }
8510  else if (N <= (std::numeric_limits<uint8_t>::max)())
8511  {
8512  oa->write_character(to_char_type(0x78));
8513  write_number(static_cast<uint8_t>(N));
8514  }
8515  else if (N <= (std::numeric_limits<uint16_t>::max)())
8516  {
8517  oa->write_character(to_char_type(0x79));
8518  write_number(static_cast<uint16_t>(N));
8519  }
8520  else if (N <= (std::numeric_limits<uint32_t>::max)())
8521  {
8522  oa->write_character(to_char_type(0x7A));
8523  write_number(static_cast<uint32_t>(N));
8524  }
8525  // LCOV_EXCL_START
8526  else if (N <= (std::numeric_limits<uint64_t>::max)())
8527  {
8528  oa->write_character(to_char_type(0x7B));
8529  write_number(static_cast<uint64_t>(N));
8530  }
8531  // LCOV_EXCL_STOP
8532 
8533  // step 2: write the string
8534  oa->write_characters(
8535  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
8536  j.m_value.string->size());
8537  break;
8538  }
8539 
8540  case value_t::array:
8541  {
8542  // step 1: write control byte and the array size
8543  const auto N = j.m_value.array->size();
8544  if (N <= 0x17)
8545  {
8546  write_number(static_cast<uint8_t>(0x80 + N));
8547  }
8548  else if (N <= (std::numeric_limits<uint8_t>::max)())
8549  {
8550  oa->write_character(to_char_type(0x98));
8551  write_number(static_cast<uint8_t>(N));
8552  }
8553  else if (N <= (std::numeric_limits<uint16_t>::max)())
8554  {
8555  oa->write_character(to_char_type(0x99));
8556  write_number(static_cast<uint16_t>(N));
8557  }
8558  else if (N <= (std::numeric_limits<uint32_t>::max)())
8559  {
8560  oa->write_character(to_char_type(0x9A));
8561  write_number(static_cast<uint32_t>(N));
8562  }
8563  // LCOV_EXCL_START
8564  else if (N <= (std::numeric_limits<uint64_t>::max)())
8565  {
8566  oa->write_character(to_char_type(0x9B));
8567  write_number(static_cast<uint64_t>(N));
8568  }
8569  // LCOV_EXCL_STOP
8570 
8571  // step 2: write each element
8572  for (const auto& el : *j.m_value.array)
8573  {
8574  write_cbor(el);
8575  }
8576  break;
8577  }
8578 
8579  case value_t::object:
8580  {
8581  // step 1: write control byte and the object size
8582  const auto N = j.m_value.object->size();
8583  if (N <= 0x17)
8584  {
8585  write_number(static_cast<uint8_t>(0xA0 + N));
8586  }
8587  else if (N <= (std::numeric_limits<uint8_t>::max)())
8588  {
8589  oa->write_character(to_char_type(0xB8));
8590  write_number(static_cast<uint8_t>(N));
8591  }
8592  else if (N <= (std::numeric_limits<uint16_t>::max)())
8593  {
8594  oa->write_character(to_char_type(0xB9));
8595  write_number(static_cast<uint16_t>(N));
8596  }
8597  else if (N <= (std::numeric_limits<uint32_t>::max)())
8598  {
8599  oa->write_character(to_char_type(0xBA));
8600  write_number(static_cast<uint32_t>(N));
8601  }
8602  // LCOV_EXCL_START
8603  else if (N <= (std::numeric_limits<uint64_t>::max)())
8604  {
8605  oa->write_character(to_char_type(0xBB));
8606  write_number(static_cast<uint64_t>(N));
8607  }
8608  // LCOV_EXCL_STOP
8609 
8610  // step 2: write each element
8611  for (const auto& el : *j.m_value.object)
8612  {
8613  write_cbor(el.first);
8614  write_cbor(el.second);
8615  }
8616  break;
8617  }
8618 
8619  default:
8620  break;
8621  }
8622  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
static constexpr CharType get_cbor_float_prefix(float)
Definition: json.hpp:9340
array (ordered collection of values)
number value (signed integer)
object (unordered set of name/value pairs)
void write_number(const NumberType n)
Definition: json.hpp:9594
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
number value (unsigned integer)
number value (floating-point)
void write_cbor(const BasicJsonType &j)
Definition: json.hpp:8383
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_msgpack ( const BasicJsonType &  j)
inline
Parameters
[in]jJSON value to serialize

Definition at line 8627 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, max(), anonymous_namespace{chameleon.cpp}::min(), nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

8628  {
8629  switch (j.type())
8630  {
8631  case value_t::null: // nil
8632  {
8633  oa->write_character(to_char_type(0xC0));
8634  break;
8635  }
8636 
8637  case value_t::boolean: // true and false
8638  {
8639  oa->write_character(j.m_value.boolean
8640  ? to_char_type(0xC3)
8641  : to_char_type(0xC2));
8642  break;
8643  }
8644 
8646  {
8647  if (j.m_value.number_integer >= 0)
8648  {
8649  // MessagePack does not differentiate between positive
8650  // signed integers and unsigned integers. Therefore, we used
8651  // the code from the value_t::number_unsigned case here.
8652  if (j.m_value.number_unsigned < 128)
8653  {
8654  // positive fixnum
8655  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8656  }
8657  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8658  {
8659  // uint 8
8660  oa->write_character(to_char_type(0xCC));
8661  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8662  }
8663  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8664  {
8665  // uint 16
8666  oa->write_character(to_char_type(0xCD));
8667  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8668  }
8669  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8670  {
8671  // uint 32
8672  oa->write_character(to_char_type(0xCE));
8673  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8674  }
8675  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
8676  {
8677  // uint 64
8678  oa->write_character(to_char_type(0xCF));
8679  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8680  }
8681  }
8682  else
8683  {
8684  if (j.m_value.number_integer >= -32)
8685  {
8686  // negative fixnum
8687  write_number(static_cast<int8_t>(j.m_value.number_integer));
8688  }
8689  else if (j.m_value.number_integer >= (std::numeric_limits<int8_t>::min)() and
8690  j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
8691  {
8692  // int 8
8693  oa->write_character(to_char_type(0xD0));
8694  write_number(static_cast<int8_t>(j.m_value.number_integer));
8695  }
8696  else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and
8697  j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
8698  {
8699  // int 16
8700  oa->write_character(to_char_type(0xD1));
8701  write_number(static_cast<int16_t>(j.m_value.number_integer));
8702  }
8703  else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and
8704  j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
8705  {
8706  // int 32
8707  oa->write_character(to_char_type(0xD2));
8708  write_number(static_cast<int32_t>(j.m_value.number_integer));
8709  }
8710  else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and
8711  j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
8712  {
8713  // int 64
8714  oa->write_character(to_char_type(0xD3));
8715  write_number(static_cast<int64_t>(j.m_value.number_integer));
8716  }
8717  }
8718  break;
8719  }
8720 
8722  {
8723  if (j.m_value.number_unsigned < 128)
8724  {
8725  // positive fixnum
8726  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8727  }
8728  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8729  {
8730  // uint 8
8731  oa->write_character(to_char_type(0xCC));
8732  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8733  }
8734  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8735  {
8736  // uint 16
8737  oa->write_character(to_char_type(0xCD));
8738  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8739  }
8740  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8741  {
8742  // uint 32
8743  oa->write_character(to_char_type(0xCE));
8744  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8745  }
8746  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
8747  {
8748  // uint 64
8749  oa->write_character(to_char_type(0xCF));
8750  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8751  }
8752  break;
8753  }
8754 
8755  case value_t::number_float:
8756  {
8757  oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
8758  write_number(j.m_value.number_float);
8759  break;
8760  }
8761 
8762  case value_t::string:
8763  {
8764  // step 1: write control byte and the string length
8765  const auto N = j.m_value.string->size();
8766  if (N <= 31)
8767  {
8768  // fixstr
8769  write_number(static_cast<uint8_t>(0xA0 | N));
8770  }
8771  else if (N <= (std::numeric_limits<uint8_t>::max)())
8772  {
8773  // str 8
8774  oa->write_character(to_char_type(0xD9));
8775  write_number(static_cast<uint8_t>(N));
8776  }
8777  else if (N <= (std::numeric_limits<uint16_t>::max)())
8778  {
8779  // str 16
8780  oa->write_character(to_char_type(0xDA));
8781  write_number(static_cast<uint16_t>(N));
8782  }
8783  else if (N <= (std::numeric_limits<uint32_t>::max)())
8784  {
8785  // str 32
8786  oa->write_character(to_char_type(0xDB));
8787  write_number(static_cast<uint32_t>(N));
8788  }
8789 
8790  // step 2: write the string
8791  oa->write_characters(
8792  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
8793  j.m_value.string->size());
8794  break;
8795  }
8796 
8797  case value_t::array:
8798  {
8799  // step 1: write control byte and the array size
8800  const auto N = j.m_value.array->size();
8801  if (N <= 15)
8802  {
8803  // fixarray
8804  write_number(static_cast<uint8_t>(0x90 | N));
8805  }
8806  else if (N <= (std::numeric_limits<uint16_t>::max)())
8807  {
8808  // array 16
8809  oa->write_character(to_char_type(0xDC));
8810  write_number(static_cast<uint16_t>(N));
8811  }
8812  else if (N <= (std::numeric_limits<uint32_t>::max)())
8813  {
8814  // array 32
8815  oa->write_character(to_char_type(0xDD));
8816  write_number(static_cast<uint32_t>(N));
8817  }
8818 
8819  // step 2: write each element
8820  for (const auto& el : *j.m_value.array)
8821  {
8822  write_msgpack(el);
8823  }
8824  break;
8825  }
8826 
8827  case value_t::object:
8828  {
8829  // step 1: write control byte and the object size
8830  const auto N = j.m_value.object->size();
8831  if (N <= 15)
8832  {
8833  // fixmap
8834  write_number(static_cast<uint8_t>(0x80 | (N & 0xF)));
8835  }
8836  else if (N <= (std::numeric_limits<uint16_t>::max)())
8837  {
8838  // map 16
8839  oa->write_character(to_char_type(0xDE));
8840  write_number(static_cast<uint16_t>(N));
8841  }
8842  else if (N <= (std::numeric_limits<uint32_t>::max)())
8843  {
8844  // map 32
8845  oa->write_character(to_char_type(0xDF));
8846  write_number(static_cast<uint32_t>(N));
8847  }
8848 
8849  // step 2: write each element
8850  for (const auto& el : *j.m_value.object)
8851  {
8852  write_msgpack(el.first);
8853  write_msgpack(el.second);
8854  }
8855  break;
8856  }
8857 
8858  default:
8859  break;
8860  }
8861  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
array (ordered collection of values)
number value (signed integer)
void write_msgpack(const BasicJsonType &j)
Definition: json.hpp:8627
object (unordered set of name/value pairs)
void write_number(const NumberType n)
Definition: json.hpp:9594
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
static constexpr CharType get_msgpack_float_prefix(float)
Definition: json.hpp:9354
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
number value (unsigned integer)
number value (floating-point)
template<typename BasicJsonType , typename CharType >
template<typename NumberType , bool OutputIsLittleEndian = false>
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number ( const NumberType  n)
inlineprivate

Definition at line 9594 of file json.hpp.

References Catch::Generators::value().

9595  {
9596  // step 1: write number to array of length NumberType
9597  std::array<CharType, sizeof(NumberType)> vec;
9598  std::memcpy(vec.data(), &n, sizeof(NumberType));
9599 
9600  // step 2: write array to output (with possible reordering)
9601  if (is_little_endian and not OutputIsLittleEndian)
9602  {
9603  // reverse byte order prior to conversion if necessary
9604  std::reverse(vec.begin(), vec.end());
9605  }
9606 
9607  oa->write_characters(vec.data(), sizeof(NumberType));
9608  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
const bool is_little_endian
whether we can assume little endianess
Definition: json.hpp:9653
template<typename BasicJsonType , typename CharType >
template<typename NumberType , typename std::enable_if< std::is_floating_point< NumberType >::value, int >::type = 0>
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number_with_ubjson_prefix ( const NumberType  n,
const bool  add_prefix 
)
inlineprivate

Definition at line 9371 of file json.hpp.

References Catch::Generators::value().

9373  {
9374  if (add_prefix)
9375  {
9376  oa->write_character(get_ubjson_float_prefix(n));
9377  }
9378  write_number(n);
9379  }
static constexpr CharType get_ubjson_float_prefix(float)
Definition: json.hpp:9568
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
void write_number(const NumberType n)
Definition: json.hpp:9594
template<typename BasicJsonType , typename CharType >
template<typename NumberType , typename std::enable_if< std::is_unsigned< NumberType >::value, int >::type = 0>
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number_with_ubjson_prefix ( const NumberType  n,
const bool  add_prefix 
)
inlineprivate

Definition at line 9384 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, max(), and Catch::Generators::value().

9386  {
9387  if (n <= static_cast<uint64_t>((std::numeric_limits<int8_t>::max)()))
9388  {
9389  if (add_prefix)
9390  {
9391  oa->write_character(to_char_type('i')); // int8
9392  }
9393  write_number(static_cast<uint8_t>(n));
9394  }
9395  else if (n <= (std::numeric_limits<uint8_t>::max)())
9396  {
9397  if (add_prefix)
9398  {
9399  oa->write_character(to_char_type('U')); // uint8
9400  }
9401  write_number(static_cast<uint8_t>(n));
9402  }
9403  else if (n <= static_cast<uint64_t>((std::numeric_limits<int16_t>::max)()))
9404  {
9405  if (add_prefix)
9406  {
9407  oa->write_character(to_char_type('I')); // int16
9408  }
9409  write_number(static_cast<int16_t>(n));
9410  }
9411  else if (n <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
9412  {
9413  if (add_prefix)
9414  {
9415  oa->write_character(to_char_type('l')); // int32
9416  }
9417  write_number(static_cast<int32_t>(n));
9418  }
9419  else if (n <= static_cast<uint64_t>((std::numeric_limits<int64_t>::max)()))
9420  {
9421  if (add_prefix)
9422  {
9423  oa->write_character(to_char_type('L')); // int64
9424  }
9425  write_number(static_cast<int64_t>(n));
9426  }
9427  else
9428  {
9429  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
9430  }
9431  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
#define JSON_THROW(exception)
Definition: json.hpp:162
void write_number(const NumberType n)
Definition: json.hpp:9594
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
template<typename BasicJsonType , typename CharType >
template<typename NumberType , typename std::enable_if< std::is_signed< NumberType >::value andnot std::is_floating_point< NumberType >::value, int >::type = 0>
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_number_with_ubjson_prefix ( const NumberType  n,
const bool  add_prefix 
)
inlineprivate

Definition at line 9437 of file json.hpp.

References nlohmann::detail::out_of_range::create(), JSON_THROW, max(), and anonymous_namespace{chameleon.cpp}::min().

9439  {
9441  {
9442  if (add_prefix)
9443  {
9444  oa->write_character(to_char_type('i')); // int8
9445  }
9446  write_number(static_cast<int8_t>(n));
9447  }
9448  else if (static_cast<int64_t>((std::numeric_limits<uint8_t>::min)()) <= n and n <= static_cast<int64_t>((std::numeric_limits<uint8_t>::max)()))
9449  {
9450  if (add_prefix)
9451  {
9452  oa->write_character(to_char_type('U')); // uint8
9453  }
9454  write_number(static_cast<uint8_t>(n));
9455  }
9457  {
9458  if (add_prefix)
9459  {
9460  oa->write_character(to_char_type('I')); // int16
9461  }
9462  write_number(static_cast<int16_t>(n));
9463  }
9465  {
9466  if (add_prefix)
9467  {
9468  oa->write_character(to_char_type('l')); // int32
9469  }
9470  write_number(static_cast<int32_t>(n));
9471  }
9473  {
9474  if (add_prefix)
9475  {
9476  oa->write_character(to_char_type('L')); // int64
9477  }
9478  write_number(static_cast<int64_t>(n));
9479  }
9480  // LCOV_EXCL_START
9481  else
9482  {
9483  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
9484  }
9485  // LCOV_EXCL_STOP
9486  }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
#define JSON_THROW(exception)
Definition: json.hpp:162
void write_number(const NumberType n)
Definition: json.hpp:9594
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:1070
template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_ubjson ( const BasicJsonType &  j,
const bool  use_count,
const bool  use_type,
const bool  add_prefix = true 
)
inline
Parameters
[in]jJSON value to serialize
[in]use_countwhether to use '#' prefixes (optimized format)
[in]use_typewhether to use '$' prefixes (optimized format)
[in]add_prefixwhether prefixes need to be used for this value

Definition at line 8869 of file json.hpp.

References nlohmann::detail::array, nlohmann::detail::boolean, nlohmann::detail::null, nlohmann::detail::number_float, nlohmann::detail::number_integer, nlohmann::detail::number_unsigned, nlohmann::detail::object, and nlohmann::detail::string.

8871  {
8872  switch (j.type())
8873  {
8874  case value_t::null:
8875  {
8876  if (add_prefix)
8877  {
8878  oa->write_character(to_char_type('Z'));
8879  }
8880  break;
8881  }
8882 
8883  case value_t::boolean:
8884  {
8885  if (add_prefix)
8886  {
8887  oa->write_character(j.m_value.boolean
8888  ? to_char_type('T')
8889  : to_char_type('F'));
8890  }
8891  break;
8892  }
8893 
8895  {
8896  write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
8897  break;
8898  }
8899 
8901  {
8902  write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
8903  break;
8904  }
8905 
8906  case value_t::number_float:
8907  {
8908  write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
8909  break;
8910  }
8911 
8912  case value_t::string:
8913  {
8914  if (add_prefix)
8915  {
8916  oa->write_character(to_char_type('S'));
8917  }
8918  write_number_with_ubjson_prefix(j.m_value.string->size(), true);
8919  oa->write_characters(
8920  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
8921  j.m_value.string->size());
8922  break;
8923  }
8924 
8925  case value_t::array:
8926  {
8927  if (add_prefix)
8928  {
8929  oa->write_character(to_char_type('['));
8930  }
8931 
8932  bool prefix_required = true;
8933  if (use_type and not j.m_value.array->empty())
8934  {
8935  assert(use_count);
8936  const CharType first_prefix = ubjson_prefix(j.front());
8937  const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
8938  [this, first_prefix](const BasicJsonType & v)
8939  {
8940  return ubjson_prefix(v) == first_prefix;
8941  });
8942 
8943  if (same_prefix)
8944  {
8945  prefix_required = false;
8946  oa->write_character(to_char_type('$'));
8947  oa->write_character(first_prefix);
8948  }
8949  }
8950 
8951  if (use_count)
8952  {
8953  oa->write_character(to_char_type('#'));
8954  write_number_with_ubjson_prefix(j.m_value.array->size(), true);
8955  }
8956 
8957  for (const auto& el : *j.m_value.array)
8958  {
8959  write_ubjson(el, use_count, use_type, prefix_required);
8960  }
8961 
8962  if (not use_count)
8963  {
8964  oa->write_character(to_char_type(']'));
8965  }
8966 
8967  break;
8968  }
8969 
8970  case value_t::object:
8971  {
8972  if (add_prefix)
8973  {
8974  oa->write_character(to_char_type('{'));
8975  }
8976 
8977  bool prefix_required = true;
8978  if (use_type and not j.m_value.object->empty())
8979  {
8980  assert(use_count);
8981  const CharType first_prefix = ubjson_prefix(j.front());
8982  const bool same_prefix = std::all_of(j.begin(), j.end(),
8983  [this, first_prefix](const BasicJsonType & v)
8984  {
8985  return ubjson_prefix(v) == first_prefix;
8986  });
8987 
8988  if (same_prefix)
8989  {
8990  prefix_required = false;
8991  oa->write_character(to_char_type('$'));
8992  oa->write_character(first_prefix);
8993  }
8994  }
8995 
8996  if (use_count)
8997  {
8998  oa->write_character(to_char_type('#'));
8999  write_number_with_ubjson_prefix(j.m_value.object->size(), true);
9000  }
9001 
9002  for (const auto& el : *j.m_value.object)
9003  {
9004  write_number_with_ubjson_prefix(el.first.size(), true);
9005  oa->write_characters(
9006  reinterpret_cast<const CharType*>(el.first.c_str()),
9007  el.first.size());
9008  write_ubjson(el.second, use_count, use_type, prefix_required);
9009  }
9010 
9011  if (not use_count)
9012  {
9013  oa->write_character(to_char_type('}'));
9014  }
9015 
9016  break;
9017  }
9018 
9019  default:
9020  break;
9021  }
9022  }
void write_number_with_ubjson_prefix(const NumberType n, const bool add_prefix)
Definition: json.hpp:9371
output_adapter_t< CharType > oa
the output
Definition: json.hpp:9656
array (ordered collection of values)
number value (signed integer)
object (unordered set of name/value pairs)
static constexpr CharType to_char_type(std::uint8_t x) noexcept
Definition: json.hpp:9617
void write_ubjson(const BasicJsonType &j, const bool use_count, const bool use_type, const bool add_prefix=true)
Definition: json.hpp:8869
number value (unsigned integer)
CharType ubjson_prefix(const BasicJsonType &j) const noexcept
determine the type prefix of container values
Definition: json.hpp:9497
number value (floating-point)

Member Data Documentation

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

whether we can assume little endianess

Definition at line 9653 of file json.hpp.

template<typename BasicJsonType , typename CharType >
output_adapter_t<CharType> nlohmann::detail::binary_writer< BasicJsonType, CharType >::oa = nullptr
private

the output

Definition at line 9656 of file json.hpp.


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