diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index fa5859d7..2e7bbe35 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -8,29 +8,6 @@ using namespace ArduinoJson::Parser; -/* -* Returns the token for the value at the specified index -*/ -JsonValue JsonArray::operator[](int index) -{ - // sanity check - if (!success() || index < 0 || index >= tokens[0].size) - return JsonValue(); - - // skip first token, it's the whole object - jsmntok_t* currentToken = tokens + 1; - - // skip all tokens before the specified index - for (int i = 0; i < index; i++) - { - // move forward: current + nested tokens - currentToken += 1 + getNestedTokenCount(currentToken); - } - - return JsonValue(json, currentToken); -} - - DEPRECATED JsonHashTable JsonArray::getHashTable(int index) { return (JsonHashTable) (*this)[index]; diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index 40104747..e9937ef8 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -5,7 +5,6 @@ #pragma once -#include "JsonObjectBase.h" #include "JsonValue.h" namespace ArduinoJson @@ -14,26 +13,32 @@ namespace ArduinoJson { class JsonHashTable; - class JsonArray : public JsonObjectBase + class JsonArray { - friend class JsonParserBase; - friend class JsonValue; - public: JsonArray() {} + JsonArray(JsonValue& value) + : value(value) + { + + } + bool success() { - return JsonObjectBase::success() && tokens->type == JSMN_ARRAY; + return value.success(); } int size() { - return success() ? tokens[0].size : 0; + return value.size(); } - JsonValue operator[](int index); + JsonValue operator[](int index) + { + return value[index]; + } DEPRECATED int getLength() { @@ -69,11 +74,7 @@ namespace ArduinoJson private: - JsonArray(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) - { - - } + JsonValue value; }; } } \ No newline at end of file diff --git a/JsonParser/JsonHashTable.h b/JsonParser/JsonHashTable.h index b6870610..30092784 100644 --- a/JsonParser/JsonHashTable.h +++ b/JsonParser/JsonHashTable.h @@ -5,7 +5,6 @@ #pragma once -#include "JsonObjectBase.h" #include "JsonValue.h" namespace ArduinoJson diff --git a/JsonParser/JsonObjectBase.cpp b/JsonParser/JsonObjectBase.cpp deleted file mode 100644 index 1d65a2cf..00000000 --- a/JsonParser/JsonObjectBase.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* -* Arduino JSON library -* Benoit Blanchon 2014 - MIT License -*/ - -#include "JsonObjectBase.h" - -using namespace ArduinoJson::Parser; - -int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) -{ - int tokensToVisit = token->size; - int count = 0; - - while (tokensToVisit) - { - count++; - token++; - tokensToVisit--; - tokensToVisit += token->size; - } - - return count; -} \ No newline at end of file diff --git a/JsonParser/JsonObjectBase.h b/JsonParser/JsonObjectBase.h deleted file mode 100644 index 2fe30777..00000000 --- a/JsonParser/JsonObjectBase.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -* Arduino JSON library -* Benoit Blanchon 2014 - MIT License -*/ - -#pragma once - -#include "jsmn.h" - -#ifdef __GNUC__ -#define DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) -#else -#define DEPRECATED -#endif - -namespace ArduinoJson -{ - namespace Parser - { - class JsonObjectBase - { - friend class JsonHashTable; - - public: - - JsonObjectBase() - : json(0), tokens(0) - { - - } - - bool success() - { - return json != 0 && tokens != 0; - } - - protected: - - JsonObjectBase(char* json, jsmntok_t* tokens) - : json(json), tokens(tokens) - { - - } - - static int getNestedTokenCount(jsmntok_t* token); - - char* json; - jsmntok_t* tokens; - }; - } -} \ No newline at end of file diff --git a/JsonParser/JsonValue.cpp b/JsonParser/JsonValue.cpp index 2de9e185..bbb5157e 100644 --- a/JsonParser/JsonValue.cpp +++ b/JsonParser/JsonValue.cpp @@ -55,25 +55,30 @@ JsonValue::operator char*() JsonValue::operator JsonArray() { - return tokens->type != JSMN_ARRAY + return tokens != 0 && tokens->type == JSMN_ARRAY ? JsonArray(*this) : JsonArray(); } JsonValue::operator JsonHashTable() { - return tokens->type != JSMN_OBJECT + return tokens != 0 && tokens->type == JSMN_OBJECT ? JsonHashTable(*this) : JsonHashTable(); } +int JsonValue::size() +{ + return tokens != 0 && tokens->type == JSMN_ARRAY ? tokens->size : 0; +} + /* * Returns the token for the value associated with the specified key */ JsonValue JsonValue::operator [](const char* desiredKey) { // sanity check - if (!json || !desiredKey || tokens->type != JSMN_OBJECT) + if (json == 0 || desiredKey == 0 || tokens->type != JSMN_OBJECT) return JsonValue(); // skip first token, it's the whole object @@ -98,4 +103,42 @@ JsonValue JsonValue::operator [](const char* desiredKey) // nothing found, return NULL return JsonValue(); +} + +/* +* Returns the token for the value at the specified index +*/ +JsonValue JsonValue::operator[](int index) +{ + // sanity check + if (index < 0 || index >= size()) + return JsonValue(); + + // skip first token, it's the whole object + jsmntok_t* currentToken = tokens + 1; + + // skip all tokens before the specified index + for (int i = 0; i < index; i++) + { + // move forward: current + nested tokens + currentToken += 1 + getNestedTokenCount(currentToken); + } + + return JsonValue(json, currentToken); +} + +int JsonValue::getNestedTokenCount(jsmntok_t* token) +{ + int tokensToVisit = token->size; + int count = 0; + + while (tokensToVisit) + { + count++; + token++; + tokensToVisit--; + tokensToVisit += token->size; + } + + return count; } \ No newline at end of file diff --git a/JsonParser/JsonValue.h b/JsonParser/JsonValue.h index d1579ac2..4478f25e 100644 --- a/JsonParser/JsonValue.h +++ b/JsonParser/JsonValue.h @@ -6,7 +6,14 @@ #pragma once #include "jsmn.h" -#include "JsonObjectBase.h" + +#ifdef __GNUC__ +#define DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED __declspec(deprecated) +#else +#define DEPRECATED +#endif namespace ArduinoJson { @@ -15,15 +22,24 @@ namespace ArduinoJson class JsonArray; class JsonHashTable; - class JsonValue : public JsonObjectBase + class JsonValue { public: - JsonValue() {} - - JsonValue(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) + JsonValue() + : json(0), tokens(0) { + } + + JsonValue(char* json, jsmntok_t* tokens) + : json(json), tokens(tokens) + { + + } + + bool success() + { + return json != 0 && tokens != 0; } operator bool(); @@ -33,7 +49,17 @@ namespace ArduinoJson operator JsonArray(); operator JsonHashTable(); - JsonValue operator[](const char* key); + JsonValue operator[](const char*); + JsonValue operator[](int); + + int size(); + + private: + + char* json; + jsmntok_t* tokens; + + static int getNestedTokenCount(jsmntok_t* token); }; } } \ No newline at end of file diff --git a/JsonParserTests/JsonParserTests.vcxproj b/JsonParserTests/JsonParserTests.vcxproj index a714e3bf..15407be6 100644 --- a/JsonParserTests/JsonParserTests.vcxproj +++ b/JsonParserTests/JsonParserTests.vcxproj @@ -88,7 +88,6 @@ - @@ -99,7 +98,6 @@ - diff --git a/JsonParserTests/JsonParserTests.vcxproj.filters b/JsonParserTests/JsonParserTests.vcxproj.filters index ebd315f2..74cb6322 100644 --- a/JsonParserTests/JsonParserTests.vcxproj.filters +++ b/JsonParserTests/JsonParserTests.vcxproj.filters @@ -24,9 +24,6 @@ Source Files - - Source Files - Source Files @@ -53,9 +50,6 @@ Header Files - - Header Files - Header Files