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(const char* value)
|
2014-07-01 14:08:15 +02:00
|
|
|
: implementation(&JsonValue::printStringTo)
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
|
|
|
content.string = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue(double value)
|
2014-07-01 14:08:15 +02:00
|
|
|
: implementation(&JsonValue::printNumberTo)
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
|
|
|
content.number = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue(bool value)
|
2014-07-01 14:08:15 +02:00
|
|
|
: implementation(&JsonValue::printBooleanTo)
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
|
|
|
content.boolean = value;
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:44:36 +02:00
|
|
|
JsonValue(Printable& value)
|
2014-07-01 14:08:15 +02:00
|
|
|
: implementation(&JsonValue::printObjectTo)
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
|
|
|
content.object = &value;
|
|
|
|
}
|
|
|
|
|
2014-07-01 14:08:15 +02:00
|
|
|
virtual size_t printTo(Print& p) const
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
2014-06-30 18:37:54 +02:00
|
|
|
// handmade polymorphism
|
2014-07-01 13:36:22 +02:00
|
|
|
return (this->*implementation)(p);
|
2014-06-30 18:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-06-27 13:42:26 +02:00
|
|
|
union Content
|
|
|
|
{
|
2014-07-01 13:44:36 +02:00
|
|
|
bool boolean;
|
|
|
|
double number;
|
|
|
|
Printable* object;
|
|
|
|
const char* string;
|
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-06-30 18:37:54 +02:00
|
|
|
|
2014-07-01 14:08:15 +02:00
|
|
|
size_t printBooleanTo(Print& p) const;
|
|
|
|
size_t printNumberTo(Print& p) const;
|
|
|
|
size_t printObjectTo(Print& p) const;
|
|
|
|
size_t printStringTo(Print& p) const;
|
2014-06-30 18:37:54 +02:00
|
|
|
};
|