-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Description
-
What is the issue you have?
Compile error when trying to (de)serialize an enum.
-
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
AFAIK the minimum requirement to trigger this is:
- A type T for which the "non-default-constructible" form of to/from_json has been defined, and for which
non-member operator== is defined. - An enum for which (de)serialization is defined using NLOMANN_JSON_SERIALIZE_ENUM.
MWE (Wandbox), see code below.
Again AFAIK, this is what happens: the comparisons in NLOHMANN_JSON_SERIALIZE_ENUM on lines 571 and 583 are against different types (BasicJsonType & ENUM_TYPE). This causes the compiler to try and convert both sides to a common type. Since there's a non-member
operator==
fordummy::foo
, it will try to convert both sides todummy::foo
. And since to/from_json has been defined, it tries to use that to deserialize the JSON. It seems that for some reason the wrongget<dummy::foo>()
is called, which then gives a compile error. That's as far as I got, I haven't had more time to try and debug all the template code.PS: Seems to me the capture
[j]
on line 581 should be[&j]
, the JSON object is being copied right now. - A type T for which the "non-default-constructible" form of to/from_json has been defined, and for which
#include <json.hpp>
namespace dummy {
struct foo { };
bool operator== (foo const & lhs, foo const & rhs);
enum class fruit { apple };
} // ns dummy
namespace nlohmann {
template <>
struct adl_serializer<dummy::foo> {
static dummy::foo from_json (json const & j);
static void to_json (json & result, dummy::foo const & obj);
};
} // ns nlohmann
namespace dummy {
NLOHMANN_JSON_SERIALIZE_ENUM(fruit, {
{ fruit::apple, "apple" }
})
} // ns dummy
int main () {
auto val = nlohmann::json("apple").get<dummy::fruit>();
}
-
What is the expected behavior?
Code compiles and (de)serializes enums properly.
-
And what is the actual behavior instead?
The code gives a compile error.
-
Which compiler and operating system are you using? Is it a supported compiler?
Tried with gcc 7.3.0 and gcc 9.1.0 (on Wandbox).
-
Did you use a released version of the library or the version from the
develop
branch?Release 3.6.1, single header version.
-
If you experience a compilation error: can you compile and run the unit tests?
I haven't gotten around to this. If it helps I can try that this weekend.