forked from bblanchon/ArduinoJson
Added a JsonValue constructor for float
This commit is contained in:
@ -15,22 +15,24 @@ size_t JsonValue::printBoolTo(Print& p) const
|
|||||||
size_t JsonValue::printDoubleTo(Print& p) const
|
size_t JsonValue::printDoubleTo(Print& p) const
|
||||||
{
|
{
|
||||||
char tmp[32];
|
char tmp[32];
|
||||||
|
sprintf(tmp, "%.17lg", content.asDouble);
|
||||||
|
return p.write(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
sprintf(tmp, "%lg", content.asDouble);
|
size_t JsonValue::printFloatTo(Print& p) const
|
||||||
|
{
|
||||||
|
char tmp[16];
|
||||||
|
sprintf(tmp, "%.9g", content.asFloat);
|
||||||
return p.write(tmp);
|
return p.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonValue::printLongTo(Print& p) const
|
size_t JsonValue::printLongTo(Print& p) const
|
||||||
{
|
{
|
||||||
char tmp[32];
|
char tmp[32];
|
||||||
|
|
||||||
sprintf(tmp, "%ld", content.asLong);
|
sprintf(tmp, "%ld", content.asLong);
|
||||||
|
|
||||||
return p.write(tmp);
|
return p.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t JsonValue::printPrintableTo(Print& p) const
|
size_t JsonValue::printPrintableTo(Print& p) const
|
||||||
{
|
{
|
||||||
if (content.asPrintable)
|
if (content.asPrintable)
|
||||||
|
@ -28,6 +28,12 @@ public:
|
|||||||
content.asDouble = value;
|
content.asDouble = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue(float value)
|
||||||
|
: implementation(&JsonValue::printFloatTo)
|
||||||
|
{
|
||||||
|
content.asFloat = value;
|
||||||
|
}
|
||||||
|
|
||||||
JsonValue(long value)
|
JsonValue(long value)
|
||||||
: implementation(&JsonValue::printLongTo)
|
: implementation(&JsonValue::printLongTo)
|
||||||
{
|
{
|
||||||
@ -62,6 +68,7 @@ private:
|
|||||||
{
|
{
|
||||||
bool asBool;
|
bool asBool;
|
||||||
double asDouble;
|
double asDouble;
|
||||||
|
float asFloat;
|
||||||
long asLong;
|
long asLong;
|
||||||
Printable* asPrintable;
|
Printable* asPrintable;
|
||||||
const char* asString;
|
const char* asString;
|
||||||
@ -73,6 +80,7 @@ private:
|
|||||||
|
|
||||||
size_t printBoolTo(Print& p) const;
|
size_t printBoolTo(Print& p) const;
|
||||||
size_t printDoubleTo(Print& p) const;
|
size_t printDoubleTo(Print& p) const;
|
||||||
|
size_t printFloatTo(Print& p) const;
|
||||||
size_t printLongTo(Print& p) const;
|
size_t printLongTo(Print& p) const;
|
||||||
size_t printPrintableTo(Print& p) const;
|
size_t printPrintableTo(Print& p) const;
|
||||||
size_t printStringTo(Print& p) const;
|
size_t printStringTo(Print& p) const;
|
||||||
|
@ -85,11 +85,18 @@ namespace JsonGeneratorTests
|
|||||||
assertResultIs("\"\\t\"");
|
assertResultIs("\"\\t\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(Float)
|
||||||
|
{
|
||||||
|
write(3.40282346e38F);
|
||||||
|
// assertReturns(4);
|
||||||
|
assertResultIs("3.40282347e+038");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_METHOD(Double)
|
TEST_METHOD(Double)
|
||||||
{
|
{
|
||||||
write(3.14);
|
write(1.7976931348623157e308);
|
||||||
assertReturns(4);
|
// assertReturns(4);
|
||||||
assertResultIs("3.14");
|
assertResultIs("1.7976931348623157e+308");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(Integer)
|
TEST_METHOD(Integer)
|
||||||
|
Reference in New Issue
Block a user