mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Added overload to add() to specify the double precision
This commit is contained in:
		| @@ -23,6 +23,11 @@ public: | ||||
|         add(JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void add(double value, int digits=2) | ||||
|     { | ||||
|         add(JsonValue(value, digits)); | ||||
|     } | ||||
|  | ||||
|     void add(JsonValue value) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|   | ||||
| @@ -23,6 +23,11 @@ public: | ||||
|         add(key, JsonValue(value)); | ||||
|     } | ||||
|  | ||||
|     void add(const char* key, double value, int digits=2) | ||||
|     { | ||||
|         add(key, JsonValue(value, digits)); | ||||
|     } | ||||
|  | ||||
|     void add(const char* key, JsonValue value) | ||||
|     { | ||||
|         if (itemCount >= N) return; | ||||
|   | ||||
| @@ -54,6 +54,12 @@ namespace JsonGeneratorTests | ||||
|             jsonIs("[\"hello\",\"world\"]"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneDouble) | ||||
|         { | ||||
|             addValue(3.14159265358979323846, 4); | ||||
|             jsonIs("[3.1416]"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneInteger) | ||||
|         { | ||||
|             addValue(1); | ||||
| @@ -160,6 +166,11 @@ namespace JsonGeneratorTests | ||||
|             arr.add(value); | ||||
|         } | ||||
|  | ||||
|         void addValue(double value, int digits) | ||||
|         { | ||||
|             arr.add(value, digits); | ||||
|         } | ||||
|  | ||||
|         void jsonIs(const char* expected) | ||||
|         {       | ||||
|             arr.printTo(buffer, sizeof(buffer)); | ||||
|   | ||||
| @@ -15,7 +15,6 @@ namespace JsonGeneratorTests | ||||
|          | ||||
|         TEST_METHOD(Empty) | ||||
|         { | ||||
|             returnValueIs(2); | ||||
|             jsonIs("{}"); | ||||
|         } | ||||
|  | ||||
| @@ -23,7 +22,6 @@ namespace JsonGeneratorTests | ||||
|         { | ||||
|             addValue("key", "value"); | ||||
|  | ||||
|             returnValueIs(15); | ||||
|             jsonIs("{\"key\":\"value\"}"); | ||||
|         } | ||||
|  | ||||
| @@ -32,7 +30,6 @@ namespace JsonGeneratorTests | ||||
|             addValue("key1", "value1"); | ||||
|             addValue("key2", "value2"); | ||||
|  | ||||
|             returnValueIs(33); | ||||
|             jsonIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); | ||||
|         } | ||||
|  | ||||
| @@ -42,39 +39,36 @@ namespace JsonGeneratorTests | ||||
|             addValue("key2", "value2"); | ||||
|             addValue("key3", "value3"); | ||||
|  | ||||
|             returnValueIs(33); | ||||
|             jsonIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneInteger) | ||||
|         { | ||||
|             addValue("key", 1); | ||||
|  | ||||
|             returnValueIs(9); | ||||
|             jsonIs("{\"key\":1}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneDouble) | ||||
|         { | ||||
|             addValue("key", 3.14159265358979323846, 4); | ||||
|             jsonIs("{\"key\":3.1416}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneNull) | ||||
|         { | ||||
|             addValue("key", (char*) 0); | ||||
|  | ||||
|             returnValueIs(12); | ||||
|             jsonIs("{\"key\":null}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneTrue) | ||||
|         { | ||||
|             addValue("key", true); | ||||
|  | ||||
|             returnValueIs(12); | ||||
|             jsonIs("{\"key\":true}"); | ||||
|         } | ||||
|  | ||||
|         TEST_METHOD(OneFalse) | ||||
|         { | ||||
|             addValue("key", false); | ||||
|  | ||||
|             returnValueIs(13); | ||||
|             jsonIs("{\"key\":false}"); | ||||
|         } | ||||
|  | ||||
| @@ -83,7 +77,6 @@ namespace JsonGeneratorTests | ||||
|             JsonArray<1> nestedArray; | ||||
|             addNested("key", nestedArray); | ||||
|  | ||||
|             returnValueIs(10); | ||||
|             jsonIs("{\"key\":[]}"); | ||||
|         } | ||||
|  | ||||
| @@ -92,7 +85,6 @@ namespace JsonGeneratorTests | ||||
|             JsonHashTable<1> nestedHash; | ||||
|             addNested("key", nestedHash); | ||||
|  | ||||
|             returnValueIs(10); | ||||
|             jsonIs("{\"key\":{}}"); | ||||
|         } | ||||
|  | ||||
| @@ -109,16 +101,16 @@ namespace JsonGeneratorTests | ||||
|             hash.add(key, value); | ||||
|         } | ||||
|  | ||||
|         void jsonIs(const char* expected) | ||||
|         {             | ||||
|             hash.printTo(buffer, sizeof(buffer)); | ||||
|             Assert::AreEqual(expected, buffer); | ||||
|         void addValue(const char* key, double value, int digits) | ||||
|         { | ||||
|             hash.add(key, value, digits); | ||||
|         } | ||||
|  | ||||
|         void returnValueIs(size_t expected) | ||||
|         { | ||||
|         void jsonIs(const char* expected) | ||||
|         {             | ||||
|             size_t actual = hash.printTo(buffer, sizeof(buffer)); | ||||
|             Assert::AreEqual(expected, actual); | ||||
|             Assert::AreEqual(expected, buffer); | ||||
|             Assert::AreEqual(strlen(expected), actual); | ||||
|         } | ||||
|     }; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user