Files
ArduinoJson/JsonGeneratorTests/JsonValue.h

68 lines
1.3 KiB
C
Raw Normal View History

2014-06-27 13:42:26 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "StringBuilder.h"
class JsonObjectBase;
class JsonValue
{
public:
JsonValue()
{
}
JsonValue(const char* value)
: implementation(&JsonValue::writeStringTo)
2014-06-27 13:42:26 +02:00
{
content.string = value;
}
JsonValue(double value)
: implementation(&JsonValue::writeNumberTo)
2014-06-27 13:42:26 +02:00
{
content.number = value;
}
JsonValue(bool value)
: implementation(&JsonValue::writeBooleanTo)
2014-06-27 13:42:26 +02:00
{
content.boolean = value;
}
JsonValue(JsonObjectBase& value)
: implementation(&JsonValue::writeObjectTo)
2014-06-27 13:42:26 +02:00
{
content.object = &value;
}
2014-07-01 13:36:22 +02:00
size_t writeTo(Print& p)
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
{
bool boolean;
double number;
2014-06-27 13:42:26 +02:00
JsonObjectBase* object;
const char* string;
2014-06-27 13:42:26 +02:00
};
Content content;
2014-07-01 13:36:22 +02:00
size_t(JsonValue::*implementation)(Print& p);
2014-07-01 13:36:22 +02:00
size_t writeBooleanTo(Print& p);
size_t writeNumberTo(Print& p);
size_t writeObjectTo(Print& p);
size_t writeStringTo(Print& p);
};