From 3e8861b1a0bd5e14b9ae022944176638032a2fce Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 21 Jul 2014 10:52:35 +0200 Subject: [PATCH] Moved implemntation of operator[] into JsonObject --- JsonParser/JsonArray.cpp | 22 ------------- JsonParser/JsonArray.h | 7 ++-- JsonParser/JsonObject.cpp | 37 --------------------- JsonParser/JsonObject.h | 7 ++-- JsonParser/JsonValue.cpp | 69 +++++++++++++++++++++++++++++++++------ 5 files changed, 69 insertions(+), 73 deletions(-) diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index 12630c74..342f0fb8 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -11,26 +11,4 @@ using namespace ArduinoJson::Parser; DEPRECATED JsonObject JsonArray::getHashTable(int index) { return operator[](index); -} - -/* -* Returns the token for the value at the specified index -*/ -JsonValue JsonArray::operator[](int index) -{ - // sanity check - if (index < 0 || !isArray() || index >= size()) - return null(); - - // skip first token, it's the whole object - JsonToken runningToken = firstChild(); - - // skip all tokens before the specified index - for (int i = 0; i < index; i++) - { - // move forward: current + nested tokens - runningToken = runningToken.nextSibling(); - } - - return runningToken; } \ No newline at end of file diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index 54fb97f7..11e02d59 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -33,13 +33,16 @@ namespace ArduinoJson return isArray(); } + JsonValue operator[](int index) + { + return JsonValue::operator[](index); + } + int size() { return isArray() ? childrenCount() : 0; } - JsonValue operator[](int index); - JsonArrayIterator begin() { return isArray() ? firstChild() : null(); diff --git a/JsonParser/JsonObject.cpp b/JsonParser/JsonObject.cpp index 1f7589ef..22453f32 100644 --- a/JsonParser/JsonObject.cpp +++ b/JsonParser/JsonObject.cpp @@ -3,7 +3,6 @@ * Benoit Blanchon 2014 - MIT License */ -#include // for strcmp() #include "JsonArray.h" #include "JsonObject.h" #include "JsonValue.h" @@ -13,40 +12,4 @@ using namespace ArduinoJson::Parser; DEPRECATED JsonArray JsonObject::getArray(const char* key) { return operator[](key); -} - -/* -* Returns the token for the value associated with the specified key -*/ -JsonValue JsonObject::operator[](const char* desiredKey) -{ - // sanity check - if (desiredKey == 0 || !isObject()) - return null(); - - // skip first token, it's the whole object - JsonToken runningToken = firstChild(); - - // scan each keys - for (int i = 0; i < childrenCount() / 2; i++) - { - // get 'key' token string - char* key = runningToken.getText(); - - // move to the 'value' token - runningToken = runningToken.nextSibling(); - - // compare with desired name - if (strcmp(desiredKey, key) == 0) - { - // return the value token that follows the key token - return runningToken; - } - - // skip nested tokens - runningToken = runningToken.nextSibling(); - } - - // nothing found, return NULL - return null(); } \ No newline at end of file diff --git a/JsonParser/JsonObject.h b/JsonParser/JsonObject.h index 6f6e1937..ef5c941c 100644 --- a/JsonParser/JsonObject.h +++ b/JsonParser/JsonObject.h @@ -31,9 +31,12 @@ namespace ArduinoJson bool success() { return isObject(); + } + + JsonValue operator[](const char* key) + { + return JsonValue::operator[](key); } - - JsonValue operator[](const char* key); bool containsKey(const char* key) { diff --git a/JsonParser/JsonValue.cpp b/JsonParser/JsonValue.cpp index 8b76c2cf..6bfd6b19 100644 --- a/JsonParser/JsonValue.cpp +++ b/JsonParser/JsonValue.cpp @@ -4,22 +4,13 @@ */ #include // for strtol, strtod +#include // for strcmp() #include "JsonArray.h" #include "JsonObject.h" #include "JsonValue.h" using namespace ArduinoJson::Parser; -JsonValue JsonValue::operator[](int index) -{ - return JsonArray(*this)[index]; -} - -JsonValue JsonValue::operator[](const char* key) -{ - return JsonObject(*this)[key]; -} - JsonValue::operator bool() { if (!isPrimitive()) return 0; @@ -52,4 +43,62 @@ JsonValue::operator long() JsonValue::operator char*() { return isString() || isPrimitive() ? getText() : 0; +} + +/* +* Returns the token for the value associated with the specified key +*/ +JsonValue JsonValue::operator[](const char* desiredKey) +{ + // sanity check + if (desiredKey == 0 || !isObject()) + return null(); + + // skip first token, it's the whole object + JsonToken runningToken = firstChild(); + + // scan each keys + for (int i = 0; i < childrenCount() / 2; i++) + { + // get 'key' token string + char* key = runningToken.getText(); + + // move to the 'value' token + runningToken = runningToken.nextSibling(); + + // compare with desired name + if (strcmp(desiredKey, key) == 0) + { + // return the value token that follows the key token + return runningToken; + } + + // skip nested tokens + runningToken = runningToken.nextSibling(); + } + + // nothing found, return NULL + return null(); +} + +/* +* Returns the token for the value at the specified index +*/ +JsonValue JsonValue::operator[](int index) +{ + // sanity check + if (index < 0 || !isArray() || index >= childrenCount()) + return null(); + + // skip first token, it's the whole object + JsonToken runningToken = firstChild(); + + // skip all tokens before the specified index + for (int i = 0; i < index; i++) + { + // move forward: current + nested tokens + runningToken = runningToken.nextSibling(); + } + + return runningToken; } \ No newline at end of file