Files
ArduinoJson/srcs/Internals/JsonNode.cpp

166 lines
3.5 KiB
C++
Raw Normal View History

2014-10-16 00:11:23 +02:00
#include "ArduinoJson/Internals/JsonNode.h"
2014-10-05 16:25:49 +02:00
2014-10-16 00:11:23 +02:00
#include "ArduinoJson/Internals/JsonWriter.h"
#include "ArduinoJson/JsonArray.h"
#include "ArduinoJson/JsonObject.h"
#include "ArduinoJson/JsonBuffer.h"
2014-10-05 16:25:49 +02:00
void JsonNode::writeTo(JsonWriter& writer)
{
switch (type)
{
case JSON_PROXY:
content.asProxy.target->writeTo(writer);
break;
2014-10-05 16:25:49 +02:00
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;
2014-10-09 12:14:10 +02:00
case JSON_LONG:
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;
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;
}
2014-10-09 14:17:09 +02:00
}
2014-10-09 14:48:55 +02:00
void JsonNode::addChild(JsonNode* childToAdd)
2014-10-09 14:17:09 +02:00
{
if (type == JSON_PROXY)
return content.asProxy.target->addChild(childToAdd);
if (type != JSON_ARRAY && type != JSON_OBJECT)
return;
2014-10-09 14:17:09 +02:00
JsonNode* lastChild = content.asContainer.child;
if (!lastChild)
{
content.asContainer.child = childToAdd;
return;
}
while (lastChild->next)
lastChild = lastChild->next;
lastChild->next = childToAdd;
}
2014-10-09 14:48:55 +02:00
void JsonNode::removeChild(JsonNode* childToRemove)
2014-10-09 14:17:09 +02:00
{
if (type == JSON_PROXY)
return content.asProxy.target->removeChild(childToRemove);
2014-10-09 14:17:09 +02:00
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
if (content.asContainer.child == childToRemove)
{
content.asContainer.child = childToRemove->next;
return;
}
for (JsonNode* child = content.asContainer.child; child; child = child->next)
{
if (child->next == childToRemove)
child->next = childToRemove->next;
}
2014-10-05 16:25:49 +02:00
}
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-11 16:59:16 +02:00
for (;;)
2014-10-07 11:22:10 +02:00
{
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-11 16:59:16 +02:00
for (;;)
2014-10-07 11:22:10 +02:00
{
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();
}
}
void JsonNode::setAsProxyOfSelf()
{
JsonBuffer* buffer = content.asContainer.buffer;
if (!buffer) return;
JsonNode* newNode = buffer->createNode();
if (!newNode) return;
*newNode = *this;
setAsProxyOf(newNode);
2014-10-14 17:28:57 +02:00
}
void JsonNode::duplicate(JsonNode* other)
{
if (!other)
{
type = JSON_UNDEFINED;
}
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
{
other->setAsProxyOfSelf();
setAsProxyOf(other->content.asProxy.target);
}
else
{
*this = *other;
}
2014-10-05 16:25:49 +02:00
}