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

It can't support "nan", "inf", "-inf" for float type #3494

Closed
2 tasks
laok-solomon opened this issue May 13, 2022 · 2 comments
Closed
2 tasks

It can't support "nan", "inf", "-inf" for float type #3494

laok-solomon opened this issue May 13, 2022 · 2 comments
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@laok-solomon
Copy link

laok-solomon commented May 13, 2022

Description

when use PCL library, I need write some data to json.
my code is some like:

"write special float value to json file":
float userFilterValue = std::numeric_limits<float>::quiet_NaN();
nlohmann::ordered_json ser;
ser["userFilterValue“] = userFilterValue;
std::ofstream("test.json") << ser.dump(2);

"read special float value to json file":
float userFilterValue2 = 0;
nlohmann::ordered_json ser2;
std::ifstream("test.json") >> ser2;
userFilterValue2 = (float) ser["userFilterValue“];

I found that the "test.json" file data:

{
 "userFilterValue": null
}

Reproduction steps

I just want to serialize some special float value, such as "nan", "inf", "-inf"
but it throws exception wiht tips "type must be number, but is null"

Expected vs. actual results

when I write "nan", "inf" as float value, but it got "null"

Minimal code example

float userFilterValue = std::numeric_limits<float>::quiet_NaN();
nlohmann::ordered_json ser;
ser["userFilterValue“] = userFilterValue;
std::ofstream("test.json") << ser.dump(2);


nlohmann::ordered_json ser2;
std::ifstream("test.json") >> ser2;
float userFilterValue2 = (float) ser["userFilterValue“];

Error messages

type must be number, but is null

Compiler and operating system

visual studio 2019

Library version

3.10.4

Validation

@nlohmann
Copy link
Owner

This is expected behavior, see https://json.nlohmann.me/features/types/number_handling/.

@laok-solomon
Copy link
Author

thank you, I see now. I'm glad you gave me a reply.
Maybe, I need to use default value for special float variable when it read "null" value.
Avoiding this special float value to serialize to file or reading "null" float value from json file.

Thank you again for writing the JSON library, which is very concise and convenient.
Although boost.json library has come out, but I still think your json library is better.

@nlohmann nlohmann added the solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) label May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

2 participants