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

NaN problem within develop branch #693

Closed
zenfey opened this issue Aug 11, 2017 · 1 comment
Closed

NaN problem within develop branch #693

zenfey opened this issue Aug 11, 2017 · 1 comment

Comments

@zenfey
Copy link

zenfey commented Aug 11, 2017

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;
}
@nlohmann
Copy link
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
Projects
None yet
Development

No branches or pull requests

2 participants