Files
ArduinoJson/JsonGenerator/JsonValue.h

94 lines
2.0 KiB
C
Raw Normal View History

2014-06-27 13:42:26 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
2014-07-01 13:44:36 +02:00
#include "Printable.h"
2014-06-27 13:42:26 +02:00
#include "StringBuilder.h"
2014-07-01 13:44:36 +02:00
class JsonValue : public Printable
2014-06-27 13:42:26 +02:00
{
public:
JsonValue()
{
}
JsonValue(bool value)
: implementation(&JsonValue::printBoolTo)
2014-06-27 13:42:26 +02:00
{
content.asBool = value;
2014-06-27 13:42:26 +02:00
}
JsonValue(double value, int digits=2)
2014-07-02 12:54:22 +02:00
: implementation(&JsonValue::printDoubleTo)
2014-06-27 13:42:26 +02:00
{
content.asDouble.value = value;
content.asDouble.digits = digits;
2014-06-27 13:42:26 +02:00
}
JsonValue(float value)
: implementation(&JsonValue::printFloatTo)
{
content.asFloat = value;
}
JsonValue(long value)
: implementation(&JsonValue::printLongTo)
2014-06-27 13:42:26 +02:00
{
content.asLong = value;
2014-06-27 13:42:26 +02:00
}
JsonValue(int value)
: implementation(&JsonValue::printLongTo)
{
content.asLong = value;
}
2014-07-01 13:44:36 +02:00
JsonValue(Printable& value)
2014-07-02 12:54:22 +02:00
: implementation(&JsonValue::printPrintableTo)
2014-06-27 13:42:26 +02:00
{
2014-07-02 12:51:27 +02:00
content.asPrintable = &value;
2014-06-27 13:42:26 +02:00
}
JsonValue(const char* value)
: implementation(&JsonValue::printStringTo)
{
content.asString = value;
}
virtual size_t printTo(Print& p) const
2014-06-27 13:42:26 +02:00
{
// handmade polymorphism
2014-07-01 13:36:22 +02:00
return (this->*implementation)(p);
}
private:
2014-06-27 13:42:26 +02:00
union Content
{
2014-07-02 12:51:27 +02:00
bool asBool;
float asFloat;
long asLong;
2014-07-02 12:51:27 +02:00
Printable* asPrintable;
const char* asString;
struct {
double value;
int digits;
} asDouble;
2014-06-27 13:42:26 +02:00
};
Content content;
2014-07-01 13:44:36 +02:00
size_t(JsonValue::*implementation)(Print& p)const;
2014-07-02 12:54:22 +02:00
size_t printBoolTo(Print& p) const;
size_t printDoubleTo(Print& p) const;
size_t printFloatTo(Print& p) const;
size_t printLongTo(Print& p) const;
2014-07-02 12:54:22 +02:00
size_t printPrintableTo(Print& p) const;
size_t printStringTo(Print& p) const;
};