forked from bblanchon/ArduinoJson
Number of digits is now a template parameter
This commit is contained in:
@ -24,20 +24,17 @@ namespace ArduinoJson
|
||||
template<typename T>
|
||||
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<int DIGITS>
|
||||
void add(double value)
|
||||
{
|
||||
if (itemCount >= N) return;
|
||||
|
||||
items[itemCount] = value;
|
||||
itemCount++;
|
||||
items[itemCount++].set<DIGITS>(value);
|
||||
}
|
||||
|
||||
using JsonObjectBase::printTo;
|
||||
|
@ -24,20 +24,20 @@ namespace ArduinoJson
|
||||
template<typename T>
|
||||
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<int DIGITS>
|
||||
void add(const char* key, double value)
|
||||
{
|
||||
if (itemCount >= N) return;
|
||||
|
||||
items[itemCount].key = key;
|
||||
items[itemCount].value = value;
|
||||
items[itemCount].value.set<DIGITS>(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)
|
||||
{
|
||||
|
@ -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<int DIGITS>
|
||||
void set(double value)
|
||||
{
|
||||
printToImpl = &printDoubleTo;
|
||||
content.asDouble.value = value;
|
||||
content.asDouble.digits = DIGITS;
|
||||
}
|
||||
|
||||
size_t printTo(Print& p) const
|
||||
{
|
||||
// handmade polymorphism
|
||||
|
@ -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<JsonObjectBase&>(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@ -167,9 +167,10 @@ namespace JsonGeneratorTests
|
||||
arr.add(value);
|
||||
}
|
||||
|
||||
void addValue(double value, int digits)
|
||||
template<int DIGITS>
|
||||
void addValue(double value)
|
||||
{
|
||||
arr.add(value, digits);
|
||||
arr.add<DIGITS>(value);
|
||||
}
|
||||
|
||||
void jsonIs(const char* expected)
|
||||
|
@ -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<JsonObjectBase&>(key, value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@ -102,9 +102,10 @@ namespace JsonGeneratorTests
|
||||
hash.add(key, value);
|
||||
}
|
||||
|
||||
void addValue(const char* key, double value, int digits)
|
||||
template<int DIGITS>
|
||||
void addValue(const char* key, double value)
|
||||
{
|
||||
hash.add(key, value, digits);
|
||||
hash.add<DIGITS>(key, value);
|
||||
}
|
||||
|
||||
void jsonIs(const char* expected)
|
||||
|
@ -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<int DIGITS>
|
||||
void write(double value)
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
returnValue = JsonValue(value, digits).printTo(sb);
|
||||
JsonValue jsonValue;
|
||||
jsonValue.set<DIGITS>(value);
|
||||
returnValue = jsonValue.printTo(sb);
|
||||
}
|
||||
|
||||
void assertResultIs(const char* expected)
|
||||
|
Reference in New Issue
Block a user