From 99521cc7180c029b139e2302d1565e6850e14407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Mon, 7 Jul 2014 13:59:31 +0200 Subject: [PATCH] Removed default template value for DIGITS, because Arduino 1.0.5 refused it --- JsonGenerator/JsonArray.h | 5 +++++ JsonGenerator/JsonHashTable.h | 5 +++++ JsonGenerator/JsonValue.h | 2 +- JsonGeneratorTests/JsonArrayTests.cpp | 8 ++++++- JsonGeneratorTests/JsonHashTableTests.cpp | 8 ++++++- JsonGeneratorTests/JsonValueTests.cpp | 26 +++++++++-------------- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/JsonGenerator/JsonArray.h b/JsonGenerator/JsonArray.h index bf9311dd..e7cbcc4a 100644 --- a/JsonGenerator/JsonArray.h +++ b/JsonGenerator/JsonArray.h @@ -29,6 +29,11 @@ namespace ArduinoJson items[itemCount++].set(value); } + void add(double value) + { + add<2>(value); + } + template void add(double value) { diff --git a/JsonGenerator/JsonHashTable.h b/JsonGenerator/JsonHashTable.h index efbdf35c..fc4e0200 100644 --- a/JsonGenerator/JsonHashTable.h +++ b/JsonGenerator/JsonHashTable.h @@ -42,6 +42,11 @@ namespace ArduinoJson itemCount++; } + void add(const char* key, double value) + { + add<2>(key, value); + } + using JsonObjectBase::printTo; private: diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index cf45aa93..a72507bc 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -47,7 +47,7 @@ namespace ArduinoJson content.asString.set(value); } - template + template void set(double value) { printToImpl = &printDoubleTo; diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index db71d1f1..9a4e5426 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -55,7 +55,13 @@ namespace JsonGeneratorTests jsonIs("[\"hello\",\"world\"]"); } - TEST_METHOD(OneDouble) + TEST_METHOD(OneDoubleDefaultDigits) + { + addValue(3.14159265358979323846); + jsonIs("[3.14]"); + } + + TEST_METHOD(OneDoubleFourDigits) { addValue<4>(3.14159265358979323846); jsonIs("[3.1416]"); diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index d05ae3b7..7c70a786 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -49,12 +49,18 @@ namespace JsonGeneratorTests jsonIs("{\"key\":1}"); } - TEST_METHOD(OneDouble) + TEST_METHOD(OneDoubleFourDigits) { addValue<4>("key", 3.14159265358979323846); jsonIs("{\"key\":3.1416}"); } + TEST_METHOD(OneDoubleDefaultDigits) + { + addValue("key", 3.14159265358979323846); + jsonIs("{\"key\":3.14}"); + } + TEST_METHOD(OneNull) { addValue("key", (char*) 0); diff --git a/JsonGeneratorTests/JsonValueTests.cpp b/JsonGeneratorTests/JsonValueTests.cpp index ae4d9cfb..de3a82f6 100644 --- a/JsonGeneratorTests/JsonValueTests.cpp +++ b/JsonGeneratorTests/JsonValueTests.cpp @@ -73,12 +73,6 @@ namespace JsonGeneratorTests write("\t"); assertResultIs("\"\\t\""); } - - TEST_METHOD(DoubleDefaultDigits) - { - write(3.14159265358979323846); - assertResultIs("3.14"); - } TEST_METHOD(DoubleZeroDigits) { @@ -97,7 +91,7 @@ namespace JsonGeneratorTests write<2>(3.14159265358979323846); assertResultIs("3.14"); } - + TEST_METHOD(Integer) { write(314); @@ -116,15 +110,6 @@ namespace JsonGeneratorTests assertResultIs("314"); } - template - void write(T value) - { - StringBuilder sb(buffer, sizeof(buffer)); - JsonValue jsonValue; - jsonValue.set(value); - returnValue = jsonValue.printTo(sb); - } - template void write(double value) { @@ -134,6 +119,15 @@ namespace JsonGeneratorTests returnValue = jsonValue.printTo(sb); } + template + void write(T value) + { + StringBuilder sb(buffer, sizeof(buffer)); + JsonValue jsonValue; + jsonValue.set(value); + returnValue = jsonValue.printTo(sb); + } + void assertResultIs(const char* expected) { Assert::AreEqual(expected, buffer);