From d189bd7140eb47309b82b6681af725916d14bbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Wed, 16 Jul 2014 13:26:11 +0200 Subject: [PATCH] Added class JsonValue. Added subscript operator on JsonArray and JsonHashTable --- JsonParser/JsonArray.cpp | 34 ++-------- JsonParser/JsonArray.h | 40 +++++++++--- JsonParser/JsonHashTable.cpp | 47 +++----------- JsonParser/JsonHashTable.h | 45 ++++++++++--- JsonParser/JsonObjectBase.cpp | 43 ------------- JsonParser/JsonObjectBase.h | 5 -- JsonParser/JsonValue.cpp | 63 +++++++++++++++++++ JsonParser/JsonValue.h | 52 +++++++++++++++ JsonParserTests/JsonParserTests.vcxproj | 2 + .../JsonParserTests.vcxproj.filters | 6 ++ 10 files changed, 205 insertions(+), 132 deletions(-) create mode 100644 JsonParser/JsonValue.cpp create mode 100644 JsonParser/JsonValue.h diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index f1490baa..1dbf66b7 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -19,11 +19,11 @@ JsonArray::JsonArray(char* json, jsmntok_t* tokens) /* * Returns the token for the value at the specified index */ -jsmntok_t* JsonArray::getToken(int index) +JsonValue JsonArray::operator[](int index) { // sanity check if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) - return 0; + return JsonValue(); // skip first token, it's the whole object jsmntok_t* currentToken = tokens + 1; @@ -35,35 +35,11 @@ jsmntok_t* JsonArray::getToken(int index) currentToken += 1 + getNestedTokenCount(currentToken); } - return currentToken; + return JsonValue(json, currentToken); } -JsonArray JsonArray::getArray(int index) -{ - return JsonArray(json, getToken(index)); -} -bool JsonArray::getBool(int index) +JsonHashTable JsonArray::getHashTable(int index) DEPRECATED { - return getBoolFromToken(getToken(index)); + return (JsonHashTable) (*this)[index]; } - -double JsonArray::getDouble(int index) -{ - return getDoubleFromToken(getToken(index)); -} - -JsonHashTable JsonArray::getHashTable(int index) -{ - return JsonHashTable(json, getToken(index)); -} - -long JsonArray::getLong(int index) -{ - return getLongFromToken(getToken(index)); -} - -char* JsonArray::getString(int index) -{ - return getStringFromToken(getToken(index)); -} \ No newline at end of file diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index 42219b58..2d9d5685 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -6,6 +6,9 @@ #pragma once #include "JsonObjectBase.h" +#include "JsonValue.h" + +#define DEPRECATED namespace ArduinoJson { @@ -16,7 +19,7 @@ namespace ArduinoJson class JsonArray : public JsonObjectBase { friend class JsonParserBase; - friend class JsonHashTable; + friend class JsonValue; public: @@ -27,17 +30,38 @@ namespace ArduinoJson return tokens != 0 ? tokens[0].size : 0; } - JsonArray getArray(int index); - bool getBool(int index); - double getDouble(int index); - JsonHashTable getHashTable(int index); - long getLong(int index); - char* getString(int index); + JsonValue operator[](int index); + + JsonArray getArray(int index) DEPRECATED + { + return (JsonArray) (*this)[index]; + } + + bool getBool(int index) DEPRECATED + { + return (bool) (*this)[index]; + } + + double getDouble(int index) DEPRECATED + { + return (double) (*this)[index]; + } + + JsonHashTable getHashTable(int index) DEPRECATED; + + long getLong(int index) DEPRECATED + { + return (long) (*this)[index]; + } + + char* getString(int index) DEPRECATED + { + return (char*) (*this)[index]; + } private: JsonArray(char* json, jsmntok_t* tokens); - jsmntok_t* getToken(int index); }; } } \ No newline at end of file diff --git a/JsonParser/JsonHashTable.cpp b/JsonParser/JsonHashTable.cpp index 513e5702..ae1f40fb 100644 --- a/JsonParser/JsonHashTable.cpp +++ b/JsonParser/JsonHashTable.cpp @@ -4,8 +4,9 @@ */ #include // for strcmp() -#include "JsonArray.h" #include "JsonHashTable.h" +#include "JsonArray.h" +#include "JsonValue.h" using namespace ArduinoJson::Parser; @@ -19,11 +20,11 @@ JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) /* * Returns the token for the value associated with the specified key */ -jsmntok_t* JsonHashTable::getToken(const char* desiredKey) +JsonValue JsonHashTable::operator [](const char* desiredKey) { // sanity check if (json == 0 || tokens == 0 || desiredKey == 0) - return 0; + return JsonValue(); // skip first token, it's the whole object jsmntok_t* currentToken = tokens + 1; @@ -32,13 +33,13 @@ jsmntok_t* JsonHashTable::getToken(const char* desiredKey) for (int i = 0; i < tokens[0].size / 2 ; i++) { // get key token string - char* key = getStringFromToken(currentToken); + char* key = JsonValue(json, currentToken); // compare with desired name if (strcmp(desiredKey, key) == 0) { // return the value token that follows the key token - return currentToken + 1; + return JsonValue(json, currentToken + 1); } // move forward: key + value + nested tokens @@ -46,40 +47,10 @@ jsmntok_t* JsonHashTable::getToken(const char* desiredKey) } // nothing found, return NULL - return 0; + return JsonValue(); } -bool JsonHashTable::containsKey(const char* key) +JsonArray JsonHashTable::getArray(const char* key) DEPRECATED { - return getToken(key) != 0; -} - -JsonArray JsonHashTable::getArray(const char* key) -{ - return JsonArray(json, getToken(key)); -} - -bool JsonHashTable::getBool(const char* key) -{ - return getBoolFromToken(getToken(key)); -} - -double JsonHashTable::getDouble(const char* key) -{ - return getDoubleFromToken(getToken(key)); -} - -JsonHashTable JsonHashTable::getHashTable(const char* key) -{ - return JsonHashTable(json, getToken(key)); -} - -long JsonHashTable::getLong(const char* key) -{ - return getLongFromToken(getToken(key)); -} - -char* JsonHashTable::getString(const char* key) -{ - return getStringFromToken(getToken(key)); + return (JsonArray) (*this)[key]; } \ No newline at end of file diff --git a/JsonParser/JsonHashTable.h b/JsonParser/JsonHashTable.h index b33492ad..fb6f00b4 100644 --- a/JsonParser/JsonHashTable.h +++ b/JsonParser/JsonHashTable.h @@ -6,6 +6,9 @@ #pragma once #include "JsonObjectBase.h" +#include "JsonValue.h" + +#define DEPRECATED namespace ArduinoJson { @@ -16,25 +19,49 @@ namespace ArduinoJson class JsonHashTable : public JsonObjectBase { friend class JsonParserBase; - friend class JsonArray; + friend class JsonValue; public: JsonHashTable() {} - bool containsKey(const char* key); + JsonValue operator[](const char* key); - JsonArray getArray(const char* key); - bool getBool(const char* key); - double getDouble(const char* key); - JsonHashTable getHashTable(const char* key); - long getLong(const char* key); - char* getString(const char* key); + bool containsKey(const char* key) + { + return (*this)[key].success(); + } + + JsonArray getArray(const char* key) DEPRECATED; + + bool getBool(const char* key) DEPRECATED + { + return (bool) (*this)[key]; + } + + double getDouble(const char* key) DEPRECATED + { + return (double) (*this)[key]; + } + + JsonHashTable getHashTable(const char* key) DEPRECATED + { + return (JsonHashTable) (*this)[key]; + } + + long getLong(const char* key) DEPRECATED + { + return (long) (*this)[key]; + } + + char* getString(const char* key) DEPRECATED + { + return (char*) (*this)[key]; + } private: JsonHashTable(char* json, jsmntok_t* tokens); - jsmntok_t* getToken(const char* key); }; } } \ No newline at end of file diff --git a/JsonParser/JsonObjectBase.cpp b/JsonParser/JsonObjectBase.cpp index 60d47ee8..1d65a2cf 100644 --- a/JsonParser/JsonObjectBase.cpp +++ b/JsonParser/JsonObjectBase.cpp @@ -3,7 +3,6 @@ * Benoit Blanchon 2014 - MIT License */ -#include // for strtol, strtod #include "JsonObjectBase.h" using namespace ArduinoJson::Parser; @@ -22,46 +21,4 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) } return count; -} - -bool JsonObjectBase::getBoolFromToken(jsmntok_t* token) -{ - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; - - // "true" - if (json[token->start] == 't') return true; - - // "false" - if (json[token->start] == 'f') return false; - - // "null" - if (json[token->start] == 'n') return false; - - // number - return strtol(json + token->start, 0, 0) != 0; -} - -double JsonObjectBase::getDoubleFromToken(jsmntok_t* token) -{ - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; - - return strtod(json + token->start, 0); -} - -long JsonObjectBase::getLongFromToken(jsmntok_t* token) -{ - if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; - - return strtol(json + token->start, 0, 0); -} - -char* JsonObjectBase::getStringFromToken(jsmntok_t* token) -{ - if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING) - return 0; - - // add null terminator to the string - json[token->end] = 0; - - return json + token->start; } \ No newline at end of file diff --git a/JsonParser/JsonObjectBase.h b/JsonParser/JsonObjectBase.h index 6c40de04..68ca2875 100644 --- a/JsonParser/JsonObjectBase.h +++ b/JsonParser/JsonObjectBase.h @@ -41,11 +41,6 @@ namespace ArduinoJson static int getNestedTokenCount(jsmntok_t* token); - bool getBoolFromToken(jsmntok_t* token); - double getDoubleFromToken(jsmntok_t* token); - long getLongFromToken(jsmntok_t* token); - char* getStringFromToken(jsmntok_t* token); - char* json; jsmntok_t* tokens; }; diff --git a/JsonParser/JsonValue.cpp b/JsonParser/JsonValue.cpp new file mode 100644 index 00000000..c93bc862 --- /dev/null +++ b/JsonParser/JsonValue.cpp @@ -0,0 +1,63 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include // for strtol, strtod +#include "JsonArray.h" +#include "JsonHashTable.h" +#include "JsonValue.h" + +using namespace ArduinoJson::Parser; + +JsonValue::operator bool() +{ + if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + + // "true" + if (json[token->start] == 't') return true; + + // "false" + if (json[token->start] == 'f') return false; + + // "null" + if (json[token->start] == 'n') return false; + + // number + return strtol(json + token->start, 0, 0) != 0; +} + +JsonValue::operator double() +{ + if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + + return strtod(json + token->start, 0); +} + +JsonValue::operator long() +{ + if (token == 0 || token->type != JSMN_PRIMITIVE) return 0; + + return strtol(json + token->start, 0, 0); +} + +JsonValue::operator char*() +{ + if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING) + return 0; + + // add null terminator to the string + json[token->end] = 0; + + return json + token->start; +} + +JsonValue::operator JsonArray() +{ + return JsonArray(json, token); +} + +JsonValue::operator JsonHashTable() +{ + return JsonHashTable(json, token); +} \ No newline at end of file diff --git a/JsonParser/JsonValue.h b/JsonParser/JsonValue.h new file mode 100644 index 00000000..463921d4 --- /dev/null +++ b/JsonParser/JsonValue.h @@ -0,0 +1,52 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "jsmn.h" + +namespace ArduinoJson +{ + namespace Parser + { + class JsonArray; + class JsonHashTable; + + class JsonValue + { + friend JsonArray; + friend JsonHashTable; + + public: + bool success() + { + return token != 0; + } + + operator bool(); + operator double(); + operator long(); + 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 diff --git a/JsonParserTests/JsonParserTests.vcxproj b/JsonParserTests/JsonParserTests.vcxproj index f9df10eb..a714e3bf 100644 --- a/JsonParserTests/JsonParserTests.vcxproj +++ b/JsonParserTests/JsonParserTests.vcxproj @@ -90,6 +90,7 @@ + @@ -101,6 +102,7 @@ + diff --git a/JsonParserTests/JsonParserTests.vcxproj.filters b/JsonParserTests/JsonParserTests.vcxproj.filters index a11a87e0..ebd315f2 100644 --- a/JsonParserTests/JsonParserTests.vcxproj.filters +++ b/JsonParserTests/JsonParserTests.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -59,5 +62,8 @@ Header Files + + Header Files + \ No newline at end of file