diff --git a/JsonGenerator/JsonArray.h b/JsonGenerator/JsonArray.h index c155eb29..bf9311dd 100644 --- a/JsonGenerator/JsonArray.h +++ b/JsonGenerator/JsonArray.h @@ -24,20 +24,17 @@ namespace ArduinoJson template void add(T value) { - add(JsonValue(value)); + if (itemCount >= N) return; + + items[itemCount++].set(value); } - void add(double value, int digits = 2) - { - add(JsonValue(value, digits)); - } - - void add(JsonValue value) + template + void add(double value) { if (itemCount >= N) return; - items[itemCount] = value; - itemCount++; + items[itemCount++].set(value); } using JsonObjectBase::printTo; diff --git a/JsonGenerator/JsonHashTable.h b/JsonGenerator/JsonHashTable.h index d5098918..5acc6a2f 100644 --- a/JsonGenerator/JsonHashTable.h +++ b/JsonGenerator/JsonHashTable.h @@ -24,20 +24,20 @@ namespace ArduinoJson template void add(const char* key, T value) { - add(key, JsonValue(value)); + if (itemCount >= N) return; + + items[itemCount].key = key; + items[itemCount].value.set(value); + itemCount++; } - void add(const char* key, double value, int digits = 2) - { - add(key, JsonValue(value, digits)); - } - - void add(const char* key, JsonValue value) + template + void add(const char* key, double value) { if (itemCount >= N) return; items[itemCount].key = key; - items[itemCount].value = value; + items[itemCount].value.set(value); itemCount++; } @@ -62,7 +62,8 @@ namespace ArduinoJson for (int i = 0; i < itemCount; i++) { - JsonValue key(items[i].key); + JsonValue key; + key.set(items[i].key); if (i > 0) { diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index 0a4ab504..cf2defc8 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -15,48 +15,45 @@ namespace ArduinoJson class JsonValue { public: - - JsonValue() - { - } - - JsonValue(bool value) - : printToImpl(&printBoolTo) + + void set(bool value) { + printToImpl = &printBoolTo; content.asBool = value; } - JsonValue(double value, uint8_t digits = 2) - : printToImpl(&printDoubleTo) - { - content.asDouble.value = value; - content.asDouble.digits = digits; - } - - JsonValue(long value) - : printToImpl(&printLongTo) + void set(long value) { + printToImpl = &printLongTo; content.asLong = value; } - JsonValue(int value) - : printToImpl(&printLongTo) + void set(int value) { + printToImpl = &printLongTo; content.asLong = value; } - JsonValue(Printable& value) - : printToImpl(&printPrintableTo) + void set(Printable& value) { + printToImpl = &printPrintableTo; content.asPrintable = &value; } - JsonValue(const char* value) - : printToImpl(&printStringTo) + void set(const char* value) { + printToImpl = &printStringTo; content.asString = value; } + template + void set(double value) + { + printToImpl = &printDoubleTo; + content.asDouble.value = value; + content.asDouble.digits = DIGITS; + } + size_t printTo(Print& p) const { // handmade polymorphism diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 07e61f37..db71d1f1 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -57,7 +57,7 @@ namespace JsonGeneratorTests TEST_METHOD(OneDouble) { - addValue(3.14159265358979323846, 4); + addValue<4>(3.14159265358979323846); jsonIs("[3.1416]"); } @@ -158,7 +158,7 @@ namespace JsonGeneratorTests void addNested(JsonObjectBase& value) { - arr.add(value); + arr.add(value); } template @@ -167,9 +167,10 @@ namespace JsonGeneratorTests arr.add(value); } - void addValue(double value, int digits) + template + void addValue(double value) { - arr.add(value, digits); + arr.add(value); } void jsonIs(const char* expected) diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index d2f2a899..d05ae3b7 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -51,7 +51,7 @@ namespace JsonGeneratorTests TEST_METHOD(OneDouble) { - addValue("key", 3.14159265358979323846, 4); + addValue<4>("key", 3.14159265358979323846); jsonIs("{\"key\":3.1416}"); } @@ -93,7 +93,7 @@ namespace JsonGeneratorTests void addNested(const char* key, JsonObjectBase& value) { - hash.add(key, value); + hash.add(key, value); } template @@ -102,9 +102,10 @@ namespace JsonGeneratorTests hash.add(key, value); } - void addValue(const char* key, double value, int digits) + template + void addValue(const char* key, double value) { - hash.add(key, value, digits); + hash.add(key, value); } void jsonIs(const char* expected) diff --git a/JsonGeneratorTests/JsonValueTests.cpp b/JsonGeneratorTests/JsonValueTests.cpp index 0ba22684..01386b09 100644 --- a/JsonGeneratorTests/JsonValueTests.cpp +++ b/JsonGeneratorTests/JsonValueTests.cpp @@ -73,28 +73,28 @@ namespace JsonGeneratorTests write("\t"); assertResultIs("\"\\t\""); } - + /* TEST_METHOD(DoubleDefaultDigits) { write(3.14159265358979323846); assertResultIs("3.14"); - } + }*/ TEST_METHOD(DoubleZeroDigits) { - write(3.14159265358979323846, 0); + write<0>(3.14159265358979323846); assertResultIs("3"); } TEST_METHOD(DoubleOneDigit) { - write(3.14159265358979323846, 1); + write<1>(3.14159265358979323846); assertResultIs("3.1"); } TEST_METHOD(DoubleTwoDigits) { - write(3.14159265358979323846, 2); + write<2>(3.14159265358979323846); assertResultIs("3.14"); } @@ -120,13 +120,18 @@ namespace JsonGeneratorTests void write(T value) { StringBuilder sb(buffer, sizeof(buffer)); - returnValue = JsonValue(value).printTo(sb); + JsonValue jsonValue; + jsonValue.set(value); + returnValue = jsonValue.printTo(sb); } - void write(double value, int digits) + template + void write(double value) { StringBuilder sb(buffer, sizeof(buffer)); - returnValue = JsonValue(value, digits).printTo(sb); + JsonValue jsonValue; + jsonValue.set(value); + returnValue = jsonValue.printTo(sb); } void assertResultIs(const char* expected)