From 9d415f0513f7b34daf4fadaf96b138d23ae850e6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 30 Sep 2026 07:33:12 +0200 Subject: [PATCH] Cast number_integer to number_unsigned_t only once in to_msgpack() Addresses review comment by @gregmarr. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/binary_writer.hpp | 9 +++++---- single_include/nlohmann/json.hpp | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index d9b9fa521..4f39f0537 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -337,24 +337,25 @@ class binary_writer // MessagePack does not differentiate between positive // signed integers and unsigned integers. Therefore, we used // the code from the value_t::number_unsigned case here. - if (static_cast(j.m_data.m_value.number_integer) < 128) + const auto value_as_unsigned = static_cast(j.m_data.m_value.number_integer); + if (value_as_unsigned < 128) { // positive fixnum write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 8 oa.write_character(to_char_type(0xCC)); write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 16 oa.write_character(to_char_type(0xCD)); write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 32 oa.write_character(to_char_type(0xCE)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ee9c86afd..937a08782 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20667,24 +20667,25 @@ class binary_writer // MessagePack does not differentiate between positive // signed integers and unsigned integers. Therefore, we used // the code from the value_t::number_unsigned case here. - if (static_cast(j.m_data.m_value.number_integer) < 128) + const auto value_as_unsigned = static_cast(j.m_data.m_value.number_integer); + if (value_as_unsigned < 128) { // positive fixnum write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 8 oa.write_character(to_char_type(0xCC)); write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 16 oa.write_character(to_char_type(0xCD)); write_number(static_cast(j.m_data.m_value.number_integer)); } - else if (static_cast(j.m_data.m_value.number_integer) <= (std::numeric_limits::max)()) + else if (value_as_unsigned <= (std::numeric_limits::max)()) { // uint 32 oa.write_character(to_char_type(0xCE));