Skip to content

Commit

Permalink
json: Avoid encoding inf/nan in JSON.
Browse files Browse the repository at this point in the history
It doesn't support them.  The common workaround is to use null.
  • Loading branch information
unknownbrackets committed Jun 6, 2018
1 parent 2e3021d commit b56249e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ext/native/json/json_writer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iomanip>
#include <cmath>
#include <cstring>
#include "ext/vjson/json.h"
#include "json/json_writer.h"
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b56249e

Please sign in to comment.