From 714a37bd5915c7fae0dc23732749e22fa4e23d79 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 18 Jul 2014 15:54:49 +0200 Subject: [PATCH] Replaced JsonToken operators by meaningful methods --- JsonParser/JsonArray.cpp | 6 +++--- JsonParser/JsonArray.h | 4 ++-- JsonParser/JsonArrayIterator.h | 2 +- JsonParser/JsonHashTable.cpp | 6 +++--- JsonParser/JsonToken.h | 13 +++++++------ JsonParserTests/JsonArrayIteratorTests.cpp | 4 ++-- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index 1d3629df..054b8a9b 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -24,14 +24,14 @@ JsonValue JsonArray::operator[](int index) return JsonValue::null(); // skip first token, it's the whole object - JsonToken currentToken = token + 1; + JsonToken runningToken = token.firstChild(); // skip all tokens before the specified index for (int i = 0; i < index; i++) { // move forward: current + nested tokens - currentToken += 1 + currentToken.nestedTokenCount(); + runningToken = runningToken.nextSibling(); } - return JsonValue(json, currentToken); + return JsonValue(json, runningToken); } \ No newline at end of file diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index d1f336c5..70cb0aea 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -45,12 +45,12 @@ namespace ArduinoJson JsonArrayIterator begin() { - return JsonArrayIterator(json, token); + return JsonArrayIterator(json, token.firstChild()); } JsonArrayIterator end() { - return JsonArrayIterator(json, token + token.nestedTokenCount()); + return JsonArrayIterator(json, token.nextSibling()); } DEPRECATED int getLength() diff --git a/JsonParser/JsonArrayIterator.h b/JsonParser/JsonArrayIterator.h index 5b4d9a2d..0cf72f3c 100644 --- a/JsonParser/JsonArrayIterator.h +++ b/JsonParser/JsonArrayIterator.h @@ -23,7 +23,7 @@ namespace ArduinoJson JsonArrayIterator operator++() { JsonArrayIterator prev = *this; - token += 1; + token = token.nextSibling(); return prev; } diff --git a/JsonParser/JsonHashTable.cpp b/JsonParser/JsonHashTable.cpp index 627c2548..464571ca 100644 --- a/JsonParser/JsonHashTable.cpp +++ b/JsonParser/JsonHashTable.cpp @@ -26,7 +26,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey) return JsonValue::null(); // skip first token, it's the whole object - JsonToken runningToken = token + 1; + JsonToken runningToken = token.firstChild(); // scan each keys for (int i = 0; i < token.size() / 2; i++) @@ -35,7 +35,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey) char* key = runningToken.getText(json); // move to the 'value' token - runningToken += 1; + runningToken = runningToken.nextSibling(); // compare with desired name if (strcmp(desiredKey, key) == 0) @@ -45,7 +45,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey) } // skip nested tokens - runningToken += 1 + runningToken.nestedTokenCount(); + runningToken = runningToken.nextSibling(); } // nothing found, return NULL diff --git a/JsonParser/JsonToken.h b/JsonParser/JsonToken.h index f29b5095..7331629f 100644 --- a/JsonParser/JsonToken.h +++ b/JsonParser/JsonToken.h @@ -27,14 +27,14 @@ namespace ArduinoJson return json + token->start; } - JsonToken operator+ (int n) + JsonToken firstChild() const { - return JsonToken(token + n); + return JsonToken(token + 1); } - void operator+= (int n) + JsonToken nextSibling() const { - token += n; + return JsonToken(token + 1 + nestedTokenCount()); } bool operator!= (const JsonToken& other) @@ -77,10 +77,11 @@ namespace ArduinoJson return JsonToken(0); } - int nestedTokenCount() const; - private: jsmntok_t* token; + + int nestedTokenCount() const; + }; } } diff --git a/JsonParserTests/JsonArrayIteratorTests.cpp b/JsonParserTests/JsonArrayIteratorTests.cpp index 99ca5cb5..77491801 100644 --- a/JsonParserTests/JsonArrayIteratorTests.cpp +++ b/JsonParserTests/JsonArrayIteratorTests.cpp @@ -11,13 +11,13 @@ namespace JsonParserTests { public: - TEST_METHOD(SimpleIteraton) + TEST_METHOD(ThreeIntegers) { char json [] = "[1,2,3]"; JsonParser<4> parser; JsonArray a = parser.parse(json); - + long expected = 1; for (auto i : a)