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;
}
void writeTo(StringBuilder& sb)
2014-06-27 13:42:26 +02:00
{
// handmade polymorphism
(this->*implementation)(sb);
}
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;
void (JsonValue::*implementation)(StringBuilder& sb);
void writeBooleanTo(StringBuilder& sb);
void writeNumberTo(StringBuilder& sb);
void writeObjectTo(StringBuilder& sb);
void writeStringTo(StringBuilder& sb);
};