mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Added a test that adds a string value in a hash
This commit is contained in:
		| @@ -12,27 +12,54 @@ class JsonHashTable : public JsonObjectBase | ||||
| { | ||||
| public: | ||||
|  | ||||
|     JsonHashTable() | ||||
|     { | ||||
|         itemCount = 0; | ||||
|     } | ||||
|  | ||||
|     void add(const char* key, const char* value) | ||||
|     { | ||||
|         ObjectValue v; | ||||
|         v.string = value; | ||||
|         addItem(key, JSON_STRING, v); | ||||
|     } | ||||
|  | ||||
|     using JsonObjectBase::writeTo; | ||||
|  | ||||
| private: | ||||
|  | ||||
|    /* struct KeyValuePair | ||||
|     struct KeyValuePair | ||||
|     { | ||||
|         const char* key; | ||||
|         ObjectContainer value; | ||||
|     };*/ | ||||
|     }; | ||||
|  | ||||
|     KeyValuePair items[N]; | ||||
|     int itemCount; | ||||
|  | ||||
|     virtual void writeTo(StringBuilder& sb) | ||||
|     { | ||||
|         sb.append("{"); | ||||
|  | ||||
|         /*for (int i = 0; i < itemCount; i++) | ||||
|         for (int i = 0; i < itemCount; i++) | ||||
|         { | ||||
|             if (i>0) sb.append(","); | ||||
|             writeObjectTo(items[i], sb); | ||||
|         }*/ | ||||
|             sb.appendEscaped(items[i].key); | ||||
|             sb.append(":"); | ||||
|             writeObjectTo(items[i].value, sb); | ||||
|         } | ||||
|  | ||||
|         sb.append("}"); | ||||
|     } | ||||
|  | ||||
|     void addItem(const char* key, ObjectType type, ObjectValue value) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|  | ||||
|         items[itemCount].key = key; | ||||
|         items[itemCount].value.type = type; | ||||
|         items[itemCount].value.value = value; | ||||
|         itemCount++; | ||||
|     } | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -16,8 +16,20 @@ namespace JsonGeneratorTests | ||||
|             assertJsonIs("{}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneString) | ||||
|         { | ||||
|             add("key", "value"); | ||||
|             assertJsonIs("{\"key\":\"value\"}"); | ||||
|         } | ||||
|  | ||||
|     private: | ||||
|  | ||||
|         template<typename T> | ||||
|         void add(const char* key, T value) | ||||
|         { | ||||
|             hash.add(key, value); | ||||
|         } | ||||
|  | ||||
|         void assertJsonIs(const char* expected) | ||||
|         { | ||||
|             char buffer[256]; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user