mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Extracted class JsonValue
This commit is contained in:
		| @@ -19,30 +19,30 @@ public: | ||||
|      | ||||
|     void add(const char* value) | ||||
|     { | ||||
|         JsonValueContent v; | ||||
|         v.string = value; | ||||
|         addItem(JSON_STRING, v); | ||||
|         addItem(JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void add(double value) | ||||
|     { | ||||
|         JsonValueContent v; | ||||
|         v.number = value; | ||||
|         addItem(JSON_NUMBER, v); | ||||
|         addItem(JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void add(bool value) | ||||
|     { | ||||
|         JsonValueContent v; | ||||
|         v.boolean = value; | ||||
|         addItem(JSON_BOOLEAN, v); | ||||
|         addItem(JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void add(JsonObjectBase& value) | ||||
|     { | ||||
|         JsonValueContent v; | ||||
|         v.object = &value; | ||||
|         addItem(JSON_OBJECT, v); | ||||
|         addItem(JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void addItem(JsonValue value) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|  | ||||
|         items[itemCount] = value; | ||||
|         itemCount++; | ||||
|     } | ||||
|  | ||||
|     using JsonObjectBase::writeTo; | ||||
| @@ -58,19 +58,10 @@ private: | ||||
|         for (int i = 0; i < itemCount; i++) | ||||
|         { | ||||
|             if (i>0) sb.append(","); | ||||
|             writeValueTo(items[i], sb); | ||||
|             items[i].writeTo(sb); | ||||
|         } | ||||
|  | ||||
|         sb.append("]"); | ||||
|     } | ||||
|  | ||||
|     void addItem(JsonValueType type, JsonValueContent content) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|  | ||||
|         items[itemCount].type = type; | ||||
|         items[itemCount].content = content; | ||||
|         itemCount++; | ||||
|     } | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -84,7 +84,7 @@ | ||||
|   <ItemGroup> | ||||
|     <ClCompile Include="JsonArrayTests.cpp" /> | ||||
|     <ClCompile Include="JsonHashTableTests.cpp" /> | ||||
|     <ClCompile Include="JsonObjectBase.cpp" /> | ||||
|     <ClCompile Include="JsonValue.cpp" /> | ||||
|     <ClCompile Include="StringBuilder.cpp" /> | ||||
|     <ClCompile Include="StringBuilderAppendEscapedTests.cpp" /> | ||||
|     <ClCompile Include="StringBuilderAppendTests.cpp" /> | ||||
| @@ -93,6 +93,7 @@ | ||||
|     <ClInclude Include="JsonArray.h" /> | ||||
|     <ClInclude Include="JsonHashTable.h" /> | ||||
|     <ClInclude Include="JsonObjectBase.h" /> | ||||
|     <ClInclude Include="JsonValue.h" /> | ||||
|     <ClInclude Include="StringBuilder.h" /> | ||||
|   </ItemGroup> | ||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||||
|   | ||||
| @@ -21,9 +21,6 @@ | ||||
|     <ClCompile Include="StringBuilder.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="JsonObjectBase.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="StringBuilderAppendEscapedTests.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
| @@ -33,6 +30,9 @@ | ||||
|     <ClCompile Include="JsonHashTableTests.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|     <ClCompile Include="JsonValue.cpp"> | ||||
|       <Filter>Source Files</Filter> | ||||
|     </ClCompile> | ||||
|   </ItemGroup> | ||||
|   <ItemGroup> | ||||
|     <ClInclude Include="JsonArray.h"> | ||||
| @@ -47,5 +47,8 @@ | ||||
|     <ClInclude Include="JsonHashTable.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|     <ClInclude Include="JsonValue.h"> | ||||
|       <Filter>Header Files</Filter> | ||||
|     </ClInclude> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
| @@ -19,9 +19,16 @@ public: | ||||
|  | ||||
|     void add(const char* key, const char* value) | ||||
|     { | ||||
|         JsonValueContent v; | ||||
|         v.string = value; | ||||
|         addItem(key, JSON_STRING, v); | ||||
|         addItem(key, JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void addItem(const char* key, JsonValue value) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|  | ||||
|         items[itemCount].key = key; | ||||
|         items[itemCount].value = value; | ||||
|         itemCount++; | ||||
|     } | ||||
|  | ||||
|     using JsonObjectBase::writeTo; | ||||
| @@ -46,20 +53,10 @@ private: | ||||
|             if (i>0) sb.append(","); | ||||
|             sb.appendEscaped(items[i].key); | ||||
|             sb.append(":"); | ||||
|             writeValueTo(items[i].value, sb); | ||||
|             items[i].value.writeTo(sb); | ||||
|         } | ||||
|  | ||||
|         sb.append("}"); | ||||
|     } | ||||
|  | ||||
|     void addItem(const char* key, JsonValueType type, JsonValueContent content) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|  | ||||
|         items[itemCount].key = key; | ||||
|         items[itemCount].value.type = type; | ||||
|         items[itemCount].value.content = content; | ||||
|         itemCount++; | ||||
|     } | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -1,26 +0,0 @@ | ||||
| #include "JsonObjectBase.h" | ||||
|  | ||||
| void JsonObjectBase::writeValueTo(JsonValue& obj, StringBuilder& sb) | ||||
| { | ||||
|     switch (obj.type) | ||||
|     { | ||||
|     case JSON_STRING: | ||||
|         sb.appendEscaped(obj.content.string); | ||||
|         break; | ||||
|  | ||||
|     case JSON_NUMBER: | ||||
|         sb.append(obj.content.number); | ||||
|         break; | ||||
|  | ||||
|     case JSON_BOOLEAN: | ||||
|         sb.append(obj.content.boolean ? "true" : "false"); | ||||
|         break; | ||||
|  | ||||
|     case JSON_OBJECT: | ||||
|         if (obj.content.object) | ||||
|             obj.content.object->writeTo(sb); | ||||
|         else | ||||
|             sb.append("null"); | ||||
|         break; | ||||
|     } | ||||
| } | ||||
| @@ -5,7 +5,7 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "StringBuilder.h" | ||||
| #include "JsonValue.h" | ||||
|  | ||||
| class JsonObjectBase | ||||
| { | ||||
| @@ -17,32 +17,6 @@ public: | ||||
|         writeTo(sb); | ||||
|     } | ||||
|  | ||||
| protected: | ||||
|  | ||||
|     enum JsonValueType | ||||
|     { | ||||
|         JSON_STRING, | ||||
|         JSON_NUMBER, | ||||
|         JSON_BOOLEAN, | ||||
|         JSON_OBJECT, | ||||
|     }; | ||||
|  | ||||
|     union JsonValueContent | ||||
|     { | ||||
|         const char*     string; | ||||
|         double          number; | ||||
|         bool            boolean; | ||||
|         JsonObjectBase* object; | ||||
|     }; | ||||
|  | ||||
|     struct JsonValue | ||||
|     { | ||||
|         JsonValueType    type; | ||||
|         JsonValueContent content; | ||||
|     }; | ||||
|  | ||||
|     void writeValueTo(JsonValue& obj, StringBuilder& sb); | ||||
|  | ||||
|     virtual void writeTo(StringBuilder& sb) = 0; | ||||
| }; | ||||
|  | ||||
|   | ||||
							
								
								
									
										32
									
								
								JsonGeneratorTests/JsonValue.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								JsonGeneratorTests/JsonValue.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| /* | ||||
|  * Arduino JSON library | ||||
|  * Benoit Blanchon 2014 - MIT License | ||||
|  */ | ||||
|  | ||||
| #include "JsonValue.h" | ||||
| #include "JsonObjectBase.h" | ||||
|  | ||||
| void JsonValue::writeTo(StringBuilder& sb) | ||||
| { | ||||
|     switch (type) | ||||
|     { | ||||
|     case JSON_STRING: | ||||
|         sb.appendEscaped(content.string); | ||||
|         break; | ||||
|  | ||||
|     case JSON_NUMBER: | ||||
|         sb.append(content.number); | ||||
|         break; | ||||
|  | ||||
|     case JSON_BOOLEAN: | ||||
|         sb.append(content.boolean ? "true" : "false"); | ||||
|         break; | ||||
|  | ||||
|     case JSON_OBJECT: | ||||
|         if (content.object) | ||||
|             ((JsonObjectBase*)content.object)->writeTo(sb); | ||||
|         else | ||||
|             sb.append("null"); | ||||
|         break; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										67
									
								
								JsonGeneratorTests/JsonValue.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								JsonGeneratorTests/JsonValue.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | ||||
| /* | ||||
|  * Arduino JSON library | ||||
|  * Benoit Blanchon 2014 - MIT License | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "StringBuilder.h" | ||||
|  | ||||
| class JsonObjectBase; | ||||
|  | ||||
| class JsonValue | ||||
| {     | ||||
| public: | ||||
|  | ||||
|     JsonValue() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     JsonValue(const char* value) | ||||
|         : type(JSON_STRING) | ||||
|     { | ||||
|         content.string = value; | ||||
|     } | ||||
|  | ||||
|     JsonValue(double value) | ||||
|         : type(JSON_NUMBER) | ||||
|     { | ||||
|         content.number = value; | ||||
|     } | ||||
|  | ||||
|     JsonValue(bool value) | ||||
|         : type(JSON_BOOLEAN) | ||||
|     { | ||||
|         content.boolean = value; | ||||
|     } | ||||
|  | ||||
|     JsonValue(JsonObjectBase& value) | ||||
|         : type(JSON_OBJECT) | ||||
|     { | ||||
|         content.object = &value; | ||||
|     } | ||||
|  | ||||
|     void writeTo(StringBuilder& sb); | ||||
|  | ||||
| private: | ||||
|  | ||||
|     enum Type | ||||
|     { | ||||
|         JSON_STRING, | ||||
|         JSON_NUMBER, | ||||
|         JSON_BOOLEAN, | ||||
|         JSON_OBJECT, | ||||
|     }; | ||||
|  | ||||
|     union Content | ||||
|     { | ||||
|         const char*     string; | ||||
|         double          number; | ||||
|         bool            boolean; | ||||
|         JsonObjectBase* object; | ||||
|     }; | ||||
|  | ||||
|     Type    type; | ||||
|     Content content; | ||||
| }; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user