diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 10183262..1b15bdaf 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -704,116 +704,126 @@ class binary_writer oa->write_characters(vec.data(), sizeof(NumberType)); } - template + // UBJSON: write number (floating point) + template::value, int>::type = 0> void write_number_with_ubjson_prefix(const NumberType n, const bool add_prefix) { - if (std::is_floating_point::value) + if (add_prefix) + { + oa->write_character(static_cast('D')); // float64 + } + write_number(n); + } + + // UBJSON: write number (unsigned integer) + template::value, int>::type = 0> + void write_number_with_ubjson_prefix(const NumberType n, + const bool add_prefix) + { + if (n <= static_cast((std::numeric_limits::max)())) { if (add_prefix) { - oa->write_character(static_cast('D')); // float64 + oa->write_character(static_cast('i')); // int8 } - write_number(n); + write_number(static_cast(n)); } - else if (std::is_unsigned::value) + else if (n <= (std::numeric_limits::max)()) { - if (n <= (std::numeric_limits::max)()) + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('i')); // int8 - } - write_number(static_cast(n)); + oa->write_character(static_cast('U')); // uint8 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('U')); // uint8 - } - write_number(static_cast(n)); + oa->write_character(static_cast('I')); // int16 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('I')); // int16 - } - write_number(static_cast(n)); + oa->write_character(static_cast('l')); // int32 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('l')); // int32 - } - write_number(static_cast(n)); - } - else if (n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('L')); // int64 - } - write_number(static_cast(n)); - } - else - { - JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); + oa->write_character(static_cast('L')); // int64 } + write_number(static_cast(n)); } else { - if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('i')); // int8 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('U')); // uint8 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('I')); // int16 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('l')); // int32 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('L')); // int64 - } - write_number(static_cast(n)); - } - // LCOV_EXCL_START - else - { - JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); - } - // LCOV_EXCL_STOP + JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); } } + // UBJSON: write number (signed integer) + template::value and + not std::is_floating_point::value, int>::type = 0> + void write_number_with_ubjson_prefix(const NumberType n, + const bool add_prefix) + { + if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('i')); // int8 + } + write_number(static_cast(n)); + } + else if (static_cast((std::numeric_limits::min)()) <= n and n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) + { + oa->write_character(static_cast('U')); // uint8 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('I')); // int16 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('l')); // int32 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('L')); // int64 + } + write_number(static_cast(n)); + } + // LCOV_EXCL_START + else + { + JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); + } + // LCOV_EXCL_STOP + } + /*! @brief determine the type prefix of container values diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4154fec0..5ef88282 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6909,116 +6909,126 @@ class binary_writer oa->write_characters(vec.data(), sizeof(NumberType)); } - template + // UBJSON: write number (floating point) + template::value, int>::type = 0> void write_number_with_ubjson_prefix(const NumberType n, const bool add_prefix) { - if (std::is_floating_point::value) + if (add_prefix) + { + oa->write_character(static_cast('D')); // float64 + } + write_number(n); + } + + // UBJSON: write number (unsigned integer) + template::value, int>::type = 0> + void write_number_with_ubjson_prefix(const NumberType n, + const bool add_prefix) + { + if (n <= static_cast((std::numeric_limits::max)())) { if (add_prefix) { - oa->write_character(static_cast('D')); // float64 + oa->write_character(static_cast('i')); // int8 } - write_number(n); + write_number(static_cast(n)); } - else if (std::is_unsigned::value) + else if (n <= (std::numeric_limits::max)()) { - if (n <= (std::numeric_limits::max)()) + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('i')); // int8 - } - write_number(static_cast(n)); + oa->write_character(static_cast('U')); // uint8 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('U')); // uint8 - } - write_number(static_cast(n)); + oa->write_character(static_cast('I')); // int16 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('I')); // int16 - } - write_number(static_cast(n)); + oa->write_character(static_cast('l')); // int32 } - else if (n <= (std::numeric_limits::max)()) + write_number(static_cast(n)); + } + else if (n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) { - if (add_prefix) - { - oa->write_character(static_cast('l')); // int32 - } - write_number(static_cast(n)); - } - else if (n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('L')); // int64 - } - write_number(static_cast(n)); - } - else - { - JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); + oa->write_character(static_cast('L')); // int64 } + write_number(static_cast(n)); } else { - if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('i')); // int8 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('U')); // uint8 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('I')); // int16 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('l')); // int32 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(static_cast('L')); // int64 - } - write_number(static_cast(n)); - } - // LCOV_EXCL_START - else - { - JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); - } - // LCOV_EXCL_STOP + JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); } } + // UBJSON: write number (signed integer) + template::value and + not std::is_floating_point::value, int>::type = 0> + void write_number_with_ubjson_prefix(const NumberType n, + const bool add_prefix) + { + if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('i')); // int8 + } + write_number(static_cast(n)); + } + else if (static_cast((std::numeric_limits::min)()) <= n and n <= static_cast((std::numeric_limits::max)())) + { + if (add_prefix) + { + oa->write_character(static_cast('U')); // uint8 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('I')); // int16 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('l')); // int32 + } + write_number(static_cast(n)); + } + else if ((std::numeric_limits::min)() <= n and n <= (std::numeric_limits::max)()) + { + if (add_prefix) + { + oa->write_character(static_cast('L')); // int64 + } + write_number(static_cast(n)); + } + // LCOV_EXCL_START + else + { + JSON_THROW(out_of_range::create(407, "number overflow serializing " + std::to_string(n))); + } + // LCOV_EXCL_STOP + } + /*! @brief determine the type prefix of container values