Skip to content

Commit

Permalink
Flip order of is_number_integer/is_number_unsigned in from json
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwyn authored and martinRenou committed Aug 24, 2022
1 parent 1b6f6d8 commit c2463f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/pybind11_json/pybind11_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ namespace pyjson
{
return py::bool_(j.get<bool>());
}
else if (j.is_number_integer())
{
return py::int_(j.get<nl::json::number_integer_t>());
}
else if (j.is_number_unsigned())
{
return py::int_(j.get<nl::json::number_unsigned_t>());
}
else if (j.is_number_integer())
{
return py::int_(j.get<nl::json::number_integer_t>());
}
else if (j.is_number_float())
{
return py::float_(j.get<double>());
Expand Down
21 changes: 21 additions & 0 deletions test/test_pybind11_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,27 @@ TEST(nljson_serializers_fromjson, integer)
ASSERT_EQ(obj2.cast<int>(), 36);
}

TEST(nljson_serializers_fromjson, integer_large_unsigned)
{
// Note: if the asserts below error, the large number is printed as "-1" with
// an overflow error. This is only in the output step in pybind. Calling
// py::print on the objects shows the correct large unsigned integer.

py::scoped_interpreter guard;
uint64_t original = 13625394757606569013ull;
py::int_ py_orig = original;
nl::json j = original;
py::object obj = j;

ASSERT_TRUE(py::isinstance<py::int_>(obj));
ASSERT_EQ(obj.cast<uint64_t>(), original);

py::int_ obj2 = j;

// Use .equal to compare values not pointers
ASSERT_TRUE(obj2.equal(py_orig));
}

TEST(nljson_serializers_fromjson, float_)
{
py::scoped_interpreter guard;
Expand Down

0 comments on commit c2463f9

Please sign in to comment.