Skip to content

NLOHMANN_JSON_SERIALIZE_ENUM potentially requires duplicate definitions #1450

Description

@jasonbeach

As many others have said this is a pretty fantastic library.
I'm trying to serialize some enums and this works (with the right to_json and from_json functions defined but not shown here):

struct Foo_t
{
  enum class color
  { 
      none = -1,
      black = 0,
      red,
      blue,
      green
  };
  int number{0};
  color foo_color{color::none};
};

NLOHMANN_JSON_SERIALIZE_ENUM(Foo_t::color,
  {
    {Foo_t::color::none, nullptr},
    {Foo_t::color::black, "black"},
    {Foo_t::color::red, "red"},
    {Foo_t::color::blue, "blue"},
    {Foo_t::color::green, "green"}
  })

however if I want to use those string definitions independent of json then I have to define another mapping, something like

static const std::unordered_map<Foo_t::color, const std::string> color_map = 
{
    {Foo_t::color::none, ""},
    {Foo_t::color::black, "black"},
    {Foo_t::color::red, "red"},
    {Foo_t::color::blue, "blue"},
    {Foo_t::color::green, "green"}
};

and unfortunately this doesn't work:

NLOHMANN_JSON_SERIALIZE_ENUM(Foo_t::color, color_map)

because the macro is expecting something that can be converted to an array of std::pairs.

Is there way to not have to duplicate the definitions?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions