Open askr0x123456789 opened 5 days ago
You try to find an enum value for the string "yellow". The documentation states:
When using template get
() , undefined JSON values will default to the first specified conversion. Select this default pair carefully. See example 1 below.
Therefore, Color::Red
is returned.
Unless I misunderstood the issue, I would say that the library behaves as documented.
Shouldn't the correct behavior be throwing an exception?
I'm reading millions of records from a database and the available string value might be wrong. If I'm not sure about the string values, converting a string to enum, is better be done by if statements.
I understand. The current macro behaves as documented, and changing it would break existing code. One possibility would be adding a throwing version of the macro.
Description
nlohmann::json conversion from invalid json to scoped enumeration works without exceptions.
Reproduction steps
1- define a scoped enumeration 2- use
NLOHMANN_JSON_SERIALIZE_ENUM
for convertion from string to the enum type 3- convert an invalid string to the enum typeExpected vs. actual results
Actual results:
Minimal code example
Error messages
Compiler and operating system
g++-14 on Kali Linux 2024.3
Library version
latest from develop branch
Validation
develop
branch is used.