Skip to content
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

Roundtripping null to nullptr does not work #1169

Closed
nlohmann opened this issue Jul 19, 2018 · 2 comments
Closed

Roundtripping null to nullptr does not work #1169

nlohmann opened this issue Jul 19, 2018 · 2 comments
Assignees
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@nlohmann
Copy link
Owner

  • What is the issue you have?

We use the nullptr to denote JSON's null value.

json j = nullptr;

But the reverse does not work:

std::nullptr_t n = j;
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main()
{
    json j = nullptr;
    std::nullptr_t n = j;
    std::cout << std::boolalpha << (n == nullptr) << std::endl;
}
  • What is the expected behavior?

Output:

true
  • And what is the actual behavior instead?

Error could not find from_json() method in T's namespace.

Xcode Version 10.0 beta (10L176w).

  • Did you use a released version of the library or the version from the develop branch?

develop.


Adding the following code to the library solves the issue:

template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
{
    if (JSON_UNLIKELY(not j.is_null()))
    {
        JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
    }
    n = nullptr;
}
@jaredgrubb
Copy link
Contributor

+1

@nlohmann nlohmann self-assigned this Jul 20, 2018
@nlohmann nlohmann added this to the Release 3.1.3 milestone Jul 20, 2018
nlohmann added a commit that referenced this issue Jul 21, 2018
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jul 21, 2018
@nlohmann
Copy link
Owner Author

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants