forked from bblanchon/ArduinoJson
Got rid of the switch/case in JsonValue
This commit is contained in:
@ -6,27 +6,25 @@
|
|||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
#include "JsonObjectBase.h"
|
#include "JsonObjectBase.h"
|
||||||
|
|
||||||
void JsonValue::writeTo(StringBuilder& sb)
|
void JsonValue::writeBooleanTo(StringBuilder& sb)
|
||||||
{
|
{
|
||||||
switch (type)
|
sb.append(content.boolean ? "true" : "false");
|
||||||
{
|
}
|
||||||
case JSON_STRING:
|
|
||||||
sb.appendEscaped(content.string);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_NUMBER:
|
void JsonValue::writeNumberTo(StringBuilder& sb)
|
||||||
sb.append(content.number);
|
{
|
||||||
break;
|
sb.append(content.number);
|
||||||
|
}
|
||||||
|
|
||||||
case JSON_BOOLEAN:
|
void JsonValue::writeObjectTo(StringBuilder& sb)
|
||||||
sb.append(content.boolean ? "true" : "false");
|
{
|
||||||
break;
|
if (content.object)
|
||||||
|
((JsonObjectBase*) content.object)->writeTo(sb);
|
||||||
|
else
|
||||||
|
sb.append("null");
|
||||||
|
}
|
||||||
|
|
||||||
case JSON_OBJECT:
|
void JsonValue::writeStringTo(StringBuilder& sb)
|
||||||
if (content.object)
|
{
|
||||||
((JsonObjectBase*)content.object)->writeTo(sb);
|
sb.appendEscaped(content.string);
|
||||||
else
|
|
||||||
sb.append("null");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -18,50 +18,51 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(const char* value)
|
JsonValue(const char* value)
|
||||||
: type(JSON_STRING)
|
: implementation(&JsonValue::writeStringTo)
|
||||||
{
|
{
|
||||||
content.string = value;
|
content.string = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(double value)
|
JsonValue(double value)
|
||||||
: type(JSON_NUMBER)
|
: implementation(&JsonValue::writeNumberTo)
|
||||||
{
|
{
|
||||||
content.number = value;
|
content.number = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(bool value)
|
JsonValue(bool value)
|
||||||
: type(JSON_BOOLEAN)
|
: implementation(&JsonValue::writeBooleanTo)
|
||||||
{
|
{
|
||||||
content.boolean = value;
|
content.boolean = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(JsonObjectBase& value)
|
JsonValue(JsonObjectBase& value)
|
||||||
: type(JSON_OBJECT)
|
: implementation(&JsonValue::writeObjectTo)
|
||||||
{
|
{
|
||||||
content.object = &value;
|
content.object = &value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeTo(StringBuilder& sb);
|
void writeTo(StringBuilder& sb)
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
enum Type
|
|
||||||
{
|
{
|
||||||
JSON_STRING,
|
// handmade polymorphism
|
||||||
JSON_NUMBER,
|
(this->*implementation)(sb);
|
||||||
JSON_BOOLEAN,
|
}
|
||||||
JSON_OBJECT,
|
|
||||||
};
|
private:
|
||||||
|
|
||||||
union Content
|
union Content
|
||||||
{
|
{
|
||||||
const char* string;
|
|
||||||
double number;
|
|
||||||
bool boolean;
|
bool boolean;
|
||||||
|
double number;
|
||||||
JsonObjectBase* object;
|
JsonObjectBase* object;
|
||||||
|
const char* string;
|
||||||
};
|
};
|
||||||
|
|
||||||
Type type;
|
|
||||||
Content content;
|
Content content;
|
||||||
};
|
|
||||||
|
|
||||||
|
void (JsonValue::*implementation)(StringBuilder& sb);
|
||||||
|
|
||||||
|
void writeBooleanTo(StringBuilder& sb);
|
||||||
|
void writeNumberTo(StringBuilder& sb);
|
||||||
|
void writeObjectTo(StringBuilder& sb);
|
||||||
|
void writeStringTo(StringBuilder& sb);
|
||||||
|
};
|
Reference in New Issue
Block a user