mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added class JsonValue.
Added subscript operator on JsonArray and JsonHashTable
This commit is contained in:
		| @@ -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)); | ||||
| } | ||||
|  | ||||
| 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)); | ||||
|     return (JsonHashTable) (*this)[index]; | ||||
| } | ||||
| @@ -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); | ||||
|         }; | ||||
|     } | ||||
| } | ||||
| @@ -4,8 +4,9 @@ | ||||
| */ | ||||
|  | ||||
| #include <string.h> // 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]; | ||||
| } | ||||
| @@ -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); | ||||
|         }; | ||||
|     } | ||||
| } | ||||
| @@ -3,7 +3,6 @@ | ||||
| * Benoit Blanchon 2014 - MIT License | ||||
| */ | ||||
|  | ||||
| #include <stdlib.h> // for strtol, strtod | ||||
| #include "JsonObjectBase.h" | ||||
|  | ||||
| using namespace ArduinoJson::Parser; | ||||
| @@ -23,45 +22,3 @@ 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; | ||||
| } | ||||
| @@ -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; | ||||
|         }; | ||||
|   | ||||
							
								
								
									
										63
									
								
								JsonParser/JsonValue.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								JsonParser/JsonValue.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| /* | ||||
|  * Arduino JSON library | ||||
|  * Benoit Blanchon 2014 - MIT License | ||||
|  */ | ||||
|  | ||||
| #include <stdlib.h> // 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); | ||||
| } | ||||
							
								
								
									
										52
									
								
								JsonParser/JsonValue.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								JsonParser/JsonValue.h
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||
|         }; | ||||
|     } | ||||
| } | ||||
| @@ -90,6 +90,7 @@ | ||||
|     <ClCompile Include="..\JsonParser\JsonHashTable.cpp" /> | ||||
|     <ClCompile Include="..\JsonParser\JsonObjectBase.cpp" /> | ||||
|     <ClCompile Include="..\JsonParser\JsonParserBase.cpp" /> | ||||
|     <ClCompile Include="..\JsonParser\JsonValue.cpp" /> | ||||
|     <ClCompile Include="JsonArrayTests.cpp" /> | ||||
|     <ClCompile Include="JsonHashTableTests.cpp" /> | ||||
|     <ClCompile Include="GbathreeBug.cpp" /> | ||||
| @@ -101,6 +102,7 @@ | ||||
|     <ClInclude Include="..\JsonParser\JsonObjectBase.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonParser.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonParserBase.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonValue.h" /> | ||||
|   </ItemGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
|   <ImportGroup Label="ExtensionTargets"> | ||||
|   | ||||
| @@ -39,6 +39,9 @@ | ||||
|     <ClCompile Include="..\JsonParser\JsonParserBase.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="..\JsonParser\JsonValue.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="..\JsonParser\jsmn.h"> | ||||
| @@ -59,5 +62,8 @@ | ||||
|     <ClInclude Include="..\JsonParser\JsonParserBase.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="..\JsonParser\JsonValue.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
		Reference in New Issue
	
	Block a user