From 6a868e46bd8b8fb45b8348dcc89175b419a5fc7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Wed, 16 Jul 2014 13:41:00 +0200 Subject: [PATCH] Made JsonValue inherit from JsonObjectBase --- JsonParser/JsonArray.cpp | 30 +++++++++++------------------- JsonParser/JsonArray.h | 13 +++++++++++-- JsonParser/JsonHashTable.cpp | 9 +-------- JsonParser/JsonHashTable.h | 13 +++++++++++-- JsonParser/JsonObjectBase.h | 13 ++++--------- JsonParser/JsonValue.cpp | 28 ++++++++++++++-------------- JsonParser/JsonValue.h | 28 +++++++++------------------- 7 files changed, 61 insertions(+), 73 deletions(-) diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index 1dbf66b7..c4d99ef4 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -8,34 +8,26 @@ using namespace ArduinoJson::Parser; -JsonArray::JsonArray(char* json, jsmntok_t* tokens) -: JsonObjectBase(json, tokens) -{ - if (tokens == 0 || tokens[0].type != JSMN_ARRAY) - makeInvalid(); -} - - /* * Returns the token for the value at the specified index */ JsonValue JsonArray::operator[](int index) { - // sanity check - if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) + // 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 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); - } + // 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); + return JsonValue(json, currentToken); } diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index 2d9d5685..6712ea5f 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -25,9 +25,14 @@ namespace ArduinoJson JsonArray() {} + bool success() + { + return JsonObjectBase::success() && tokens->type == JSMN_ARRAY; + } + int getLength() { - return tokens != 0 ? tokens[0].size : 0; + return success() ? tokens[0].size : 0; } JsonValue operator[](int index); @@ -61,7 +66,11 @@ namespace ArduinoJson private: - JsonArray(char* json, jsmntok_t* tokens); + JsonArray(char* json, jsmntok_t* tokens) + : JsonObjectBase(json, tokens) + { + + } }; } } \ No newline at end of file diff --git a/JsonParser/JsonHashTable.cpp b/JsonParser/JsonHashTable.cpp index ae1f40fb..748cc9a6 100644 --- a/JsonParser/JsonHashTable.cpp +++ b/JsonParser/JsonHashTable.cpp @@ -10,20 +10,13 @@ using namespace ArduinoJson::Parser; -JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) -: JsonObjectBase(json, tokens) -{ - if (tokens == 0 || tokens[0].type != JSMN_OBJECT) - makeInvalid(); -} - /* * Returns the token for the value associated with the specified key */ JsonValue JsonHashTable::operator [](const char* desiredKey) { // sanity check - if (json == 0 || tokens == 0 || desiredKey == 0) + if (!success() || desiredKey == 0) return JsonValue(); // skip first token, it's the whole object diff --git a/JsonParser/JsonHashTable.h b/JsonParser/JsonHashTable.h index fb6f00b4..6873b0c4 100644 --- a/JsonParser/JsonHashTable.h +++ b/JsonParser/JsonHashTable.h @@ -23,8 +23,13 @@ namespace ArduinoJson public: - JsonHashTable() {} + JsonHashTable() {} + bool success() + { + return JsonObjectBase::success() && tokens->type == JSMN_OBJECT; + } + JsonValue operator[](const char* key); bool containsKey(const char* key) @@ -61,7 +66,11 @@ namespace ArduinoJson private: - JsonHashTable(char* json, jsmntok_t* tokens); + JsonHashTable(char* json, jsmntok_t* tokens) + : JsonObjectBase(json, tokens) + { + + } }; } } \ No newline at end of file diff --git a/JsonParser/JsonObjectBase.h b/JsonParser/JsonObjectBase.h index 68ca2875..9599b430 100644 --- a/JsonParser/JsonObjectBase.h +++ b/JsonParser/JsonObjectBase.h @@ -16,8 +16,9 @@ namespace ArduinoJson public: JsonObjectBase() - { - makeInvalid(); + : json(0), tokens(0) + { + } bool success() @@ -28,15 +29,9 @@ namespace ArduinoJson protected: JsonObjectBase(char* json, jsmntok_t* tokens) + : json(json), tokens(tokens) { - this->json = json; - this->tokens = tokens; - } - void makeInvalid() - { - json = 0; - tokens = 0; } static int getNestedTokenCount(jsmntok_t* token); diff --git a/JsonParser/JsonValue.cpp b/JsonParser/JsonValue.cpp index c93bc862..0b135acd 100644 --- a/JsonParser/JsonValue.cpp +++ b/JsonParser/JsonValue.cpp @@ -12,52 +12,52 @@ using namespace ArduinoJson::Parser; JsonValue::operator bool() { - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0; // "true" - if (json[token->start] == 't') return true; + if (json[tokens->start] == 't') return true; // "false" - if (json[token->start] == 'f') return false; + if (json[tokens->start] == 'f') return false; // "null" - if (json[token->start] == 'n') return false; + if (json[tokens->start] == 'n') return false; // number - return strtol(json + token->start, 0, 0) != 0; + return strtol(json + tokens->start, 0, 0) != 0; } JsonValue::operator double() { - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0; - return strtod(json + token->start, 0); + return strtod(json + tokens->start, 0); } JsonValue::operator long() { - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0; - return strtol(json + token->start, 0, 0); + return strtol(json + tokens->start, 0, 0); } JsonValue::operator char*() { - if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING) + if (tokens == 0 || tokens->type != JSMN_PRIMITIVE && tokens->type != JSMN_STRING) return 0; // add null terminator to the string - json[token->end] = 0; + json[tokens->end] = 0; - return json + token->start; + return json + tokens->start; } JsonValue::operator JsonArray() { - return JsonArray(json, token); + return JsonArray(json, tokens); } JsonValue::operator JsonHashTable() { - return JsonHashTable(json, token); + return JsonHashTable(json, tokens); } \ No newline at end of file diff --git a/JsonParser/JsonValue.h b/JsonParser/JsonValue.h index 463921d4..d5606ddc 100644 --- a/JsonParser/JsonValue.h +++ b/JsonParser/JsonValue.h @@ -7,6 +7,8 @@ #include "jsmn.h" +#include "JsonObjectBase.h" + namespace ArduinoJson { namespace Parser @@ -14,15 +16,19 @@ namespace ArduinoJson class JsonArray; class JsonHashTable; - class JsonValue + class JsonValue : public JsonObjectBase { friend JsonArray; friend JsonHashTable; public: - bool success() + + JsonValue() {} + + JsonValue(char* json, jsmntok_t* tokens) + : JsonObjectBase(json, tokens) { - return token != 0; + } operator bool(); @@ -31,22 +37,6 @@ namespace ArduinoJson operator char*(); operator JsonArray(); operator JsonHashTable(); - - private: - JsonValue() - : json(0), token(0) - { - - } - - JsonValue(char* json, jsmntok_t* token) - : json(json), token(token) - { - - } - - char* json; - jsmntok_t* token; }; } } \ No newline at end of file