Files
ArduinoJson/srcs/Internals/JsonNode.cpp

93 lines
1.8 KiB
C++
Raw Normal View History

2014-10-05 16:25:49 +02:00
#include "JsonNode.h"
#include "JsonWriter.h"
#include "../JsonArray.h"
#include "../JsonObject.h"
void JsonNode::writeTo(JsonWriter& writer)
{
switch (type)
{
case JSON_ARRAY:
writeArrayTo(writer);
break;
case JSON_OBJECT:
writeObjectTo(writer);
break;
case JSON_STRING:
2014-10-07 11:22:10 +02:00
writer.writeString(content.asString);
2014-10-05 16:25:49 +02:00
break;
case JSON_INTEGER:
2014-10-07 11:22:10 +02:00
writer.writeInteger(content.asInteger);
2014-10-05 16:25:49 +02:00
break;
case JSON_BOOLEAN:
2014-10-07 11:22:10 +02:00
writer.writeBoolean(content.asBoolean);
2014-10-05 16:25:49 +02:00
break;
case JSON_PROXY:
content.asProxy.target->writeTo(writer);
break;
default: // >= JSON_DOUBLE_0_DECIMALS
2014-10-07 11:22:10 +02:00
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
2014-10-05 16:25:49 +02:00
break;
}
}
void JsonNode::writeArrayTo(JsonWriter& writer)
{
JsonNode* child = content.asContainer.child;
2014-10-07 11:22:10 +02:00
if (child)
2014-10-05 16:25:49 +02:00
{
2014-10-07 11:22:10 +02:00
writer.beginArray();
2014-10-05 16:25:49 +02:00
2014-10-07 11:22:10 +02:00
while (true)
{
child->writeTo(writer);
child = child->next;
if (!child) break;
writer.writeComma();
}
2014-10-05 16:25:49 +02:00
2014-10-07 11:22:10 +02:00
writer.endArray();
}
else
{
writer.writeEmptyArray();
}
2014-10-05 16:25:49 +02:00
}
void JsonNode::writeObjectTo(JsonWriter& writer)
{
JsonNode* child = content.asContainer.child;
2014-10-07 11:22:10 +02:00
if (child)
2014-10-05 16:25:49 +02:00
{
2014-10-07 11:22:10 +02:00
writer.beginObject();
2014-10-05 16:25:49 +02:00
2014-10-07 11:22:10 +02:00
while (true)
{
writer.writeString(child->content.asKey.key);
writer.writeColon();
child->content.asKey.value->writeTo(writer);
child = child->next;
if (!child) break;
writer.writeComma();
}
2014-10-05 16:25:49 +02:00
2014-10-07 11:22:10 +02:00
writer.endObject();
}
else
{
writer.writeEmptyObject();
}
2014-10-05 16:25:49 +02:00
}