|
|
@@ -8,87 +8,93 @@
|
|
|
#include "Printable.h"
|
|
|
#include "StringBuilder.h"
|
|
|
|
|
|
-class JsonValue : public Printable
|
|
|
-{
|
|
|
-public:
|
|
|
-
|
|
|
- JsonValue()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- JsonValue(bool value)
|
|
|
- : implementation(&JsonValue::printBoolTo)
|
|
|
+namespace ArduinoJson
|
|
|
+{
|
|
|
+ namespace Generator
|
|
|
{
|
|
|
- content.asBool = value;
|
|
|
- }
|
|
|
+ class JsonValue : public Printable
|
|
|
+ {
|
|
|
+ public:
|
|
|
|
|
|
- JsonValue(double value, int digits=2)
|
|
|
- : implementation(&JsonValue::printDoubleTo)
|
|
|
- {
|
|
|
- content.asDouble.value = value;
|
|
|
- content.asDouble.digits = digits;
|
|
|
- }
|
|
|
+ JsonValue()
|
|
|
+ {
|
|
|
+ }
|
|
|
|
|
|
- JsonValue(float value)
|
|
|
- : implementation(&JsonValue::printFloatTo)
|
|
|
- {
|
|
|
- content.asFloat = value;
|
|
|
- }
|
|
|
-
|
|
|
- JsonValue(long value)
|
|
|
- : implementation(&JsonValue::printLongTo)
|
|
|
- {
|
|
|
- content.asLong = value;
|
|
|
- }
|
|
|
+ JsonValue(bool value)
|
|
|
+ : implementation(&JsonValue::printBoolTo)
|
|
|
+ {
|
|
|
+ content.asBool = value;
|
|
|
+ }
|
|
|
|
|
|
- JsonValue(int value)
|
|
|
- : implementation(&JsonValue::printLongTo)
|
|
|
- {
|
|
|
- content.asLong = value;
|
|
|
- }
|
|
|
+ JsonValue(double value, int digits = 2)
|
|
|
+ : implementation(&JsonValue::printDoubleTo)
|
|
|
+ {
|
|
|
+ content.asDouble.value = value;
|
|
|
+ content.asDouble.digits = digits;
|
|
|
+ }
|
|
|
|
|
|
- JsonValue(Printable& value)
|
|
|
- : implementation(&JsonValue::printPrintableTo)
|
|
|
- {
|
|
|
- content.asPrintable = &value;
|
|
|
- }
|
|
|
+ JsonValue(float value)
|
|
|
+ : implementation(&JsonValue::printFloatTo)
|
|
|
+ {
|
|
|
+ content.asFloat = value;
|
|
|
+ }
|
|
|
|
|
|
- JsonValue(const char* value)
|
|
|
- : implementation(&JsonValue::printStringTo)
|
|
|
- {
|
|
|
- content.asString = value;
|
|
|
- }
|
|
|
+ JsonValue(long value)
|
|
|
+ : implementation(&JsonValue::printLongTo)
|
|
|
+ {
|
|
|
+ content.asLong = value;
|
|
|
+ }
|
|
|
|
|
|
- virtual size_t printTo(Print& p) const
|
|
|
- {
|
|
|
- // handmade polymorphism
|
|
|
- return (this->*implementation)(p);
|
|
|
+ JsonValue(int value)
|
|
|
+ : implementation(&JsonValue::printLongTo)
|
|
|
+ {
|
|
|
+ content.asLong = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ JsonValue(Printable& value)
|
|
|
+ : implementation(&JsonValue::printPrintableTo)
|
|
|
+ {
|
|
|
+ content.asPrintable = &value;
|
|
|
+ }
|
|
|
+
|
|
|
+ JsonValue(const char* value)
|
|
|
+ : implementation(&JsonValue::printStringTo)
|
|
|
+ {
|
|
|
+ content.asString = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ virtual size_t printTo(Print& p) const
|
|
|
+ {
|
|
|
+ // handmade polymorphism
|
|
|
+ return (this->*implementation)(p);
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+
|
|
|
+ union Content
|
|
|
+ {
|
|
|
+ bool asBool;
|
|
|
+ float asFloat;
|
|
|
+ long asLong;
|
|
|
+ Printable* asPrintable;
|
|
|
+ const char* asString;
|
|
|
+
|
|
|
+ struct {
|
|
|
+ double value;
|
|
|
+ int digits;
|
|
|
+ } asDouble;
|
|
|
+ };
|
|
|
+
|
|
|
+ Content content;
|
|
|
+
|
|
|
+ size_t(JsonValue::*implementation)(Print& p)const;
|
|
|
+
|
|
|
+ size_t printBoolTo(Print& p) const;
|
|
|
+ size_t printDoubleTo(Print& p) const;
|
|
|
+ size_t printFloatTo(Print& p) const;
|
|
|
+ size_t printLongTo(Print& p) const;
|
|
|
+ size_t printPrintableTo(Print& p) const;
|
|
|
+ size_t printStringTo(Print& p) const;
|
|
|
+ };
|
|
|
}
|
|
|
-
|
|
|
-private:
|
|
|
-
|
|
|
- union Content
|
|
|
- {
|
|
|
- bool asBool;
|
|
|
- float asFloat;
|
|
|
- long asLong;
|
|
|
- Printable* asPrintable;
|
|
|
- const char* asString;
|
|
|
-
|
|
|
- struct {
|
|
|
- double value;
|
|
|
- int digits;
|
|
|
- } asDouble;
|
|
|
- };
|
|
|
-
|
|
|
- Content content;
|
|
|
-
|
|
|
- size_t(JsonValue::*implementation)(Print& p)const;
|
|
|
-
|
|
|
- size_t printBoolTo(Print& p) const;
|
|
|
- size_t printDoubleTo(Print& p) const;
|
|
|
- size_t printFloatTo(Print& p) const;
|
|
|
- size_t printLongTo(Print& p) const;
|
|
|
- size_t printPrintableTo(Print& p) const;
|
|
|
- size_t printStringTo(Print& p) const;
|
|
|
-};
|
|
|
+}
|