You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum -> string during serialization and vice versa #455
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
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.
I think it makes no sense to add code to this library to generate nicer serialization/deserialization for enums defined with BETTER_ENUM. And after all, if a user is using such enums, she is perfectly able to write nicer to_json/from_json functions.
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 BETTER_ENUM)
Activity
TurpentineDistillery commentedon Feb 15, 2017
Perhaps this is what you want? https://github.com/nlohmann/json#arbitrary-types-conversions
nlohmann commentedon Feb 15, 2017
I am not sure whether the conversion from enum values to strings can be done after the enum has been defined. Can it?
nlohmann commentedon Feb 22, 2017
Any idea on this?
fnc12 commentedon Feb 22, 2017
I think one can use
adl_serializerto encode enumnlohmann commentedon Feb 22, 2017
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.
fnc12 commentedon Feb 22, 2017
Yes. With
BETTER_ENUMlib it can be done like this:nlohmann commentedon Feb 22, 2017
Right, but for this, you must define
countrywith a macro defined in better enum.fnc12 commentedon Feb 22, 2017
It's ok. But there is another problem - better enum value has no default constructor
nlohmann commentedon Feb 22, 2017
I think it makes no sense to add code to this library to generate nicer serialization/deserialization for enums defined with
BETTER_ENUM. And after all, if a user is using such enums, she is perfectly able to write nicerto_json/from_jsonfunctions.fnc12 commentedon Feb 22, 2017
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
BETTER_ENUM)nlohmann commentedon Feb 22, 2017
Alright. I think I can close this issue as there is nothing to be done in the library.