Skip to content
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

Closed
thijse opened this issue Feb 15, 2017 · 11 comments
Closed

Enum -> string during serialization and vice versa #455

thijse opened this issue Feb 15, 2017 · 11 comments

Comments

@thijse
Copy link

thijse commented Feb 15, 2017

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!

@TurpentineDistillery
Copy link

Perhaps this is what you want? https://github.com/nlohmann/json#arbitrary-types-conversions

@nlohmann
Copy link
Owner

I am not sure whether the conversion from enum values to strings can be done after the enum has been defined. Can it?

@nlohmann nlohmann added kind: enhancement/improvement state: please discuss please discuss the issue or vote for your favorite option labels Feb 15, 2017
@nlohmann
Copy link
Owner

Any idea on this?

@fnc12
Copy link

fnc12 commented Feb 22, 2017

I think one can use adl_serializer to encode enum

@nlohmann
Copy link
Owner

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
Copy link

fnc12 commented Feb 22, 2017

Yes. With BETTER_ENUM lib it can be done like this:

template <typename BasicJsonType>
void to_json(BasicJsonType& j, country c)
{
    j = c._to_string();
}

@nlohmann
Copy link
Owner

Right, but for this, you must define country with a macro defined in better enum.

@fnc12
Copy link

fnc12 commented Feb 22, 2017

It's ok. But there is another problem - better enum value has no default constructor

@nlohmann
Copy link
Owner

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.

@fnc12
Copy link

fnc12 commented 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 nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Feb 22, 2017
@nlohmann
Copy link
Owner

Alright. I think I can close this issue as there is nothing to be done in the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants