Skip to content

NaN problem within develop branch #693

Closed

Description

@zenfey

Library json is a very nice and foregoing work.

I noticed that develop branch made effort on the bug of NaN and INF problem. however after deserialization of a serialized json object containing nan, an exception is throwed. binary serialization works fine.

#include <json.hpp>
#include <vector>
#include <iostream>

using json =  nlohmann::json;

int main() {

	std::vector<double> a;
	a.push_back(std::numeric_limits<double>::quiet_NaN());
	a.push_back(2);
	json j;
	j["vec"] = a;
	j["number"] = std::numeric_limits<double>::quiet_NaN();

	std::string strj = j.dump();

	json j2 = json::parse(strj);

	std::vector<double> b = j2["vec"];
	std::cout << b[0] << std::endl;
	double n = j2["number"];
	std::cout << n << std::endl;
	
	return 0;
}

Activity

  1. nlohmann commented on Aug 11, 2017

    @nlohmann
    Owner

    RFC 7159 states:

    Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

    The library dumps inf and NaN to null. This is documented in https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a88d6103cb3620410b35200ee8e313d97.html#a88d6103cb3620410b35200ee8e313d97.

    Therefore, strj is

    {"number":null,"vec":[null,2.0]}
    

    so j2["number"] is null and cannot be converted to double, hence the exception

    [json.exception.type_error.302] type must be number, but is null
    

    Binary serialization works, because CBOR and MessagePack do not have the limitations about inf and NaN.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions