-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Allow disabling default enum conversions #3536
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add JSON_NO_ENUM
to detail/macro_scope.hpp
and detail/macro_unscope.hpp
.
Added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this flag should be more descriptive, such as JSON_DISABLE_ENUM_SERIALIZATION
.
Sure, I don't mind either way, I think more descriptive names are better but I was trying to stick with some sort of convention I found in the code already, as mentioned in original comment, I made this similarly to Should I change it to |
@falbrechtskirchinger @nlohmann Thoughts on naming? It's not a huge deal for me, just trying to avoid future user confusion. |
I agree that a more descriptive name is preferable. |
I thought about |
Agreed. Let's go with And for future consideration: |
Makes sense to me. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've been quite nitpicky when reviewing documentation lately. By that standard, there're a few issues but nothing major. If anyone else requests changes, I'll add mine.
Otherwise, looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add my suggestions as well.
I messed up by doing the review from two tabs and not noticing. Hopefully, I fixed everything.
Applied all the changes, I agree it is much clearer and easier to digest. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing.
Some day we should strictly define what the terms "parsing", "serializing", "deserializing", and "type conversion" mean and how they relate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
|
This PR adds define
JSON_NO_ENUM
which works in similar vein asJSON_NO_IO
. WhenJSON_NO_ENUM
is defined it disables defaultfrom_json
/to_json
functions that convert the enum to underlying integer value and serialize it. This means if this is defined you have to provide user-defined conversion for enums (either by providingfrom_json
/to_json
pairs, usingNLOHMANN_JSON_SERIALIZE_ENUM
or specializingadl_serializer
) if you want to serialize them, otherwise you get compiler error.This does not change default behavior of this library, meaning
JSON_NO_ENUM
is not defined by default, retaining backwards compatibility, users have to opt-in.Motivation
In our project we serialize all enums to json automatically by using the popular magic_enum library. This is done by using
adl_serializer
that is specialized for all enums, which then usesmagic_enum
to serialize enum variants. This piece of code needs to be included in order for the enums to get serialized properly, if we fail to do so, the enums get implicitly serialized to integer, leading to silent failures.This change fixes that by turning all these use cases into compiler errors, allowing us to quickly fix this.
I thought this would benefit more people which use the recently introduced
NLOHMANN_JSON_SERIALIZE_ENUM
as it goes quite nicely into ensuring that you covered all your enum conversions.I only briefly skimmed through issues and PRs, sorry if this has already been brought up in some way or form.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.