-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Upgrading to 3.x: to_/from_json with enum class #1093
Comments
Are the functions in the same namespace as the respective Enum? |
Yep, although they are in a different header file (which is included). The internal ToString and FromString are in the global namespace, but I don't think that matters. It all works fine with 2.1.1. |
I'll try to take a look in the next days, meanwhile I would suggest partially specialize I don't remember when we did add support for enums, I think it was in 2.1.0, which would be even stranger that it breaks now |
In 2.1.1 there is code for Unscoped Enums, using this:
I don't think it will pick up enum classes (by design, probably). The newer code picks up both enum and enum class. I'll see if I can figure out the |
I did take a look, and in order to pick up your overload, you have to constrain your template a bit: template <typename JSON, typename Enum, typename = std::enable_if_t<std::is_enum<Enum>::value>>
void to_json(JSON & j, Enum e) {
j = ToString(e);
} No need to fiddle with |
Thanks for that, and apologies for the slow response. It'll be a few days before I can get back to rolling out new library versions, but I'm sure it will work. Thanks. |
Any news on this? |
Sorry for the lack of response. We've been approaching a release, and I've been busy putting out other fires. I'll try and have a look shortly. Thanks again for your help, and for the library. It's very useful. |
@endorph-soft Any news one this? |
Can I close this issue? |
Hi everyone, I was solving a problem very similar to this, and @theodelrieu 's response was very close to being correct. I could not get things to work until I changed the template a little. I went from... template <typename JSON, typename Enum, typename = std::enable_if_t<std::is_enum<Enum>::value>>
void to_json(JSON & j, Enum e) {
j = ToString(e);
} to typename Enum, typename = std::enable_if_t<std::is_enum<Enum>::value>>
void to_json(nlohmann::json& j, Enum e) {
j = ToString(e);
} Before making this change, I was still being told that my type had no |
Thanks for checking back! |
Hmm, this is quite weird, I'd like to know what is the real difference between both... Anyway, glad you could solve it. |
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:
There are definitions of
ToString
andFromString
for all the enumerations of interest. I'm not using SFINAE to restrictEnum
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
andfrom_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.
The text was updated successfully, but these errors were encountered: