From b56249eec172bd73a250a07f54c8d274dc82ea0f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 14 Apr 2018 20:24:12 -0700 Subject: [PATCH] json: Avoid encoding inf/nan in JSON. It doesn't support them. The common workaround is to use null. --- ext/native/json/json_writer.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ext/native/json/json_writer.cpp b/ext/native/json/json_writer.cpp index 54cb503aa63d..6d4c6905e187 100644 --- a/ext/native/json/json_writer.cpp +++ b/ext/native/json/json_writer.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "ext/vjson/json.h" #include "json/json_writer.h" @@ -129,14 +130,22 @@ void JsonWriter::writeInt(const char *name, int value) { } void JsonWriter::writeFloat(double value) { - str_ << arrayComma() << arrayIndent() << value; + str_ << arrayComma() << arrayIndent(); + if (std::isfinite(value)) + str_ << value; + else + str_ << "null"; stack_.back().first = false; } void JsonWriter::writeFloat(const char *name, double value) { str_ << comma() << indent() << "\""; writeEscapedString(name); - str_ << "\": " << value; + str_ << "\": "; + if (std::isfinite(value)) + str_ << value; + else + str_ << "null"; stack_.back().first = false; }