Skip to content

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

@laok-solomon

Description

@laok-solomon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions