Skip to content

Commit c2463f9

Browse files
pengwynmartinRenou
authored andcommitted
Flip order of is_number_integer/is_number_unsigned in from json
1 parent 1b6f6d8 commit c2463f9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

include/pybind11_json/pybind11_json.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ namespace pyjson
3131
{
3232
return py::bool_(j.get<bool>());
3333
}
34-
else if (j.is_number_integer())
35-
{
36-
return py::int_(j.get<nl::json::number_integer_t>());
37-
}
3834
else if (j.is_number_unsigned())
3935
{
4036
return py::int_(j.get<nl::json::number_unsigned_t>());
4137
}
38+
else if (j.is_number_integer())
39+
{
40+
return py::int_(j.get<nl::json::number_integer_t>());
41+
}
4242
else if (j.is_number_float())
4343
{
4444
return py::float_(j.get<double>());

test/test_pybind11_json.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,27 @@ TEST(nljson_serializers_fromjson, integer)
315315
ASSERT_EQ(obj2.cast<int>(), 36);
316316
}
317317

318+
TEST(nljson_serializers_fromjson, integer_large_unsigned)
319+
{
320+
// Note: if the asserts below error, the large number is printed as "-1" with
321+
// an overflow error. This is only in the output step in pybind. Calling
322+
// py::print on the objects shows the correct large unsigned integer.
323+
324+
py::scoped_interpreter guard;
325+
uint64_t original = 13625394757606569013ull;
326+
py::int_ py_orig = original;
327+
nl::json j = original;
328+
py::object obj = j;
329+
330+
ASSERT_TRUE(py::isinstance<py::int_>(obj));
331+
ASSERT_EQ(obj.cast<uint64_t>(), original);
332+
333+
py::int_ obj2 = j;
334+
335+
// Use .equal to compare values not pointers
336+
ASSERT_TRUE(obj2.equal(py_orig));
337+
}
338+
318339
TEST(nljson_serializers_fromjson, float_)
319340
{
320341
py::scoped_interpreter guard;

0 commit comments

Comments
 (0)