diff --git a/JsonParser/JsonArray.cpp b/JsonParser/JsonArray.cpp index dab52fbb..f1490baa 100644 --- a/JsonParser/JsonArray.cpp +++ b/JsonParser/JsonArray.cpp @@ -6,6 +6,8 @@ #include "JsonArray.h" #include "JsonHashTable.h" +using namespace ArduinoJson::Parser; + JsonArray::JsonArray(char* json, jsmntok_t* tokens) : JsonObjectBase(json, tokens) { diff --git a/JsonParser/JsonArray.h b/JsonParser/JsonArray.h index eb14ecfa..0633f91c 100644 --- a/JsonParser/JsonArray.h +++ b/JsonParser/JsonArray.h @@ -7,33 +7,39 @@ #include "JsonObjectBase.h" -class JsonHashTable; - -class JsonArray : public JsonObjectBase +namespace ArduinoJson { - template - friend class JsonParser; + namespace Parser + { + class JsonHashTable; - friend class JsonHashTable; + class JsonArray : public JsonObjectBase + { + template + friend class JsonParser; -public: + friend class JsonHashTable; - JsonArray() {} + public: - int getLength() - { - return tokens != 0 ? tokens[0].size : 0; - } + JsonArray() {} - JsonArray getArray(int index); - bool getBool(int index); - double getDouble(int index); - JsonHashTable getHashTable(int index); - long getLong(int index); - char* getString(int index); + int getLength() + { + return tokens != 0 ? tokens[0].size : 0; + } -private: + JsonArray getArray(int index); + bool getBool(int index); + double getDouble(int index); + JsonHashTable getHashTable(int index); + long getLong(int index); + char* getString(int index); - JsonArray(char* json, jsmntok_t* tokens); - jsmntok_t* getToken(int index); -}; \ No newline at end of file + 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 1073fbd3..513e5702 100644 --- a/JsonParser/JsonHashTable.cpp +++ b/JsonParser/JsonHashTable.cpp @@ -3,10 +3,11 @@ * Benoit Blanchon 2014 - MIT License */ +#include // for strcmp() #include "JsonArray.h" #include "JsonHashTable.h" -#include // for strcmp() +using namespace ArduinoJson::Parser; JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) : JsonObjectBase(json, tokens) diff --git a/JsonParser/JsonHashTable.h b/JsonParser/JsonHashTable.h index 5fe62c7e..a0139945 100644 --- a/JsonParser/JsonHashTable.h +++ b/JsonParser/JsonHashTable.h @@ -7,30 +7,36 @@ #include "JsonObjectBase.h" -class JsonArray; - -class JsonHashTable : public JsonObjectBase +namespace ArduinoJson { - template - friend class JsonParser; + namespace Parser + { + class JsonArray; - friend class JsonArray; + class JsonHashTable : public JsonObjectBase + { + template + friend class JsonParser; -public: + friend class JsonArray; - JsonHashTable() {} + public: - bool containsKey(const char* key); + JsonHashTable() {} - 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); -private: + 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); - JsonHashTable(char* json, jsmntok_t* tokens); - jsmntok_t* getToken(const char* key); -}; \ No newline at end of file + 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 994790ff..2a8092b3 100644 --- a/JsonParser/JsonObjectBase.cpp +++ b/JsonParser/JsonObjectBase.cpp @@ -3,9 +3,10 @@ * Benoit Blanchon 2014 - MIT License */ +#include // for strtol, strtod #include "JsonObjectBase.h" -#include // for strtol, strtod +using namespace ArduinoJson::Parser; int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) { diff --git a/JsonParser/JsonObjectBase.h b/JsonParser/JsonObjectBase.h index 9b9d8a60..6c40de04 100644 --- a/JsonParser/JsonObjectBase.h +++ b/JsonParser/JsonObjectBase.h @@ -7,41 +7,47 @@ #include "jsmn.h" -class JsonObjectBase +namespace ArduinoJson { -public: + namespace Parser + { + class JsonObjectBase + { + public: - JsonObjectBase() - { - makeInvalid(); - } + JsonObjectBase() + { + makeInvalid(); + } - bool success() - { - return json != 0 && tokens != 0; - } + bool success() + { + return json != 0 && tokens != 0; + } -protected: - - JsonObjectBase(char* json, jsmntok_t* tokens) - { - this->json = json; - this->tokens = tokens; - } - - void makeInvalid() - { - json = 0; - tokens = 0; - } - - static int getNestedTokenCount(jsmntok_t* token); + protected: - bool getBoolFromToken(jsmntok_t* token); - double getDoubleFromToken(jsmntok_t* token); - long getLongFromToken(jsmntok_t* token); - char* getStringFromToken(jsmntok_t* token); + JsonObjectBase(char* json, jsmntok_t* tokens) + { + this->json = json; + this->tokens = tokens; + } - char* json; - jsmntok_t* tokens; -}; + void makeInvalid() + { + json = 0; + tokens = 0; + } + + 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; + }; + } +} \ No newline at end of file diff --git a/JsonParser/JsonParser.h b/JsonParser/JsonParser.h index ff21f7ff..ac41c00c 100644 --- a/JsonParser/JsonParser.h +++ b/JsonParser/JsonParser.h @@ -8,58 +8,64 @@ #include "JsonHashTable.h" #include "JsonArray.h" -/* -* The JSON parser. -* -* You need to specifiy the number of token to be allocated for that parser. -* Values from 16 to 32 are recommended. -* The parser size will be MAX_TOKEN*8 bytes. -* Don't forget that the memory size of standard Arduino board is only 2KB -* -* CAUTION: JsonArray and JsonHashTable contain pointers to tokens of the -* JsonParser, so they need the JsonParser to be in memory to work. -* As a result, you must not create JsonArray and JsonHashTable that have a -* longer life that the JsonParser. -*/ -template -class JsonParser +namespace ArduinoJson { -public: + namespace Parser + { + /* + * The JSON parser. + * + * You need to specifiy the number of token to be allocated for that parser. + * Values from 16 to 32 are recommended. + * The parser size will be MAX_TOKEN*8 bytes. + * Don't forget that the memory size of standard Arduino board is only 2KB + * + * CAUTION: JsonArray and JsonHashTable contain pointers to tokens of the + * JsonParser, so they need the JsonParser to be in memory to work. + * As a result, you must not create JsonArray and JsonHashTable that have a + * longer life that the JsonParser. + */ + template + class JsonParser + { + public: - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonArray parseArray(char* json) - { - return JsonArray(json, parse(json)); - } + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonArray parseArray(char* json) + { + return JsonArray(json, parse(json)); + } - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonHashTable parseHashTable(char* json) - { - return JsonHashTable(json, parse(json)); - } + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonHashTable parseHashTable(char* json) + { + return JsonHashTable(json, parse(json)); + } -private: + private: - jsmntok_t* parse(char* json) - { - jsmn_parser parser; - jsmn_init(&parser); + jsmntok_t* parse(char* json) + { + jsmn_parser parser; + jsmn_init(&parser); - if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) - return 0; + if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) + return 0; - return tokens; - } + return tokens; + } - jsmntok_t tokens[MAX_TOKENS]; -}; \ No newline at end of file + jsmntok_t tokens[MAX_TOKENS]; + }; + } +} \ No newline at end of file diff --git a/JsonParserTests/TestArrayExample.cpp b/JsonParserTests/TestArrayExample.cpp index 01aa9578..976a3461 100644 --- a/JsonParserTests/TestArrayExample.cpp +++ b/JsonParserTests/TestArrayExample.cpp @@ -7,6 +7,7 @@ #include "JsonParser.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; namespace ArduinoJsonParserTests { diff --git a/JsonParserTests/TestArrays.cpp b/JsonParserTests/TestArrays.cpp index eae07f2f..df49c323 100644 --- a/JsonParserTests/TestArrays.cpp +++ b/JsonParserTests/TestArrays.cpp @@ -7,6 +7,7 @@ #include "JsonParser.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; namespace ArduinoJsonParserTests { diff --git a/JsonParserTests/TestGbathreeStrings.cpp b/JsonParserTests/TestGbathreeStrings.cpp index d5fa22b7..d7d7f38d 100644 --- a/JsonParserTests/TestGbathreeStrings.cpp +++ b/JsonParserTests/TestGbathreeStrings.cpp @@ -9,6 +9,7 @@ using namespace std; using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; namespace ArduinoJsonParserTests { diff --git a/JsonParserTests/TestHashTableExample.cpp b/JsonParserTests/TestHashTableExample.cpp index 214a2d66..9aba8470 100644 --- a/JsonParserTests/TestHashTableExample.cpp +++ b/JsonParserTests/TestHashTableExample.cpp @@ -9,6 +9,7 @@ using namespace std; using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Parser; namespace ArduinoJsonParserTests { diff --git a/examples/JsonParserExample/JsonParserExample.ino b/examples/JsonParserExample/JsonParserExample.ino index f5b633f5..094fe5a6 100644 --- a/examples/JsonParserExample/JsonParserExample.ino +++ b/examples/JsonParserExample/JsonParserExample.ino @@ -5,6 +5,8 @@ #include +using namespace ArduinoJson::Parser; + void ParseAnObject() { char json[] = "{\"Name\":\"Blanchon\",\"Skills\":[\"C\",\"C++\",\"C#\"],\"Age\":32,\"Online\":true}";