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
|
|
|
{
|
2014-07-06 21:08:04 +02:00
|
|
|
class JsonValue
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-07-07 13:24:14 +02:00
|
|
|
|
|
|
|
void set(bool value)
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
2014-07-07 13:24:14 +02:00
|
|
|
printToImpl = &printBoolTo;
|
2014-07-03 13:54:27 +02:00
|
|
|
content.asBool = value;
|
|
|
|
}
|
2014-06-27 13:42:26 +02:00
|
|
|
|
2014-07-07 13:24:14 +02:00
|
|
|
void set(long value)
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
2014-07-07 13:24:14 +02:00
|
|
|
printToImpl = &printLongTo;
|
2014-07-03 13:54:27 +02:00
|
|
|
content.asLong = value;
|
|
|
|
}
|
2014-07-02 13:02:28 +02:00
|
|
|
|
2014-07-07 13:24:14 +02:00
|
|
|
void set(int value)
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
2014-07-07 13:24:14 +02:00
|
|
|
printToImpl = &printLongTo;
|
2014-07-03 13:54:27 +02:00
|
|
|
content.asLong = value;
|
|
|
|
}
|
|
|
|
|
2014-07-07 13:24:14 +02:00
|
|
|
void set(Printable& value)
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
2014-07-07 13:24:14 +02:00
|
|
|
printToImpl = &printPrintableTo;
|
2014-07-03 13:54:27 +02:00
|
|
|
content.asPrintable = &value;
|
|
|
|
}
|
|
|
|
|
2014-07-07 13:24:14 +02:00
|
|
|
void set(const char* value)
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
2014-07-07 13:24:14 +02:00
|
|
|
printToImpl = &printStringTo;
|
2014-07-03 13:54:27 +02:00
|
|
|
content.asString = value;
|
|
|
|
}
|
|
|
|
|
2014-07-07 13:24:14 +02:00
|
|
|
template<int DIGITS>
|
|
|
|
void set(double value)
|
|
|
|
{
|
|
|
|
printToImpl = &printDoubleTo;
|
|
|
|
content.asDouble.value = value;
|
|
|
|
content.asDouble.digits = DIGITS;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:28:39 +02:00
|
|
|
size_t printTo(Print& p) const
|
2014-07-03 13:54:27 +02:00
|
|
|
{
|
|
|
|
// handmade polymorphism
|
2014-07-06 21:34:46 +02:00
|
|
|
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;
|
2014-07-06 21:28:39 +02:00
|
|
|
uint8_t digits;
|
2014-07-03 13:54:27 +02:00
|
|
|
} asDouble;
|
|
|
|
};
|
|
|
|
|
|
|
|
Content content;
|
|
|
|
|
2014-07-06 21:34:46 +02:00
|
|
|
size_t(*printToImpl)(const Content&, Print& p);
|
2014-07-03 13:54:27 +02:00
|
|
|
|
2014-07-06 21:34:46 +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-06-30 18:37:54 +02:00
|
|
|
}
|
2014-07-03 13:54:27 +02:00
|
|
|
}
|