Permalink
Browse files

json: Avoid encoding inf/nan in JSON.

It doesn't support them.  The common workaround is to use null.
  • Loading branch information...
unknownbrackets committed Apr 15, 2018
1 parent 2e3021d commit b56249eec172bd73a250a07f54c8d274dc82ea0f
Showing with 11 additions and 2 deletions.
  1. +11 −2 ext/native/json/json_writer.cpp
@@ -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;
}

0 comments on commit b56249e

Please sign in to comment.