mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added namespace for the parser
This commit is contained in:
		| @@ -6,6 +6,8 @@ | |||||||
| #include "JsonArray.h" | #include "JsonArray.h" | ||||||
| #include "JsonHashTable.h" | #include "JsonHashTable.h" | ||||||
|  |  | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| JsonArray::JsonArray(char* json, jsmntok_t* tokens) | JsonArray::JsonArray(char* json, jsmntok_t* tokens) | ||||||
| : JsonObjectBase(json, tokens) | : JsonObjectBase(json, tokens) | ||||||
| { | { | ||||||
|   | |||||||
| @@ -7,33 +7,39 @@ | |||||||
|  |  | ||||||
| #include "JsonObjectBase.h" | #include "JsonObjectBase.h" | ||||||
|  |  | ||||||
| class JsonHashTable; | namespace ArduinoJson | ||||||
|  |  | ||||||
| class JsonArray : public JsonObjectBase |  | ||||||
| { | { | ||||||
| 	template <int N> |     namespace Parser | ||||||
| 	friend class JsonParser; |     { | ||||||
|  |         class JsonHashTable; | ||||||
|  |  | ||||||
| 	friend class JsonHashTable; |         class JsonArray : public JsonObjectBase | ||||||
|  |         { | ||||||
|  |             template <int N> | ||||||
|  |             friend class JsonParser; | ||||||
|  |  | ||||||
| public: |             friend class JsonHashTable; | ||||||
|  |  | ||||||
| 	JsonArray()	{} |         public: | ||||||
|  |  | ||||||
| 	int getLength() |             JsonArray()	{} | ||||||
| 	{ |  | ||||||
| 		return tokens != 0 ? tokens[0].size : 0; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	JsonArray getArray(int index); |             int getLength() | ||||||
| 	bool getBool(int index); |             { | ||||||
| 	double getDouble(int index); |                 return tokens != 0 ? tokens[0].size : 0; | ||||||
| 	JsonHashTable getHashTable(int index); |             } | ||||||
| 	long getLong(int index); |  | ||||||
| 	char* getString(int index); |  | ||||||
|  |  | ||||||
| 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); |         private: | ||||||
| 	jsmntok_t* getToken(int index); |  | ||||||
| }; |             JsonArray(char* json, jsmntok_t* tokens); | ||||||
|  |             jsmntok_t* getToken(int index); | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -3,10 +3,11 @@ | |||||||
| * Benoit Blanchon 2014 - MIT License | * Benoit Blanchon 2014 - MIT License | ||||||
| */ | */ | ||||||
|  |  | ||||||
|  | #include <string.h> // for strcmp() | ||||||
| #include "JsonArray.h" | #include "JsonArray.h" | ||||||
| #include "JsonHashTable.h" | #include "JsonHashTable.h" | ||||||
|  |  | ||||||
| #include <string.h> // for strcmp() | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) | JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens) | ||||||
| : JsonObjectBase(json, tokens) | : JsonObjectBase(json, tokens) | ||||||
|   | |||||||
| @@ -7,30 +7,36 @@ | |||||||
|  |  | ||||||
| #include "JsonObjectBase.h" | #include "JsonObjectBase.h" | ||||||
|  |  | ||||||
| class JsonArray; | namespace ArduinoJson | ||||||
|  |  | ||||||
| class JsonHashTable : public JsonObjectBase |  | ||||||
| { | { | ||||||
| 	template <int N> |     namespace Parser | ||||||
| 	friend class JsonParser; |     { | ||||||
|  |         class JsonArray; | ||||||
|  |  | ||||||
| 	friend class JsonArray; |         class JsonHashTable : public JsonObjectBase | ||||||
|  |         { | ||||||
|  |             template <int N> | ||||||
|  |             friend class JsonParser; | ||||||
|  |  | ||||||
| public: |             friend class JsonArray; | ||||||
|  |  | ||||||
| 	JsonHashTable() {} |         public: | ||||||
|  |  | ||||||
| 	bool containsKey(const char* key); |             JsonHashTable() {} | ||||||
|  |  | ||||||
| 	JsonArray getArray(const char* key); |             bool containsKey(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); |  | ||||||
|  |  | ||||||
| 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); |         private: | ||||||
| 	jsmntok_t* getToken(const char* key); |  | ||||||
| }; |             JsonHashTable(char* json, jsmntok_t* tokens); | ||||||
|  |             jsmntok_t* getToken(const char* key); | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -3,9 +3,10 @@ | |||||||
| * Benoit Blanchon 2014 - MIT License | * Benoit Blanchon 2014 - MIT License | ||||||
| */ | */ | ||||||
|  |  | ||||||
|  | #include <stdlib.h> // for strtol, strtod | ||||||
| #include "JsonObjectBase.h" | #include "JsonObjectBase.h" | ||||||
|  |  | ||||||
| #include <stdlib.h> // for strtol, strtod | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) | int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) | ||||||
| { | { | ||||||
|   | |||||||
| @@ -7,41 +7,47 @@ | |||||||
|  |  | ||||||
| #include "jsmn.h" | #include "jsmn.h" | ||||||
|  |  | ||||||
| class JsonObjectBase | namespace ArduinoJson | ||||||
| { | { | ||||||
| public: |     namespace Parser | ||||||
|  |     { | ||||||
|  |         class JsonObjectBase | ||||||
|  |         { | ||||||
|  |         public: | ||||||
|  |  | ||||||
| 	JsonObjectBase() |             JsonObjectBase() | ||||||
| 	{ |             { | ||||||
| 		makeInvalid(); |                 makeInvalid(); | ||||||
| 	} |             } | ||||||
|  |  | ||||||
| 	bool success()  |             bool success() | ||||||
| 	{  |             { | ||||||
| 		return json != 0 && tokens != 0; |                 return json != 0 && tokens != 0; | ||||||
| 	} |             } | ||||||
|  |  | ||||||
| protected: |         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); |  | ||||||
|  |  | ||||||
| 	bool getBoolFromToken(jsmntok_t* token); |             JsonObjectBase(char* json, jsmntok_t* tokens) | ||||||
| 	double getDoubleFromToken(jsmntok_t* token); |             { | ||||||
| 	long getLongFromToken(jsmntok_t* token); |                 this->json = json; | ||||||
| 	char* getStringFromToken(jsmntok_t* token); |                 this->tokens = tokens; | ||||||
|  |             } | ||||||
|  |  | ||||||
| 	char* json; |             void makeInvalid() | ||||||
| 	jsmntok_t* tokens; |             { | ||||||
| }; |                 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; | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -8,58 +8,64 @@ | |||||||
| #include "JsonHashTable.h" | #include "JsonHashTable.h" | ||||||
| #include "JsonArray.h" | #include "JsonArray.h" | ||||||
|  |  | ||||||
| /* | namespace ArduinoJson | ||||||
| * 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 <int MAX_TOKENS> |  | ||||||
| class JsonParser |  | ||||||
| { | { | ||||||
| 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 <int MAX_TOKENS> | ||||||
|  |         class JsonParser | ||||||
|  |         { | ||||||
|  |         public: | ||||||
|  |  | ||||||
| 	/* |             /* | ||||||
| 	* Parse the JSON string and return a array. |             * Parse the JSON string and return a array. | ||||||
| 	* |             * | ||||||
| 	* The content of the string may be altered to add '\0' at the |             * The content of the string may be altered to add '\0' at the | ||||||
| 	* end of string tokens |             * end of string tokens | ||||||
| 	*/  |             */ | ||||||
| 	JsonArray parseArray(char* json) |             JsonArray parseArray(char* json) | ||||||
| 	{ |             { | ||||||
| 		return JsonArray(json, parse(json)); |                 return JsonArray(json, parse(json)); | ||||||
| 	} |             } | ||||||
|  |  | ||||||
| 	/* |             /* | ||||||
| 	* Parse the JSON string and return a array. |             * Parse the JSON string and return a array. | ||||||
| 	* |             * | ||||||
| 	* The content of the string may be altered to add '\0' at the |             * The content of the string may be altered to add '\0' at the | ||||||
| 	* end of string tokens |             * end of string tokens | ||||||
| 	*/ |             */ | ||||||
| 	JsonHashTable parseHashTable(char* json) |             JsonHashTable parseHashTable(char* json) | ||||||
| 	{ |             { | ||||||
| 		return JsonHashTable(json, parse(json)); |                 return JsonHashTable(json, parse(json)); | ||||||
| 	} |             } | ||||||
|  |  | ||||||
| private: |         private: | ||||||
|  |  | ||||||
| 	jsmntok_t* parse(char* json) |             jsmntok_t* parse(char* json) | ||||||
| 	{ |             { | ||||||
| 		jsmn_parser parser; |                 jsmn_parser parser; | ||||||
| 		jsmn_init(&parser); |                 jsmn_init(&parser); | ||||||
|  |  | ||||||
| 		if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) |                 if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) | ||||||
| 			return 0; |                     return 0; | ||||||
|  |  | ||||||
| 		return tokens; |                 return tokens; | ||||||
| 	} |             } | ||||||
|  |  | ||||||
| 	jsmntok_t tokens[MAX_TOKENS]; |             jsmntok_t tokens[MAX_TOKENS]; | ||||||
| }; |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -7,6 +7,7 @@ | |||||||
| #include "JsonParser.h" | #include "JsonParser.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| namespace ArduinoJsonParserTests | namespace ArduinoJsonParserTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ | |||||||
| #include "JsonParser.h" | #include "JsonParser.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| namespace ArduinoJsonParserTests | namespace ArduinoJsonParserTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ | |||||||
|  |  | ||||||
| using namespace std; | using namespace std; | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| namespace ArduinoJsonParserTests | namespace ArduinoJsonParserTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ | |||||||
|  |  | ||||||
| using namespace std; | using namespace std; | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| namespace ArduinoJsonParserTests | namespace ArduinoJsonParserTests | ||||||
| {		 | {		 | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ | |||||||
|  |  | ||||||
| #include <JsonParser.h> | #include <JsonParser.h> | ||||||
|  |  | ||||||
|  | using namespace ArduinoJson::Parser; | ||||||
|  |  | ||||||
| void ParseAnObject() | void ParseAnObject() | ||||||
| { | { | ||||||
|     char json[] = "{\"Name\":\"Blanchon\",\"Skills\":[\"C\",\"C++\",\"C#\"],\"Age\":32,\"Online\":true}"; |     char json[] = "{\"Name\":\"Blanchon\",\"Skills\":[\"C\",\"C++\",\"C#\"],\"Age\":32,\"Online\":true}"; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user