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