fix(JSON::Stringifier): JSON Serializing NAN #3251

This commit is contained in:
Alex Fabijanic 2022-06-22 13:44:00 +02:00
parent 5cbe30e199
commit cafd56a947
3 changed files with 15 additions and 0 deletions

View File

@ -62,6 +62,8 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
else if (any.isNumeric() || any.isBoolean())
{
std::string value = any.convert<std::string>();
if ((Poco::icompare(value, "nan") == 0) ||
(Poco::icompare(value, "inf") == 0)) value = "null";
if (any.type() == typeid(char)) formatString(value, out, options);
else out << value;
}

View File

@ -1586,6 +1586,17 @@ void JSONTest::testStringify()
}
void JSONTest::testStringifyNaN()
{
Object::Ptr o = new Object;
o->set("NaN", NAN);
o->set("Infinity", INFINITY);
std::ostringstream stream;
o->stringify(stream, 0);
assertEqual (stream.str(), std::string(R"({"Infinity":null,"NaN":null})"));
}
void JSONTest::testStringifyPreserveOrder()
{
Object presObj(Poco::JSON_PRESERVE_KEY_ORDER);
@ -2361,6 +2372,7 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testComment);
CppUnit_addTest(pSuite, JSONTest, testPrintHandler);
CppUnit_addTest(pSuite, JSONTest, testStringify);
CppUnit_addTest(pSuite, JSONTest, testStringifyNaN);
CppUnit_addTest(pSuite, JSONTest, testStringifyPreserveOrder);
CppUnit_addTest(pSuite, JSONTest, testVarConvert);
CppUnit_addTest(pSuite, JSONTest, testValidJanssonFiles);

View File

@ -67,6 +67,7 @@ public:
void testComment();
void testPrintHandler();
void testStringify();
void testStringifyNaN();
void testStringifyPreserveOrder();
void testVarConvert();