Replies: 1 comment
-
Actually, nevermind. namespace nlohmann
{
namespace detail
{
EnumJsonFunctions
}
} Works like a charm. I thought of this as I was typing "my guess is in global scope" above. For posterity, I'll leave the original post here, in case someone else has the same problem. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
(Solved. See first comment below.)
I have some enums, each in a different file. Simplified example:
I need to de-/serialize those to/from json. Simplified example:
This will serialize the enum values simply as the underlying integral value.
To get my own serialization to work, I need to declare
to_json
andfrom_json
functions at the place of#include "Helper.h"
:My problem is that I have to repeat this exact code for every namespace that contains an enum, even though the code is exactly the same. In this example, I need to do this:
This works, but it quickly becomes unwieldy. I have a big project with >60 enums in a heap of different namespaces. When I add a new enum I need to remember to look in Helper.h to see whether a matching namespace block exists and C&P one if necessary. And when I remove an enum I need to check again and try to remember whether there are any other enums that need that block, and remove it if none are left.
Alternatively, I could make the
to_json
andfrom_json
functions not templates and have a specific one for each enum, along with the enum declaration in each file. But that's even more annoying code duplication.Aside from being error-prone busywork, this is just silly to have to do. It would be much nicer to have one of those functions (my guess is in global scope) and have that be used.
Beta Was this translation helpful? Give feedback.
All reactions