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>
|
template<typename T>
|
||||||
void add(T value)
|
void add(T value)
|
||||||
{
|
{
|
||||||
add(JsonValue(value));
|
if (itemCount >= N) return;
|
||||||
|
|
||||||
|
items[itemCount++].set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(double value, int digits = 2)
|
template<int DIGITS>
|
||||||
{
|
void add(double value)
|
||||||
add(JsonValue(value, digits));
|
|
||||||
}
|
|
||||||
|
|
||||||
void add(JsonValue value)
|
|
||||||
{
|
{
|
||||||
if (itemCount >= N) return;
|
if (itemCount >= N) return;
|
||||||
|
|
||||||
items[itemCount] = value;
|
items[itemCount++].set<DIGITS>(value);
|
||||||
itemCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
using JsonObjectBase::printTo;
|
using JsonObjectBase::printTo;
|
||||||
|
@ -24,20 +24,20 @@ namespace ArduinoJson
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void add(const char* key, T value)
|
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)
|
template<int DIGITS>
|
||||||
{
|
void add(const char* key, double value)
|
||||||
add(key, JsonValue(value, digits));
|
|
||||||
}
|
|
||||||
|
|
||||||
void add(const char* key, JsonValue value)
|
|
||||||
{
|
{
|
||||||
if (itemCount >= N) return;
|
if (itemCount >= N) return;
|
||||||
|
|
||||||
items[itemCount].key = key;
|
items[itemCount].key = key;
|
||||||
items[itemCount].value = value;
|
items[itemCount].value.set<DIGITS>(value);
|
||||||
itemCount++;
|
itemCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,8 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
for (int i = 0; i < itemCount; i++)
|
for (int i = 0; i < itemCount; i++)
|
||||||
{
|
{
|
||||||
JsonValue key(items[i].key);
|
JsonValue key;
|
||||||
|
key.set(items[i].key);
|
||||||
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
|
@ -16,47 +16,44 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
JsonValue()
|
void set(bool value)
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue(bool value)
|
|
||||||
: printToImpl(&printBoolTo)
|
|
||||||
{
|
{
|
||||||
|
printToImpl = &printBoolTo;
|
||||||
content.asBool = value;
|
content.asBool = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(double value, uint8_t digits = 2)
|
void set(long value)
|
||||||
: printToImpl(&printDoubleTo)
|
|
||||||
{
|
|
||||||
content.asDouble.value = value;
|
|
||||||
content.asDouble.digits = digits;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue(long value)
|
|
||||||
: printToImpl(&printLongTo)
|
|
||||||
{
|
{
|
||||||
|
printToImpl = &printLongTo;
|
||||||
content.asLong = value;
|
content.asLong = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(int value)
|
void set(int value)
|
||||||
: printToImpl(&printLongTo)
|
|
||||||
{
|
{
|
||||||
|
printToImpl = &printLongTo;
|
||||||
content.asLong = value;
|
content.asLong = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(Printable& value)
|
void set(Printable& value)
|
||||||
: printToImpl(&printPrintableTo)
|
|
||||||
{
|
{
|
||||||
|
printToImpl = &printPrintableTo;
|
||||||
content.asPrintable = &value;
|
content.asPrintable = &value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(const char* value)
|
void set(const char* value)
|
||||||
: printToImpl(&printStringTo)
|
|
||||||
{
|
{
|
||||||
|
printToImpl = &printStringTo;
|
||||||
content.asString = value;
|
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
|
size_t printTo(Print& p) const
|
||||||
{
|
{
|
||||||
// handmade polymorphism
|
// handmade polymorphism
|
||||||
|
@ -57,7 +57,7 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
TEST_METHOD(OneDouble)
|
TEST_METHOD(OneDouble)
|
||||||
{
|
{
|
||||||
addValue(3.14159265358979323846, 4);
|
addValue<4>(3.14159265358979323846);
|
||||||
jsonIs("[3.1416]");
|
jsonIs("[3.1416]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
void addNested(JsonObjectBase& value)
|
void addNested(JsonObjectBase& value)
|
||||||
{
|
{
|
||||||
arr.add(value);
|
arr.add<JsonObjectBase&>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -167,9 +167,10 @@ namespace JsonGeneratorTests
|
|||||||
arr.add(value);
|
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)
|
void jsonIs(const char* expected)
|
||||||
|
@ -51,7 +51,7 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
TEST_METHOD(OneDouble)
|
TEST_METHOD(OneDouble)
|
||||||
{
|
{
|
||||||
addValue("key", 3.14159265358979323846, 4);
|
addValue<4>("key", 3.14159265358979323846);
|
||||||
jsonIs("{\"key\":3.1416}");
|
jsonIs("{\"key\":3.1416}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
void addNested(const char* key, JsonObjectBase& value)
|
void addNested(const char* key, JsonObjectBase& value)
|
||||||
{
|
{
|
||||||
hash.add(key, value);
|
hash.add<JsonObjectBase&>(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -102,9 +102,10 @@ namespace JsonGeneratorTests
|
|||||||
hash.add(key, value);
|
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)
|
void jsonIs(const char* expected)
|
||||||
|
@ -73,28 +73,28 @@ namespace JsonGeneratorTests
|
|||||||
write("\t");
|
write("\t");
|
||||||
assertResultIs("\"\\t\"");
|
assertResultIs("\"\\t\"");
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
TEST_METHOD(DoubleDefaultDigits)
|
TEST_METHOD(DoubleDefaultDigits)
|
||||||
{
|
{
|
||||||
write(3.14159265358979323846);
|
write(3.14159265358979323846);
|
||||||
assertResultIs("3.14");
|
assertResultIs("3.14");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
TEST_METHOD(DoubleZeroDigits)
|
TEST_METHOD(DoubleZeroDigits)
|
||||||
{
|
{
|
||||||
write(3.14159265358979323846, 0);
|
write<0>(3.14159265358979323846);
|
||||||
assertResultIs("3");
|
assertResultIs("3");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(DoubleOneDigit)
|
TEST_METHOD(DoubleOneDigit)
|
||||||
{
|
{
|
||||||
write(3.14159265358979323846, 1);
|
write<1>(3.14159265358979323846);
|
||||||
assertResultIs("3.1");
|
assertResultIs("3.1");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(DoubleTwoDigits)
|
TEST_METHOD(DoubleTwoDigits)
|
||||||
{
|
{
|
||||||
write(3.14159265358979323846, 2);
|
write<2>(3.14159265358979323846);
|
||||||
assertResultIs("3.14");
|
assertResultIs("3.14");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,13 +120,18 @@ namespace JsonGeneratorTests
|
|||||||
void write(T value)
|
void write(T value)
|
||||||
{
|
{
|
||||||
StringBuilder sb(buffer, sizeof(buffer));
|
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));
|
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)
|
void assertResultIs(const char* expected)
|
||||||
|
Reference in New Issue
Block a user