Handle decimal points and empty messages.
We were not handling fields with decimal points, or empty messages. Fix
that.
Change-Id: Icde878d211b979a1377d72acfc7bd6aa71872cf1
diff --git a/aos/json_to_flatbuffer.cc b/aos/json_to_flatbuffer.cc
index d73089b..737bacb 100644
--- a/aos/json_to_flatbuffer.cc
+++ b/aos/json_to_flatbuffer.cc
@@ -926,7 +926,12 @@
ConsumeWhitespace();
- state_ = State::kExpectField;
+ if (Consume("}")) {
+ ConsumeWhitespace();
+ state_ = State::kExpectObjectEnd;
+ } else {
+ state_ = State::kExpectField;
+ }
return TokenType::kStartObject;
case State::kExpectField: {
@@ -1092,7 +1097,7 @@
const char *pos = field_value().c_str();
errno = 0;
*value = strtoll(field_value().c_str(), const_cast<char **>(&pos), 10);
- if (pos == field_value().c_str() || errno != 0) {
+ if (pos != field_value().c_str() + field_value().size() || errno != 0) {
return false;
}
return true;
@@ -1103,7 +1108,7 @@
errno = 0;
*value = strtod(field_value().c_str(), const_cast<char **>(&pos));
- if (pos == field_value().c_str() || errno != 0) {
+ if (pos != field_value().c_str() + field_value().size() || errno != 0) {
return false;
}
return true;
diff --git a/aos/json_to_flatbuffer_test.cc b/aos/json_to_flatbuffer_test.cc
index 37edbf3..4b0629e 100644
--- a/aos/json_to_flatbuffer_test.cc
+++ b/aos/json_to_flatbuffer_test.cc
@@ -61,6 +61,12 @@
EXPECT_TRUE(JsonAndBack("{ \"foo_string\": \"baz\" }"));
}
+// Tests that we can handle decimal points.
+TEST_F(JsonToFlatbufferTest, DecimalPoint) {
+ EXPECT_TRUE(JsonAndBack("{ \"foo_float\": 5.1 }"));
+ EXPECT_TRUE(JsonAndBack("{ \"foo_double\": 5.1 }"));
+}
+
// Test what happens if you pass a field name that we don't know.
TEST_F(JsonToFlatbufferTest, InvalidFieldName) {
EXPECT_FALSE(JsonAndBack("{ \"foo\": 5 }"));
@@ -133,6 +139,11 @@
"\"wow\" } ] }"));
}
+// Test that we can parse an empty message.
+TEST_F(JsonToFlatbufferTest, EmptyMessage) {
+ EXPECT_TRUE(JsonAndBack("{ }"));
+}
+
// TODO(austin): Missmatched values.
} // namespace testing