From 4713e90f128bd3ce9b3a28daf34c00e1446d9266 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 11 Jan 2014 16:41:18 +0100 Subject: [PATCH] Added verification of the type of token --- JsonArray.cpp | 26 ++++++++++++++++++-------- JsonArray.h | 12 +++--------- JsonHashTable.cpp | 31 +++++++++++++++++++------------ JsonHashTable.h | 16 +++++----------- JsonObjectBase.h | 9 +++++++-- JsonParser.cpp | 23 ++++++++++++++--------- JsonParser.h | 31 ++++++++----------------------- 7 files changed, 74 insertions(+), 74 deletions(-) diff --git a/JsonArray.cpp b/JsonArray.cpp index 0a508485..4c84ba6b 100644 --- a/JsonArray.cpp +++ b/JsonArray.cpp @@ -1,20 +1,32 @@ /* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ #include "JsonArray.h" #include "JsonHashTable.h" +JsonArray::JsonArray(char* json, jsmntok_t* tokens) +: JsonObjectBase(json, tokens) +{ + if (tokens[0].type != JSMN_ARRAY) + makeInvalid(); +} + + +/* +* Returns the token for the value at the specified index +*/ jsmntok_t* JsonArray::getToken(int index) { + // sanity check if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) return 0; // skip first token, it's the whole object int currentToken = 1; + // skip all tokens before the specified index for (int i = 0; i < index; i++) { // move forward: current + nested tokens @@ -26,14 +38,12 @@ jsmntok_t* JsonArray::getToken(int index) JsonArray JsonArray::getArray(int index) { - jsmntok_t* token = getToken(index); - return JsonArray(json, token); + return JsonArray(json, getToken(index)); } JsonHashTable JsonArray::getHashTable(int index) { - jsmntok_t* token = getToken(index); - return JsonHashTable(json, token); + return JsonHashTable(json, getToken(index)); } char* JsonArray::getString(int index) diff --git a/JsonArray.h b/JsonArray.h index 30b789eb..73bcd682 100644 --- a/JsonArray.h +++ b/JsonArray.h @@ -1,7 +1,6 @@ /* * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License + * Benoit Blanchon 2014 - MIT License */ #ifndef __JSONARRAY_H @@ -31,13 +30,8 @@ public: private: - JsonArray(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) - { - } - + JsonArray(char* json, jsmntok_t* tokens); jsmntok_t* getToken(int index); }; -#endif - +#endif \ No newline at end of file diff --git a/JsonHashTable.cpp b/JsonHashTable.cpp index 5de2bf2e..6e778b6d 100644 --- a/JsonHashTable.cpp +++ b/JsonHashTable.cpp @@ -1,18 +1,27 @@ /* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ #include "JsonArray.h" #include "JsonHashTable.h" #include // for strcmp() -jsmntok_t* JsonHashTable::getToken(char* name) +JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) +: JsonObjectBase(json, tokens) +{ + if (tokens[0].type != JSMN_OBJECT) + makeInvalid(); +} + +/* +* Returns the token for the value associated with the specified key +*/ +jsmntok_t* JsonHashTable::getToken(char* desiredKey) { // sanity check - if (json == 0 || tokens == 0 || name == 0) + if (json == 0 || tokens == 0 || desiredKey == 0) return 0; // skip first token, it's the whole object @@ -21,11 +30,11 @@ jsmntok_t* JsonHashTable::getToken(char* name) // scan each keys for (int i = 0; i < tokens[0].size / 2 ; i++) { - // Get key token string + // get key token string char* key = json + tokens[currentToken].start; // Compare with desired name - if (strcmp(name, key) == 0) + if (strcmp(desiredKey, key) == 0) { return &tokens[currentToken + 1]; } @@ -40,14 +49,12 @@ jsmntok_t* JsonHashTable::getToken(char* name) JsonArray JsonHashTable::getArray(char* key) { - jsmntok_t* token = getToken(key); - return JsonArray(json, token); + return JsonArray(json, getToken(key)); } JsonHashTable JsonHashTable::getHashTable(char* key) { - jsmntok_t* token = getToken(key); - return JsonHashTable(json, token); + return JsonHashTable(json, getToken(key)); } char* JsonHashTable::getString(char* key) diff --git a/JsonHashTable.h b/JsonHashTable.h index 98e2b4c7..96ad7400 100644 --- a/JsonHashTable.h +++ b/JsonHashTable.h @@ -1,8 +1,7 @@ /* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ #ifndef __JSONHASHTABLE_H #define __JSONHASHTABLE_H @@ -26,13 +25,8 @@ public: private: - JsonHashTable(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) - { - } - + JsonHashTable(char* json, jsmntok_t* tokens); jsmntok_t* getToken(char* key); }; -#endif - +#endif \ No newline at end of file diff --git a/JsonObjectBase.h b/JsonObjectBase.h index d576b8e1..4cd6ba1d 100644 --- a/JsonObjectBase.h +++ b/JsonObjectBase.h @@ -15,8 +15,7 @@ public: JsonObjectBase() { - json = 0; - tokens = 0; + makeInvalid(); } bool success() @@ -32,6 +31,12 @@ protected: this->tokens = tokens; } + void makeInvalid() + { + json = 0; + tokens = 0; + } + int getNestedTokenCount(int tokenIndex); char* json; diff --git a/JsonParser.cpp b/JsonParser.cpp index 97bff18b..7c4e47b6 100644 --- a/JsonParser.cpp +++ b/JsonParser.cpp @@ -1,23 +1,28 @@ /* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ #include "JsonParser.h" -bool JsonParserBase::parse(char* jsonString) +JsonParserBase::JsonParserBase(jsmntok_t* tokens, int maxTokenCount) { - buffer = jsonString; + this->maxTokenCount = maxTokenCount; + this->tokens = tokens; + jsmn_init(&parser); +} + +jsmntok_t* JsonParserBase::parse(char* jsonString) +{ if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount)) - return false; + return 0; // Add null termination to each token for (int i = 1; i < parser.toknext; i++) { - buffer[tokens[i].end] = 0; + jsonString[tokens[i].end] = 0; } - return true; + return tokens; } \ No newline at end of file diff --git a/JsonParser.h b/JsonParser.h index 3e034c37..ac578692 100644 --- a/JsonParser.h +++ b/JsonParser.h @@ -1,8 +1,7 @@ /* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ #ifndef __JSONPARSER_H #define __JSONPARSER_H @@ -16,35 +15,22 @@ public: JsonArray parseArray(char* json) { - if (!parse(json) || tokens[0].type != JSMN_ARRAY) - return JsonArray(); - - return JsonArray(json, tokens); + return JsonArray(json, parse(json)); } JsonHashTable parseHashTable(char* json) { - if (!parse(json) || tokens[0].type != JSMN_OBJECT) - return JsonHashTable(); - - return JsonHashTable(json, tokens); + return JsonHashTable(json, parse(json)); } protected: - JsonParserBase(jsmntok_t* tokens, int maxTokenCount) - { - this->maxTokenCount = maxTokenCount; - this->tokens = tokens; - - jsmn_init(&parser); - } + JsonParserBase(jsmntok_t* tokens, int maxTokenCount); - bool parse(char* json); - private: - char* buffer; + jsmntok_t* parse(char* json); + jsmn_parser parser; int maxTokenCount; jsmntok_t* tokens; @@ -58,7 +44,6 @@ public: JsonParser() : JsonParserBase(tokens, N) { - } private: