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

Inconsistent handling of floating point numbers after parse() #3329

Closed
3 of 5 tasks
newr5 opened this issue Feb 11, 2022 · 3 comments
Closed
3 of 5 tasks

Inconsistent handling of floating point numbers after parse() #3329

newr5 opened this issue Feb 11, 2022 · 3 comments

Comments

@newr5
Copy link

newr5 commented Feb 11, 2022

What is the issue you have?

I read the article about Number Handling (nan-handling)
In it there is an example of a double variable being converted to json and back.

I have extended this example somewhat (see below) by parsing the dumped json object and attempt to convert/assign it to another double variable.
This fails with an exception:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be number, but is null

Please describe the steps to reproduce the issue.

See the code at: godbolt.org

In case that that doesn't work, here is the code:

#include <iostream>
#include <nlohmann/json.hpp>

int main()
{
    double val = std::numeric_limits<double>::quiet_NaN();
    std::cout << "val=" << val << std::endl;
    nlohmann::json j = val;
    std::cout << "j=" << j.dump() << std::endl;
    std::cout << "j.is_number() = " << j.is_number() << std::endl;
    std::cout << "j.is_null() = " << j.is_null() << std::endl;

    val = j;
    std::cout << "val=" << val << std::endl;

    nlohmann::json parsedJson = nlohmann::json::parse(j.dump());
    std::cout << "parsedJson=" << parsedJson.dump() << std::endl;

    std::cout << "parsedJson.is_number() = " << parsedJson.is_number() << std::endl;
    std::cout << "parsedJson.is_null() = " << parsedJson.is_null() << std::endl;

    double parsedVal;

    // Here, the following error occurs:
    // terminate called after throwing an instance of 'nlohmann::detail::type_error'
    //   what():  [json.exception.type_error.302] type must be number, but is null

    parsedVal = parsedJson;

    std::cout << "parsedVal=" << parsedVal << std::endl;
}

The generated output is:

val=nan
j=null
j.is_number() = 1
j.is_null() = 0
val=nan
parsedJson=null
parsedJson.is_number() = 0
parsedJson.is_null() = 1

Followed by the error message shown above.

Can you provide a small but working code example?

What is the expected behavior?

Instead, of generating an exception, I would expect that (for results of float, double, and long double type) the conversion would result in a nan value as it did with the original example...

And what is the actual behavior instead?

The conversion to a double variable fails with an exception:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be number, but is null

Which compiler and operating system are you using?

I don't believe that this is dependent of either OS or compiler, but at 'godbolt' I see the following:

  • Compiler: gcc 11.2
  • Operating system: Not sure what godbolt is using. I assume that it is Linux...

Which version of the library did you use?

  • other release - 3.6.0
  • other release - 'Whatever godbolt refers to as 'trunk'.

If you experience a compilation error: can you compile and run the unit tests?

  • dont-care - No compilation error.
  • yes
  • no - please copy/paste the error message below
@gregmarr
Copy link
Contributor

From the page you linked:

That is, there is no way to parse a NaN value.

That is, you can store it, and you can dump it (as null), but when you parse it again you get a null not a nan.

@nlohmann
Copy link
Owner

The behavior is as expected and documented.

@newr5
Copy link
Author

newr5 commented Feb 11, 2022

OK, now I get it.
I was thrown off by that example. I thought that while indeed NaN was not part of JSON, the null representation as printed by the example was nlohmann::json's way of coping with it.

Thank you both for your explanation and your time.

@newr5 newr5 closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants