Open RAnders00 opened 6 months ago
This is documented behavior, see https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/#parameters
In this case, I need to use the adl_serializer
specialization everywhere? I see. Thank you for the swift response, I had indeed overlooked this limitation.
Would you be open to tracking this deficiency as a feature request/request for enhancement then?
Would you be open to tracking this deficiency as a feature request/request for enhancement then?
Sure - though I hope it does not mean adding another dozen of macros for this case...
This works just fine, but now structs containing this type cannot be json-converted using the macros anymore: Godbolt link: https://godbolt.org/z/q8oxq61sx (gcc trunk, nlohmann_json trunk)
That's simply because you didn't implement the version of from_json
that takes the destination object by reference. You need that in order to support the get_to
from the macros. Also, you should use the templated version of the first parameter so that you can also support ordered_json
.
NLOHMANN_JSON_NAMESPACE_BEGIN
template <> struct adl_serializer<NonDefaultConstructible> {
template<typename BasicJsonType>
static void to_json(BasicJsonType &json_value, const NonDefaultConstructible &data) {
json_value = data.get_inner_value();
}
template<typename BasicJsonType>
static void from_json(BasicJsonType && j, NonDefaultConstructible &val)
{
// There is no setter for this member, so I had to create a new one and move it into the original.
// I assume in the original there is a setter, which would make this easier.
val = NonDefaultConstructible{j.template get<std::string>()};
}
};
NLOHMANN_JSON_NAMESPACE_END
@nlohmann It would probably be a good idea to update the docs examples to use the template BasicJsonType
.
template<typename BasicJsonType> static void from_json(BasicJsonType && j, NonDefaultConstructible &val)
Just curious, why the forwarding reference? When I try it, it seems to work with a const BasicJsonType&
too, and clangd gives me a warning that the forwarding reference is never forwarded inside the function body?
It should probably just be const&
.
Description
The family of
NLOHMANN_JSON_FROM*
macros all seem to be incompatible with any kind of type that does/cannot have a default constructor.Reproduction steps
See code example below, including Godbolt link.
Expected vs. actual results
Expected result: There is a macro available that can generate a
from_json
function that compiles successfully. Actual result: Template compile error.Minimal code example
Given the following simple class:
From the documentation I gather that the only way to json-convert this type is to specialize
adl_serializer
for it:This works just fine, but now structs containing this type cannot be json-converted using the macros anymore:
Godbolt link: https://godbolt.org/z/q8oxq61sx (gcc trunk, nlohmann_json trunk)
Error messages
Compiler and operating system
g++ 11.4, g++ trunk, clang++ 11.0.1, clang trunk, OS: ubuntu 22.04/godbolt
Library version
3.11.3
Validation
develop
branch is used.