Skip to content

Enum -> string during serialization and vice versa #455

Closed

Description

@thijse

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!

Activity

  1. TurpentineDistillery commented on Feb 15, 2017

    @TurpentineDistillery
  2. nlohmann commented on Feb 15, 2017

    @nlohmann
    Owner

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

  3. added
    state: please discuss
    on Feb 15, 2017
  4. nlohmann commented on Feb 22, 2017

    @nlohmann
    Owner

    Any idea on this?

  5. fnc12 commented on Feb 22, 2017

    @fnc12

    I think one can use adl_serializer to encode enum

  6. nlohmann commented on Feb 22, 2017

    @nlohmann
    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.

  7. fnc12 commented on Feb 22, 2017

    @fnc12

    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();
    }
  8. nlohmann commented on Feb 22, 2017

    @nlohmann
    Owner

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

  9. fnc12 commented on Feb 22, 2017

    @fnc12

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

  10. nlohmann commented on Feb 22, 2017

    @nlohmann
    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.

  11. fnc12 commented on Feb 22, 2017

    @fnc12

    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)

  12. removed
    state: please discuss
    on Feb 22, 2017
  13. nlohmann commented on Feb 22, 2017

    @nlohmann
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions