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