json: Avoid encoding inf/nan in JSON.

It doesn't support them.  The common workaround is to use null.
This commit is contained in:
Unknown W. Brackets 2018-04-14 20:24:12 -07:00
parent 2e3021da0c
commit b56249eec1

View File

@ -1,4 +1,5 @@
#include <iomanip>
#include <cmath>
#include <cstring>
#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;
}