mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added JsonObjectIterator
This commit is contained in:
		| @@ -22,10 +22,9 @@ namespace ArduinoJson | ||||
|  | ||||
|             } | ||||
|  | ||||
|             const JsonArrayIterator& operator++() | ||||
|             void operator++() | ||||
|             { | ||||
|                 token = token.nextSibling(); | ||||
|                 return *this; | ||||
|             } | ||||
|  | ||||
|             JsonValue operator*() const | ||||
|   | ||||
| @@ -6,6 +6,7 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "JsonValue.h" | ||||
| #include "JsonObjectIterator.h" | ||||
|  | ||||
| namespace ArduinoJson | ||||
| { | ||||
| @@ -43,6 +44,16 @@ namespace ArduinoJson | ||||
|                 return getValue(key).success(); | ||||
|             } | ||||
|  | ||||
|             JsonObjectIterator begin() | ||||
|             { | ||||
|                 return JsonObjectIterator(json, token.firstChild()); | ||||
|             } | ||||
|  | ||||
|             JsonObjectIterator end() | ||||
|             { | ||||
|                 return JsonObjectIterator(json, token.nextSibling()); | ||||
|             } | ||||
|  | ||||
|             DEPRECATED JsonArray getArray(const char* key); | ||||
|  | ||||
|             DEPRECATED bool getBool(const char* key) | ||||
|   | ||||
							
								
								
									
										47
									
								
								JsonParser/JsonObjectIterator.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								JsonParser/JsonObjectIterator.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| /* | ||||
| * Arduino JSON library | ||||
| * Benoit Blanchon 2014 - MIT License | ||||
| */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "JsonValue.h" | ||||
| #include "JsonPair.h" | ||||
| #include "JsonToken.h" | ||||
|  | ||||
| namespace ArduinoJson | ||||
| { | ||||
|     namespace Parser | ||||
|     { | ||||
|         class JsonObjectIterator | ||||
|         { | ||||
|         public: | ||||
|  | ||||
|             JsonObjectIterator(char* json, Internal::JsonToken token) | ||||
|                 : json(json), token(token) | ||||
|             { | ||||
|  | ||||
|             } | ||||
|  | ||||
|             void operator++() | ||||
|             { | ||||
|                 token = token.nextSibling().nextSibling(); | ||||
|             } | ||||
|  | ||||
|             JsonPair operator*() const | ||||
|             { | ||||
|                 return JsonPair(json, token); | ||||
|             } | ||||
|  | ||||
|             bool operator !=(const JsonObjectIterator& other) | ||||
|             { | ||||
|                 return token != other.token; | ||||
|             } | ||||
|  | ||||
|         private: | ||||
|  | ||||
|             char* json; | ||||
|             Internal::JsonToken token; | ||||
|         }; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										39
									
								
								JsonParser/JsonPair.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								JsonParser/JsonPair.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| /* | ||||
| * Arduino JSON library | ||||
| * Benoit Blanchon 2014 - MIT License | ||||
| */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "JsonValue.h" | ||||
|  | ||||
| namespace ArduinoJson | ||||
| { | ||||
|     namespace Parser | ||||
|     { | ||||
|         class JsonPair | ||||
|         { | ||||
|         public: | ||||
|  | ||||
|             JsonPair(char* json, Internal::JsonToken token) | ||||
|                 : json(json), token(token) | ||||
|             { | ||||
|  | ||||
|             } | ||||
|  | ||||
|             const char* key() | ||||
|             { | ||||
|                 return token.getText(json); | ||||
|             } | ||||
|  | ||||
|             JsonValue value() | ||||
|             { | ||||
|                 return JsonValue(json, token.nextSibling()); | ||||
|             } | ||||
|  | ||||
|         private: | ||||
|             char* json; | ||||
|             Internal::JsonToken token; | ||||
|         }; | ||||
|     } | ||||
| } | ||||
| @@ -7,25 +7,28 @@ using namespace ArduinoJson::Parser; | ||||
|  | ||||
| namespace JsonParserTests | ||||
| { | ||||
| 	TEST_CLASS(JsonArrayIteratorTests) | ||||
| 	{ | ||||
| 	public: | ||||
| 		 | ||||
| 		TEST_METHOD(ThreeIntegers) | ||||
| 		{ | ||||
|             char json [] = "[1,2,3]"; | ||||
|             long expected [] = { 1, 2, 3 }; | ||||
|             JsonParser<4> parser; | ||||
|     TEST_CLASS(JsonObjectIteratorTests) | ||||
|     { | ||||
|     public: | ||||
|  | ||||
|             JsonArray a = parser.parse(json); | ||||
|         TEST_METHOD(ThreeStrings) | ||||
|         { | ||||
|             char json [] = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}"; | ||||
|             char* expectedKeys [] = { "key1", "key2", "key3" }; | ||||
|             char* expectedValues [] = { "value1", "value2", "value3" }; | ||||
|             JsonParser<7> parser; | ||||
|  | ||||
|             JsonHashTable a = parser.parse(json); | ||||
|  | ||||
|             int index = 0; | ||||
|  | ||||
|             for (long i : a) | ||||
|             for (auto i : a) | ||||
|             { | ||||
|                 Assert::AreEqual(expected[index++], i); | ||||
|                 Assert::AreEqual(expectedKeys[index], i.key()); | ||||
|                 Assert::AreEqual(expectedValues[index], (const char*) i.value()); | ||||
|                 index++; | ||||
|             } | ||||
| 		} | ||||
|         } | ||||
|  | ||||
| 	}; | ||||
|     }; | ||||
| } | ||||
							
								
								
									
										31
									
								
								JsonParserTests/JsonObjectIteratorTests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								JsonParserTests/JsonObjectIteratorTests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| #include "CppUnitTest.h" | ||||
| #include "JsonParser.h" | ||||
|  | ||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||
| using namespace ArduinoJson::Parser; | ||||
|  | ||||
|  | ||||
| namespace JsonParserTests | ||||
| { | ||||
| 	TEST_CLASS(JsonArrayIteratorTests) | ||||
| 	{ | ||||
| 	public: | ||||
| 		 | ||||
| 		TEST_METHOD(ThreeIntegers) | ||||
| 		{ | ||||
|             char json [] = "[1,2,3]"; | ||||
|             long expected [] = { 1, 2, 3 }; | ||||
|             JsonParser<4> parser; | ||||
|  | ||||
|             JsonArray a = parser.parse(json); | ||||
|  | ||||
|             int index = 0; | ||||
|  | ||||
|             for (long i : a) | ||||
|             { | ||||
|                 Assert::AreEqual(expected[index++], i); | ||||
|             } | ||||
| 		} | ||||
|  | ||||
| 	}; | ||||
| } | ||||
| @@ -93,6 +93,7 @@ | ||||
|     <ClCompile Include="..\JsonParser\JsonValue.cpp" /> | ||||
|     <ClCompile Include="JsonArrayIteratorTests.cpp" /> | ||||
|     <ClCompile Include="JsonArrayTests.cpp" /> | ||||
|     <ClCompile Include="JsonObjectIteratorTests.cpp" /> | ||||
|     <ClCompile Include="JsonObjectTests.cpp" /> | ||||
|     <ClCompile Include="GbathreeBug.cpp" /> | ||||
|   </ItemGroup> | ||||
| @@ -101,6 +102,8 @@ | ||||
|     <ClInclude Include="..\JsonParser\JsonArray.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonArrayIterator.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonObject.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonObjectIterator.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonPair.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonParser.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonParserBase.h" /> | ||||
|     <ClInclude Include="..\JsonParser\JsonToken.h" /> | ||||
|   | ||||
| @@ -45,6 +45,9 @@ | ||||
|     <ClCompile Include="JsonObjectTests.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="JsonObjectIteratorTests.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="..\JsonParser\jsmn.h"> | ||||
| @@ -71,5 +74,11 @@ | ||||
|     <ClInclude Include="..\JsonParser\JsonObject.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="..\JsonParser\JsonPair.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="..\JsonParser\JsonObjectIterator.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
		Reference in New Issue
	
	Block a user