Files
ArduinoJson/JsonGenerator/JsonValue.h

88 lines
2.2 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-03 13:54:27 +02:00
namespace ArduinoJson
{
namespace Generator
2014-06-27 13:42:26 +02:00
{
class JsonValue
2014-07-03 13:54:27 +02:00
{
public:
void set(bool value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printBoolTo;
2014-07-03 13:54:27 +02:00
content.asBool = value;
}
2014-06-27 13:42:26 +02:00
void set(long value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printLongTo;
2014-07-03 13:54:27 +02:00
content.asLong = value;
}
void set(int value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printLongTo;
2014-07-03 13:54:27 +02:00
content.asLong = value;
}
void set(Printable& value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printPrintableTo;
2014-07-03 13:54:27 +02:00
content.asPrintable = &value;
}
void set(const char* value)
2014-07-03 13:54:27 +02:00
{
printToImpl = &printStringTo;
2014-07-03 13:54:27 +02:00
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
2014-07-03 13:54:27 +02:00
{
// handmade polymorphism
return printToImpl(content, p);
2014-07-03 13:54:27 +02:00
}
private:
union Content
{
bool asBool;
long asLong;
Printable* asPrintable;
const char* asString;
struct {
double value;
uint8_t digits;
2014-07-03 13:54:27 +02:00
} asDouble;
};
Content content;
size_t(*printToImpl)(const Content&, Print& p);
2014-07-03 13:54:27 +02:00
static size_t printBoolTo(const Content&, Print& p);
static size_t printDoubleTo(const Content&, Print& p);
static size_t printLongTo(const Content&, Print& p);
static size_t printPrintableTo(const Content&, Print& p);
static size_t printStringTo(const Content&, Print& p);
2014-07-03 13:54:27 +02:00
};
}
2014-07-03 13:54:27 +02:00
}