Skip to content

Upgrading to 3.x: to_/from_json with enum class #1093

@endorph-soft

Description

@endorph-soft

I'm in the process of updating a project from version 2.1.1 to the version 3.x series.

I have the following two functions defined to handle enum classes:

template <typename JSON, typename Enum>
void to_json(JSON & j, Enum e) {
  j = ToString(e);
}

template <typename JSON, typename Enum>
void from_json(const JSON & j, Enum & e) {
  if (!FromString(e, j)) {
    throw std::invalid_argument("Badness...");
  }
}

There are definitions of ToString and FromString for all the enumerations of interest. I'm not using SFINAE to restrict Enum to enumerations, because it's not currently needed. With version 2.1.1, everything is working fine.

With the update to 3.x, these functions are no longer found by the lookup. If I comment out the new enum to_json and from_json inside the library, the errors disappear, and all is well again.

How can I make my functions work? Do I need a more specific signature? I don't want to define a function for every enumeration, if I can get away with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions