Skip to content

Commit

Permalink
Create fc::json::json_integer_limits enum and use it instead of const…
Browse files Browse the repository at this point in the history
…ants.
  • Loading branch information
asuch committed Oct 17, 2023
1 parent b2fd7a7 commit 79f89b0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 6 additions & 0 deletions libraries/fc/include/fc/io/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ namespace fc
stringify_large_ints_and_doubles = 0,
legacy_generator = 1
};
// Javascript needs to use bigint type for values which exceeses below limits
enum json_integer_limits : int64_t
{
max_negative_value = -0x1fffffffffffff,
max_positive_value = 0x1fffffffffffff
};

static ostream& to_stream( ostream& out, const fc::string& );
static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles );
Expand Down
13 changes: 3 additions & 10 deletions libraries/fc/src/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,10 @@ namespace fc
return;
case variant::int64_type:
{
// Javascript needs to use bigint type for values which exceeses below limits,
// so in case if v exceeses, return it as string.
constexpr int64_t json_max_int_value_limit = 0x1fffffffffffff;
constexpr int64_t json_min_int_value_limit = -0x1fffffffffffff;

int64_t i = v.as_int64();
if( format == json::stringify_large_ints_and_doubles &&
(i > json_max_int_value_limit ||
i < json_min_int_value_limit ))
(i > json::json_integer_limits::max_positive_value ||
i < json::json_integer_limits::max_negative_value ))
os << '"'<<v.as_string()<<'"';
else
os << i;
Expand All @@ -708,11 +703,9 @@ namespace fc
}
case variant::uint64_type:
{
constexpr uint64_t json_max_int_value_limit = 0x1fffffffffffff;

uint64_t i = v.as_uint64();
if( format == json::stringify_large_ints_and_doubles &&
i > json_max_int_value_limit )
i > static_cast<uint64_t>(json::json_integer_limits::max_positive_value))
os << '"'<<v.as_string()<<'"';
else
os << i;
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,10 @@ BOOST_AUTO_TEST_CASE( fc_optional_alignment )
BOOST_AUTO_TEST_CASE( fc_json_integer_formatting )
{
{
constexpr int64_t json_max_int_value_limit = 0x1fffffffffffff;
constexpr int64_t json_min_int_value_limit = -0x1fffffffffffff;
const fc::variant v(std::vector<int64_t>{json_max_int_value_limit + 1, json_min_int_value_limit - 1, json_max_int_value_limit, json_min_int_value_limit});
const fc::variant v(std::vector<int64_t>{ fc::json::json_integer_limits::max_positive_value + 1,
fc::json::json_integer_limits::max_negative_value - 1,
fc::json::json_integer_limits::max_positive_value,
fc::json::json_integer_limits::max_negative_value});

const string legacy_generator_formating_pattern = "[9007199254740992,-9007199254740992,9007199254740991,-9007199254740991]";
BOOST_CHECK_EQUAL(fc::json::to_string(v, fc::json::output_formatting::legacy_generator), legacy_generator_formating_pattern);
Expand All @@ -852,8 +853,7 @@ BOOST_AUTO_TEST_CASE( fc_json_integer_formatting )
}

{
constexpr uint64_t javascript_max_uint_value_limit = 0x1fffffffffffff;
const fc::variant v(std::vector<uint64_t>{javascript_max_uint_value_limit + 1, javascript_max_uint_value_limit});
const fc::variant v(std::vector<uint64_t>{fc::json::json_integer_limits::max_positive_value + 1, fc::json::json_integer_limits::max_positive_value});

const string legacy_generator_formating_pattern = "[9007199254740992,9007199254740991]";
BOOST_CHECK_EQUAL(fc::json::to_string(v, fc::json::output_formatting::legacy_generator), legacy_generator_formating_pattern);
Expand Down

0 comments on commit 79f89b0

Please sign in to comment.