Skip to content

Roundtripping null to nullptr does not work #1169

Closed

Description

@nlohmann
  • 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;
}

Activity

  1. jaredgrubb commented on Jul 20, 2018

    @jaredgrubb
    Contributor

    +1

  2. self-assigned this
    on Jul 20, 2018
  3. added this to the Release 3.1.3 milestone on Jul 20, 2018
  4. added a commit that references this issue on Jul 21, 2018
  5. nlohmann commented on Jul 21, 2018

    @nlohmann
    OwnerAuthor

    Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions