-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NLOHMANN_JSON_SERIALIZE_ENUM defaulting to the first enum value is surprising behavior #3992
Comments
I removed the bug label as this is documented behavior. We cannot change this behavior as it would be a breaking change. I could think of a different macro to achieve this behavior though. |
I agree. I think there is definitely a legitimate usecase for the current behavior for weakly typed enums which might be commonly used with implicit (un-named) enum values, for example: enum State {
INIT = 0
SETUP = 1
STEP = 2
// bunch of implicit steps
DONE = 10
};
if (step == State::STEP + 4) ... It's less likely someone would be using an
It's almost like the current Either way I'd be very eager to use a new more strict macro! |
Some potential naming options for a new enum courtesy of chatGPT
|
I like |
just opened a MR for |
@pabloariasal why was #3996 closed? I just ran into this issue again and am looking forward to getting your PR merged. |
Same here. |
Same |
Any update on this? @pabloariasal |
When could I expect this feature, would be great to have it. |
Description
I had a json document with an invalid enum string. I would like an exception to be raised if the string is not a known conversion back to the enum. I don't want to have to add and check an
Unknown
orInvalid
enum value which would only be used for json serialization.This is well documented: "undefined JSON values will default to the first specified conversion." but is IMO is surprising behavior since we throw
json.exception.type_error
's for other type conversions if they fail (like converting an integer in the json document to astd::string
)This is maybe only a good idea for strongly typed
enum class
's.Reproduction steps
Expected vs. actual results
Expected: It prints 'invalid color'
Actual:
color
gets set toColor::red
I believe the behavior I am after can be achieved by with this patch
Minimal code example
No response
Error messages
No response
Compiler and operating system
clang 15.0.7
Library version
v3.11.2
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: