diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 5c6fa935..5839d005 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -4,8 +4,6 @@ */ #include "JsonValue.h" -#include -#include size_t JsonValue::printBoolTo(Print& p) const { @@ -14,23 +12,17 @@ size_t JsonValue::printBoolTo(Print& p) const size_t JsonValue::printDoubleTo(Print& p) const { - char tmp[32]; - sprintf(tmp, "%.17lg", content.asDouble); - return p.write(tmp); + return p.print(content.asDouble); } size_t JsonValue::printFloatTo(Print& p) const { - char tmp[16]; - sprintf(tmp, "%.9g", content.asFloat); - return p.write(tmp); + return p.print(content.asFloat); } size_t JsonValue::printLongTo(Print& p) const { - char tmp[32]; - sprintf(tmp, "%ld", content.asLong); - return p.write(tmp); + return p.print(content.asLong); } size_t JsonValue::printPrintableTo(Print& p) const diff --git a/JsonGenerator/Print.h b/JsonGenerator/Print.h index 84368c0c..a277a51b 100644 --- a/JsonGenerator/Print.h +++ b/JsonGenerator/Print.h @@ -9,6 +9,7 @@ typedef unsigned char uint8_t; +// This class reproduces Arduino's Print class Print { public: @@ -16,6 +17,8 @@ public: virtual size_t write(uint8_t c) = 0; size_t write(const char* s); + size_t print(double, int = 2); + size_t print(long); }; #else diff --git a/JsonGeneratorTests/Print.cpp b/JsonGeneratorTests/Print.cpp index d1d40b60..ba70a0d4 100644 --- a/JsonGeneratorTests/Print.cpp +++ b/JsonGeneratorTests/Print.cpp @@ -3,7 +3,11 @@ * Benoit Blanchon 2014 - MIT License */ +#ifndef ARDUINO + #include "Print.h" +#include +//#include size_t Print::write(const char* s) { @@ -13,4 +17,20 @@ size_t Print::write(const char* s) n += write(*s++); } return n; -} \ No newline at end of file +} + +size_t Print::print(double value, int digits) +{ + char tmp[32]; + sprintf(tmp, "%.*lg", digits, value); + return write(tmp); +} + +size_t Print::print(long value) +{ + char tmp[32]; + sprintf(tmp, "%ld", value); + return write(tmp); +} + +#endif \ No newline at end of file