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

json error on parsing DBL_MAX from string #1796

Closed
sinannuman opened this issue Oct 16, 2019 · 2 comments
Closed

json error on parsing DBL_MAX from string #1796

sinannuman opened this issue Oct 16, 2019 · 2 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@sinannuman
Copy link

sinannuman commented Oct 16, 2019

Hello,
I am facing an exception with serialization on QNX platform. I am serializing a double with the DBL_MAX value (1.7976931348623157e+308). Then I try to parse that value from the string but the exception is thrown:

Code snippet is:

{
    try
    {
        double d = DBL_MAX;
        nlohmann::json js;
        js["max"] = d;
 
        std::stringstream ss;
        ss << js["max"].dump();
        std::cerr << ss.str() << std::endl;
        nlohmann::json js2 = nlohmann::json::parse(ss.str());
        std::cerr << js2.dump() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
}  

Exception:
[json.exception.out_of_range.406] number overflow parsing '1.7976931348623157e+308'

Related ticket:
#360

@t-b
Copy link
Contributor

t-b commented Oct 16, 2019

@sinannuman Do the tests pass on your platform?

If I read the test code correctly we already have a test for DBL_MAX, see

SECTION("issue #360 - Loss of precision when serializing <double>")
{
auto check_roundtrip = [](double number)
{
CAPTURE(number)
json j = number;
CHECK(j.is_number_float());
std::stringstream ss;
ss << j;
CHECK_NOTHROW(ss >> j);
CHECK(j.is_number_float());
CHECK(j.get<json::number_float_t>() == number);
};
check_roundtrip(100000000000.1236);
check_roundtrip((std::numeric_limits<json::number_float_t>::max)());
// Some more numbers which fail to roundtrip when serialized with digits10 significand digits (instead of max_digits10)
.

@nlohmann
Copy link
Owner

I cannot reproduce the issue (macOS 10.14.6, Xcode 11.0):

#include <iostream>
#include <sstream>
#include <cfloat>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    try
    {
        double d = DBL_MAX;
        nlohmann::json js;
        js["max"] = d;
 
        std::stringstream ss;
        ss << js["max"].dump();
        std::cerr << ss.str() << std::endl;
        nlohmann::json js2 = nlohmann::json::parse(ss.str());
        std::cerr << js2.dump() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
}

Output:

1.7976931348623157e+308
1.7976931348623157e+308
Program ended with exit code: 0

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Oct 17, 2019
@nlohmann nlohmann closed this as completed Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants