-
-
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
from_json not working for boost::optional example #718
Comments
Does your code compile? Your snippet might not be complete, since I had to change the code to This is because |
Sorry about that, I've modified the snippet. I'd be happy if the code didn't compile but it fails at runtime which I think is really bad. |
Here is my full file: #include <json.hpp>
#include <boost/optional.hpp>
// partial specialization (full specialization works too)
namespace nlohmann {
template <typename T>
struct adl_serializer<boost::optional<T>> {
static void to_json(json& j, const boost::optional<T>& opt) {
if (opt == boost::none) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json& j, boost::optional<T>& opt) {
if (j.is_null()) {
opt = boost::none;
} else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
};
}
int main(int argc, char const *argv[])
{
nlohmann::json json{{"test", nullptr}};
boost::optional<int> works = json["test"];
boost::optional<int> broken;
broken = json["test"];
} I still got the same compiler error, which boost version are you using? I have 1.64 on Debian 8 x64 |
Yes, that's the code that's failing for me on Mac with XCode 8.3.3 and Boost 1.64. It seems to be an issue with Apple's version of Clang. I tried reproducing this using wandbox.org by changing the compiler version and I get the compiler error like you with every version of Clang they have. The problem here is that Apple ships a custom version of Clang that we know nothing about and they obviously diverged in behaviour (or it's a command line switch I'm unaware of)... |
On OSX, I also can't compile the code. GCC and Clang (all with different versions) agree on this error:
|
It's the boost version, not the compiler, sorry for the wild goose chase. This only happens with boost 1.56 and is not that relevant in my view, it'll get resolved as we moved to latest version of boost. I'm happy for you to close this bug. Thanks for the quick responses everyone. |
Ok. Thanks for checking back! |
I have a verbatim copy of adl_serializer for boost::optional from the documentation. Version 2.1.1 of the library.
Assignment to 'broken' variable won't go through the adl_serializer and will throw std::domain_error: type must be number, but is null
The text was updated successfully, but these errors were encountered: