2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/Internals/JsonNode.hpp"
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
|
|
|
#include "ArduinoJson/JsonArray.hpp"
|
|
|
|
#include "ArduinoJson/JsonObject.hpp"
|
|
|
|
#include "ArduinoJson/JsonBuffer.hpp"
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson::Internals;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::writeTo(JsonWriter &writer) {
|
|
|
|
switch (type) {
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_PROXY:
|
|
|
|
content.asProxy.target->writeTo(writer);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_ARRAY:
|
|
|
|
writeArrayTo(writer);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_OBJECT:
|
|
|
|
writeObjectTo(writer);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_STRING:
|
|
|
|
writer.writeString(content.asString);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_LONG:
|
|
|
|
writer.writeInteger(content.asInteger);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
case JSON_BOOLEAN:
|
|
|
|
writer.writeBoolean(content.asBoolean);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
default: // >= JSON_DOUBLE_0_DECIMALS
|
|
|
|
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
|
|
|
break;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::addChild(JsonNode *childToAdd) {
|
2014-10-23 23:13:13 +02:00
|
|
|
if (type == JSON_PROXY) return content.asProxy.target->addChild(childToAdd);
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonNode *lastChild = content.asContainer.child;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
if (!lastChild) {
|
|
|
|
content.asContainer.child = childToAdd;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
while (lastChild->next) lastChild = lastChild->next;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
lastChild->next = childToAdd;
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::removeChild(JsonNode *childToRemove) {
|
|
|
|
if (type == JSON_PROXY)
|
|
|
|
return content.asProxy.target->removeChild(childToRemove);
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
if (content.asContainer.child == childToRemove) {
|
|
|
|
content.asContainer.child = childToRemove->next;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (JsonNode *child = content.asContainer.child; child;
|
|
|
|
child = child->next) {
|
2014-10-23 23:13:13 +02:00
|
|
|
if (child->next == childToRemove) child->next = childToRemove->next;
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::writeArrayTo(JsonWriter &writer) {
|
|
|
|
JsonNode *child = content.asContainer.child;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
if (child) {
|
|
|
|
writer.beginArray();
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (;;) {
|
|
|
|
child->writeTo(writer);
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
child = child->next;
|
2014-10-23 23:13:13 +02:00
|
|
|
if (!child) break;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
writer.writeComma();
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
writer.endArray();
|
|
|
|
} else {
|
|
|
|
writer.writeEmptyArray();
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::writeObjectTo(JsonWriter &writer) {
|
|
|
|
JsonNode *child = content.asContainer.child;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
if (child) {
|
|
|
|
writer.beginObject();
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (;;) {
|
|
|
|
writer.writeString(child->content.asKeyValue.key);
|
|
|
|
writer.writeColon();
|
|
|
|
child->content.asKeyValue.value->writeTo(writer);
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
child = child->next;
|
2014-10-23 23:13:13 +02:00
|
|
|
if (!child) break;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
writer.writeComma();
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
2014-10-23 19:54:00 +02:00
|
|
|
|
|
|
|
writer.endObject();
|
|
|
|
} else {
|
|
|
|
writer.writeEmptyObject();
|
|
|
|
}
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void JsonNode::setAsProxyOfSelf() {
|
|
|
|
JsonBuffer *buffer = content.asContainer.buffer;
|
2014-10-23 23:13:13 +02:00
|
|
|
if (!buffer) return;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonNode *newNode = buffer->createNode();
|
2014-10-23 23:13:13 +02:00
|
|
|
if (!newNode) return;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
*newNode = *this;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
setAsProxyOf(newNode);
|
2014-10-21 21:11:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +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;
|
|
|
|
}
|
|
|
|
}
|