Support nan in the json tokenizer
flatbuffers renders nan as a literal nan in the json file.
Change-Id: Ife76397f8af12a03d382ca364870d8e7cb332fc1
diff --git a/aos/json_tokenizer.cc b/aos/json_tokenizer.cc
index aa92762..e2e8f37 100644
--- a/aos/json_tokenizer.cc
+++ b/aos/json_tokenizer.cc
@@ -139,6 +139,13 @@
// Consume the leading - unconditionally.
Consume("-");
+ // See if we find nan. This isn't standards compliant, but is what
+ // flatbuffers prints out, so we need to parse it.
+ if (Consume("nan")) {
+ *s = ::std::string(original.substr(0, original.size() - data_.size()));
+ return true;
+ }
+
// Then, we either get a 0, or we get a nonzero. Only nonzero can be followed
// by a second number.
if (!Consume("0")) {
@@ -429,6 +436,14 @@
bool Tokenizer::FieldAsDouble(double *value) {
const char *pos = field_value().c_str();
errno = 0;
+ if (field_value() == "nan") {
+ *value = std::numeric_limits<double>::quiet_NaN();
+ return true;
+ } else if (field_value() == "-nan") {
+ *value = -std::numeric_limits<double>::quiet_NaN();
+ return true;
+ }
+
*value = strtod(field_value().c_str(), const_cast<char **>(&pos));
if (pos != field_value().c_str() + field_value().size() || errno != 0) {