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

NLOHMANN_JSON_SERIALIZE_ENUM potentially requires duplicate definitions #1450

Closed
jasonbeach opened this issue Jan 21, 2019 · 3 comments
Closed
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@jasonbeach
Copy link

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?

@jasonbeach
Copy link
Author

I came up with a partial solution. In the macro, replace both lines:
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
with
static const std::vector<std::pair<ENUM_TYPE, std::string> > m = __VA_ARGS__;

as well as changing BasicJsonType to std::string in the lambda.

@stale
Copy link

stale bot commented Feb 22, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 22, 2019
@stale stale bot closed this as completed Mar 1, 2019
@paexter
Copy link

paexter commented Sep 30, 2020

I would like to use NLOHMANN_JSON_SERIALIZE_ENUM in combination with magic_enum https://github.com/Neargye/magic_enum as follows:

NLOHMANN_JSON_SERIALIZE_ENUM( MyEnum, magic_enum::enum_entries<MyEnum> )

The required change would be minimal, but would be of great value for this kind of combination of libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants