nlohmann / json

JSON for Modern C++

Home Page:https://json.nlohmann.me

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`to_json` is erroneously converting enums with underlying unsigned types to signed numbers

TheJCAB opened this issue · comments

Description

In modern C++, enum types can have a user-specified integral underlying type. This type can be signed or unsigned. We use this in Microsoft's TTD in a number of places, where we need stronger-typed integral-like types.

We've been trying this JSON serializer, and encountered this issue in some of our enums that use unsigned underlying types and "special values" in the high range. Those are written out as signed so for instance the JSON file will have -2 instead of 0xFFFFFFFFFFFFFFFE or 18446744073709551614. No data is technically lost, but this can cause failures in some deserializers.

Because all numbers are internally stored as 64-bit, this is most visible with enums that are unsigned 64-bit integers like uint64_t.

Reproduction steps

I've reproduced this in the unit tests like so:

enum class book_id : uint64_t;

...
    const udt::book_id large_id{udt::book_id(uint64_t(1) << 63)};
...
        CHECK(json(large_id) > 0u);
        CHECK(to_string(json(large_id)) == "9223372036854775808");
        CHECK(json(large_id).is_number_unsigned());

Those three checks fail. The first one fails because the json object contains a negative 64-bit number. The second one fails because it is serialized as a negative number so the string comparison fails, and the third one is the direct check to verify that the underlying number is, indeed, unsigned like the original enum was.

Expected vs. actual results

Serializing unsigned values (including enums with underlying unsigned types) should write positive numbers to the JSON file. Instead, enums with large unsigned values get written out as negative numbers.

Minimal code example

See repro steps for the code.

Error messages

/home/jcab/nlohmann/json/tests/src/unit-udt.cpp:257: ERROR: CHECK( json(large_id) > 0u ) is NOT correct!
  values: CHECK( -9223372036854775808 >  0 )

/home/jcab/nlohmann/json/tests/src/unit-udt.cpp:258: ERROR: CHECK( to_string(json(large_id)) == "9223372036854775808" ) is NOT correct!
  values: CHECK( -9223372036854775808 == 9223372036854775808 )

/home/jcab/nlohmann/json/tests/src/unit-udt.cpp:259: ERROR: CHECK( json(large_id).is_number_unsigned() ) is NOT correct!
  values: CHECK( false )

Compiler and operating system

Microsoft Visual C++, Visual Studio 2022 17.9 preview, C++20
clang version 14.0.0-1ubuntu1.1

Library version

Used via vcpkg, with commit 0ca0fe433eb70cea0d5761079c0c5b47b736565b
Reproduced on the develop branch.

Validation