Replaced JsonToken operators by meaningful methods

This commit is contained in:
Benoit Blanchon
2014-07-18 15:54:49 +02:00
parent 4a1d8483cc
commit 714a37bd59
6 changed files with 18 additions and 17 deletions

View File

@ -24,14 +24,14 @@ JsonValue JsonArray::operator[](int index)
return JsonValue::null(); return JsonValue::null();
// skip first token, it's the whole object // skip first token, it's the whole object
JsonToken currentToken = token + 1; JsonToken runningToken = token.firstChild();
// skip all tokens before the specified index // skip all tokens before the specified index
for (int i = 0; i < index; i++) for (int i = 0; i < index; i++)
{ {
// move forward: current + nested tokens // move forward: current + nested tokens
currentToken += 1 + currentToken.nestedTokenCount(); runningToken = runningToken.nextSibling();
} }
return JsonValue(json, currentToken); return JsonValue(json, runningToken);
} }

View File

@ -45,12 +45,12 @@ namespace ArduinoJson
JsonArrayIterator begin() JsonArrayIterator begin()
{ {
return JsonArrayIterator(json, token); return JsonArrayIterator(json, token.firstChild());
} }
JsonArrayIterator end() JsonArrayIterator end()
{ {
return JsonArrayIterator(json, token + token.nestedTokenCount()); return JsonArrayIterator(json, token.nextSibling());
} }
DEPRECATED int getLength() DEPRECATED int getLength()

View File

@ -23,7 +23,7 @@ namespace ArduinoJson
JsonArrayIterator operator++() JsonArrayIterator operator++()
{ {
JsonArrayIterator prev = *this; JsonArrayIterator prev = *this;
token += 1; token = token.nextSibling();
return prev; return prev;
} }

View File

@ -26,7 +26,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey)
return JsonValue::null(); return JsonValue::null();
// skip first token, it's the whole object // skip first token, it's the whole object
JsonToken runningToken = token + 1; JsonToken runningToken = token.firstChild();
// scan each keys // scan each keys
for (int i = 0; i < token.size() / 2; i++) for (int i = 0; i < token.size() / 2; i++)
@ -35,7 +35,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey)
char* key = runningToken.getText(json); char* key = runningToken.getText(json);
// move to the 'value' token // move to the 'value' token
runningToken += 1; runningToken = runningToken.nextSibling();
// compare with desired name // compare with desired name
if (strcmp(desiredKey, key) == 0) if (strcmp(desiredKey, key) == 0)
@ -45,7 +45,7 @@ JsonValue JsonHashTable::getValue(const char* desiredKey)
} }
// skip nested tokens // skip nested tokens
runningToken += 1 + runningToken.nestedTokenCount(); runningToken = runningToken.nextSibling();
} }
// nothing found, return NULL // nothing found, return NULL

View File

@ -27,14 +27,14 @@ namespace ArduinoJson
return json + token->start; 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) bool operator!= (const JsonToken& other)
@ -77,10 +77,11 @@ namespace ArduinoJson
return JsonToken(0); return JsonToken(0);
} }
int nestedTokenCount() const;
private: private:
jsmntok_t* token; jsmntok_t* token;
int nestedTokenCount() const;
}; };
} }
} }

View File

@ -11,7 +11,7 @@ namespace JsonParserTests
{ {
public: public:
TEST_METHOD(SimpleIteraton) TEST_METHOD(ThreeIntegers)
{ {
char json [] = "[1,2,3]"; char json [] = "[1,2,3]";
JsonParser<4> parser; JsonParser<4> parser;