From e924bef4095ef18a190ae6a2cf266a85a862675a Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 11 Jan 2014 15:05:35 +0100 Subject: [PATCH] Splitted in several files --- ArduinoJsonParser.cpp | 90 ---------------------- ArduinoJsonParser.h | 169 ------------------------------------------ JsonArray.cpp | 30 ++++++++ JsonArray.h | 51 +++++++++++++ JsonHashTable.cpp | 45 +++++++++++ JsonHashTable.h | 45 +++++++++++ JsonObjectBase.cpp | 19 +++++ JsonObjectBase.h | 42 +++++++++++ JsonParser.cpp | 23 ++++++ JsonParser.h | 70 +++++++++++++++++ 10 files changed, 325 insertions(+), 259 deletions(-) delete mode 100644 ArduinoJsonParser.cpp delete mode 100644 ArduinoJsonParser.h create mode 100644 JsonArray.cpp create mode 100644 JsonArray.h create mode 100644 JsonHashTable.cpp create mode 100644 JsonHashTable.h create mode 100644 JsonObjectBase.cpp create mode 100644 JsonObjectBase.h create mode 100644 JsonParser.cpp create mode 100644 JsonParser.h diff --git a/ArduinoJsonParser.cpp b/ArduinoJsonParser.cpp deleted file mode 100644 index a3b54a5a..00000000 --- a/ArduinoJsonParser.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ - -#include "ArduinoJsonParser.h" - -int JsonObjectBase::getNestedTokenCounts(int tokenIndex) -{ - int count = 0; - - for (int i = 0; i < tokens[tokenIndex].size; i++) - { - count += 1 + getNestedTokenCounts(tokenIndex + 1 + i); - } - - return count; -} - -bool JsonParserBase::parse(char* jsonString) -{ - buffer = jsonString; - - if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount)) - return false; - - // Add null termination to each token - for (int i = 1; i < parser.toknext; i++) - { - buffer[tokens[i].end] = 0; - } - - return true; -} - -jsmntok_t* JsonHashTable::getToken(char* name) -{ - // skip first token, it's the whole object - int currentToken = 1; - - // Scan each keys - for (int i = 0; i < tokens[0].size / 2 ; i++) - { - // Get key token string - char* key = json + tokens[currentToken].start; - - // Compare with desired name - if (strcmp(name, key) == 0) - { - return &tokens[currentToken + 1]; - } - - // move forward: key + value + nested tokens - currentToken += 2 + getNestedTokenCounts(currentToken + 1); - } - - return NULL; -} - -JsonArray JsonHashTable::getArray(char* key) -{ - jsmntok_t* token = getToken(key); - return JsonArray(json, token); -} - -jsmntok_t* JsonArray::getToken(int index) -{ - if (json == NULL) return NULL; - if (tokens == NULL) return NULL; - if (index < 0) return NULL; - if (index >= tokens[0].size) return NULL; - - // skip first token, it's the whole object - int currentToken = 1; - - for (int i = 0; i < index; i++) - { - // move forward: current + nested tokens - currentToken += 1 + getNestedTokenCounts(currentToken); - } - - return &tokens[currentToken]; -} - -JsonArray JsonArray::getArray(int index) -{ - jsmntok_t* token = getToken(index); - return JsonArray(json, token); -} \ No newline at end of file diff --git a/ArduinoJsonParser.h b/ArduinoJsonParser.h deleted file mode 100644 index 7f0f6631..00000000 --- a/ArduinoJsonParser.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * malloc-free JSON parser for Arduino - * Benoit Blanchon 2014 - * MIT License - */ - -#ifndef __ARDUINOJSONPARSER_H -#define __ARDUINOJSONPARSER_H - -#include -#include "utility/jsmn.h" - -class JsonObjectBase -{ -public: - - JsonObjectBase() - { - json = NULL; - tokens = NULL; - } - - bool success() - { - return json != NULL && tokens != NULL; - } - -protected: - - JsonObjectBase(char* json, jsmntok_t* tokens) - { - this->json = json; - this->tokens = tokens; - } - - int getNestedTokenCounts(int tokenIndex); - - char* json; - jsmntok_t* tokens; -}; - -class JsonArray; - -class JsonHashTable : public JsonObjectBase -{ - friend class JsonParserBase; - -public: - - JsonHashTable() - { - - } - - char* getString(char* key) - { - jsmntok_t* token = getToken(key); - return token != NULL ? json + token->start : NULL; - } - - JsonArray getArray(char* key); - -private: - - JsonHashTable(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) - { - - } - - jsmntok_t* getToken(char* key); -}; - -class JsonArray : public JsonObjectBase -{ - friend class JsonParserBase; - friend class JsonHashTable; - -public: - -public: - - JsonArray() - { - - } - - JsonArray getArray(int index); - - char* getString(int index) - { - jsmntok_t* token = getToken(index); - return token != NULL ? json + token->start : NULL; - } - - int getLength() - { - return tokens != NULL ? tokens[0].size : 0; - } - -private: - - JsonArray(char* json, jsmntok_t* tokens) - : JsonObjectBase(json, tokens) - { - - } - - jsmntok_t* getToken(int index); -}; - -class JsonParserBase -{ -public: - - JsonArray parseArray(char* json) - { - if (!parse(json) || tokens[0].type != JSMN_ARRAY) - return JsonArray(); - - return JsonArray(json, tokens); - } - - JsonHashTable parseHashTable(char* json) - { - if (!parse(json) || tokens[0].type != JSMN_OBJECT) - return JsonHashTable(); - - return JsonHashTable(json, tokens); - } - -protected: - - JsonParserBase(jsmntok_t* tokens, int maxTokenCount) - { - this->maxTokenCount = maxTokenCount; - this->tokens = tokens; - - jsmn_init(&parser); - } - - bool parse(char* json); - -private: - - char* buffer; - jsmn_parser parser; - int maxTokenCount; - jsmntok_t* tokens; -}; - -template -class JsonParser : public JsonParserBase -{ -public: - - JsonParser() - : JsonParserBase(tokens, N) - { - - } - -private: - - jsmntok_t tokens[N]; -}; - -#endif - diff --git a/JsonArray.cpp b/JsonArray.cpp new file mode 100644 index 00000000..fe9d40b6 --- /dev/null +++ b/JsonArray.cpp @@ -0,0 +1,30 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#include "JsonArray.h" + +jsmntok_t* JsonArray::getToken(int index) +{ + if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) + return 0; + + // skip first token, it's the whole object + int currentToken = 1; + + for (int i = 0; i < index; i++) + { + // move forward: current + nested tokens + currentToken += 1 + getNestedTokenCounts(currentToken); + } + + return &tokens[currentToken]; +} + +JsonArray JsonArray::getArray(int index) +{ + jsmntok_t* token = getToken(index); + return JsonArray(json, token); +} \ No newline at end of file diff --git a/JsonArray.h b/JsonArray.h new file mode 100644 index 00000000..b83a0012 --- /dev/null +++ b/JsonArray.h @@ -0,0 +1,51 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#ifndef __JSONARRAY_H +#define __JSONARRAY_H + +#include "JsonObjectBase.h" + +class JsonArray : public JsonObjectBase +{ + friend class JsonParserBase; + friend class JsonHashTable; + +public: + +public: + + JsonArray() + { + + } + + JsonArray getArray(int index); + + char* getString(int index) + { + jsmntok_t* token = getToken(index); + return token != 0 ? json + token->start : 0; + } + + int getLength() + { + return tokens != 0 ? tokens[0].size : 0; + } + +private: + + JsonArray(char* json, jsmntok_t* tokens) + : JsonObjectBase(json, tokens) + { + + } + + jsmntok_t* getToken(int index); +}; + +#endif + diff --git a/JsonHashTable.cpp b/JsonHashTable.cpp new file mode 100644 index 00000000..ec23d201 --- /dev/null +++ b/JsonHashTable.cpp @@ -0,0 +1,45 @@ +/* + * 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) +{ + // sanity check + if (json == 0 || tokens == 0 || name == 0) + return 0; + + // skip first token, it's the whole object + int currentToken = 1; + + // scan each keys + for (int i = 0; i < tokens[0].size / 2 ; i++) + { + // Get key token string + char* key = json + tokens[currentToken].start; + + // Compare with desired name + if (strcmp(name, key) == 0) + { + return &tokens[currentToken + 1]; + } + + // move forward: key + value + nested tokens + currentToken += 2 + getNestedTokenCounts(currentToken + 1); + } + + // nothing found, return NULL + return 0; +} + +JsonArray JsonHashTable::getArray(char* key) +{ + jsmntok_t* token = getToken(key); + return JsonArray(json, token); +} \ No newline at end of file diff --git a/JsonHashTable.h b/JsonHashTable.h new file mode 100644 index 00000000..773714ff --- /dev/null +++ b/JsonHashTable.h @@ -0,0 +1,45 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#ifndef __JSONHASHTABLE_H +#define __JSONHASHTABLE_H + +#include "JsonObjectBase.h" + +class JsonArray; + +class JsonHashTable : public JsonObjectBase +{ + friend class JsonParserBase; + +public: + + JsonHashTable() + { + + } + + char* getString(char* key) + { + jsmntok_t* token = getToken(key); + return token != 0 ? json + token->start : 0; + } + + JsonArray getArray(char* key); + +private: + + JsonHashTable(char* json, jsmntok_t* tokens) + : JsonObjectBase(json, tokens) + { + + } + + jsmntok_t* getToken(char* key); +}; + +#endif + diff --git a/JsonObjectBase.cpp b/JsonObjectBase.cpp new file mode 100644 index 00000000..956ea703 --- /dev/null +++ b/JsonObjectBase.cpp @@ -0,0 +1,19 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#include "JsonObjectBase.h" + +int JsonObjectBase::getNestedTokenCounts(int tokenIndex) +{ + int count = 0; + + for (int i = 0; i < tokens[tokenIndex].size; i++) + { + count += 1 + getNestedTokenCounts(tokenIndex + 1 + i); + } + + return count; +} \ No newline at end of file diff --git a/JsonObjectBase.h b/JsonObjectBase.h new file mode 100644 index 00000000..9553d826 --- /dev/null +++ b/JsonObjectBase.h @@ -0,0 +1,42 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#ifndef __JSONOBJECTBASE_H +#define __JSONOBJECTBASE_H + +#include "utility/jsmn.h" + +class JsonObjectBase +{ +public: + + JsonObjectBase() + { + json = 0; + tokens = 0; + } + + bool success() + { + return json != 0 && tokens != 0; + } + +protected: + + JsonObjectBase(char* json, jsmntok_t* tokens) + { + this->json = json; + this->tokens = tokens; + } + + int getNestedTokenCounts(int tokenIndex); + + char* json; + jsmntok_t* tokens; +}; + +#endif + diff --git a/JsonParser.cpp b/JsonParser.cpp new file mode 100644 index 00000000..97bff18b --- /dev/null +++ b/JsonParser.cpp @@ -0,0 +1,23 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#include "JsonParser.h" + +bool JsonParserBase::parse(char* jsonString) +{ + buffer = jsonString; + + if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount)) + return false; + + // Add null termination to each token + for (int i = 1; i < parser.toknext; i++) + { + buffer[tokens[i].end] = 0; + } + + return true; +} \ No newline at end of file diff --git a/JsonParser.h b/JsonParser.h new file mode 100644 index 00000000..3e034c37 --- /dev/null +++ b/JsonParser.h @@ -0,0 +1,70 @@ +/* + * malloc-free JSON parser for Arduino + * Benoit Blanchon 2014 + * MIT License + */ + +#ifndef __JSONPARSER_H +#define __JSONPARSER_H + +#include "JsonHashTable.h" +#include "JsonArray.h" + +class JsonParserBase +{ +public: + + JsonArray parseArray(char* json) + { + if (!parse(json) || tokens[0].type != JSMN_ARRAY) + return JsonArray(); + + return JsonArray(json, tokens); + } + + JsonHashTable parseHashTable(char* json) + { + if (!parse(json) || tokens[0].type != JSMN_OBJECT) + return JsonHashTable(); + + return JsonHashTable(json, tokens); + } + +protected: + + JsonParserBase(jsmntok_t* tokens, int maxTokenCount) + { + this->maxTokenCount = maxTokenCount; + this->tokens = tokens; + + jsmn_init(&parser); + } + + bool parse(char* json); + +private: + + char* buffer; + jsmn_parser parser; + int maxTokenCount; + jsmntok_t* tokens; +}; + +template +class JsonParser : public JsonParserBase +{ +public: + + JsonParser() + : JsonParserBase(tokens, N) + { + + } + +private: + + jsmntok_t tokens[N]; +}; + +#endif +