diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index d6173fef..54fb97f7 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -35,19 +35,19 @@ namespace ArduinoJson int size() { - return isArray() ? JsonToken::size() : 0; + return isArray() ? childrenCount() : 0; } JsonValue operator[](int index); JsonArrayIterator begin() { - return firstChild(); + return isArray() ? firstChild() : null(); } JsonArrayIterator end() { - return nextSibling(); + return isArray() ? nextSibling() : null(); } DEPRECATED int getLength() diff --git a/JsonParser/JsonObject.cpp b/JsonParser/JsonObject.cpp index 618c34b3..1f7589ef 100644 --- a/JsonParser/JsonObject.cpp +++ b/JsonParser/JsonObject.cpp @@ -28,7 +28,7 @@ JsonValue JsonObject::operator[](const char* desiredKey) JsonToken runningToken = firstChild(); // scan each keys - for (int i = 0; i < size() / 2; i++) + for (int i = 0; i < childrenCount() / 2; i++) { // get 'key' token string char* key = runningToken.getText(); diff --git a/JsonParser/JsonToken.h b/JsonParser/JsonToken.h index 82c302e3..0e7ffffe 100644 --- a/JsonParser/JsonToken.h +++ b/JsonParser/JsonToken.h @@ -75,7 +75,7 @@ namespace ArduinoJson return token != 0 && token->type == JSMN_STRING; } - int size() + int childrenCount() { return token->size; } diff --git a/JsonParserTests/JsonArrayIteratorTests.cpp b/JsonParserTests/JsonArrayIteratorTests.cpp index 94e0349e..b1774fd3 100644 --- a/JsonParserTests/JsonArrayIteratorTests.cpp +++ b/JsonParserTests/JsonArrayIteratorTests.cpp @@ -10,6 +10,23 @@ namespace JsonParserTests TEST_CLASS(JsonArrayIteratorTests) { public: + + TEST_METHOD(EmptyJson) + { + char json[] = ""; + JsonParser<1> parser; + + JsonArray a = parser.parse(json); + + int loopCount = 0; + + for (long i : a) + { + loopCount++; + } + + Assert::AreEqual(0, loopCount); + } TEST_METHOD(ThreeIntegers) { @@ -17,8 +34,7 @@ namespace JsonParserTests long expected [] = { 1, 2, 3 }; JsonParser<4> parser; - JsonValue v = parser.parse(json); - JsonArray a = (ArduinoJson::Parser::JsonArray)v; + JsonArray a = parser.parse(json); int index = 0; @@ -27,6 +43,5 @@ namespace JsonParserTests Assert::AreEqual(expected[index++], i); } } - }; } \ No newline at end of file