-
-
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
Enum -> string during serialization and vice versa #455
Comments
Perhaps this is what you want? https://github.com/nlohmann/json#arbitrary-types-conversions |
I am not sure whether the conversion from enum values to strings can be done after the enum has been defined. Can it? |
Any idea on this? |
I think one can use |
Of course it can - currently, it is done like this: https://github.com/nlohmann/json/blob/develop/test/src/unit-udt.cpp#L100 I just don't think we can do it any nicer without touching the original enum. As far as I understand, there is no way to get any string representation from an enum. |
Yes. With template <typename BasicJsonType>
void to_json(BasicJsonType& j, country c)
{
j = c._to_string();
} |
Right, but for this, you must define |
It's ok. But there is another problem - better enum value has no default constructor |
I think it makes no sense to add code to this library to generate nicer serialization/deserialization for enums defined with |
Yep. This lib gives user an ability to provide a custom type mapper and it is a responsibility of a developer to create enum mapper for example (or use mapped from external libraries like |
Alright. I think I can close this issue as there is nothing to be done in the library. |
A feature request:
Include enum to string functionality for improved readability. For example, integrate
https://github.com/aantron/better-enums
I now use this to convert my enums into strings before putting it in a serialization object, but it would be much cleaner if this was integrated in the serialization code
Thanks for the great library!
The text was updated successfully, but these errors were encountered: