mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-22 23:07:29 +02:00
Added JSON_PROXY to copy arrays and objects by reference
This commit is contained in:
@ -3,11 +3,16 @@
|
||||
#include "JsonWriter.h"
|
||||
#include "../JsonArray.h"
|
||||
#include "../JsonObject.h"
|
||||
#include "../JsonBuffer.h"
|
||||
|
||||
void JsonNode::writeTo(JsonWriter& writer)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON_PROXY:
|
||||
content.asProxy.target->writeTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_ARRAY:
|
||||
writeArrayTo(writer);
|
||||
break;
|
||||
@ -36,7 +41,11 @@ void JsonNode::writeTo(JsonWriter& writer)
|
||||
|
||||
void JsonNode::addChild(JsonNode* childToAdd)
|
||||
{
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->addChild(childToAdd);
|
||||
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
||||
return;
|
||||
|
||||
JsonNode* lastChild = content.asContainer.child;
|
||||
|
||||
@ -54,6 +63,9 @@ void JsonNode::addChild(JsonNode* childToAdd)
|
||||
|
||||
void JsonNode::removeChild(JsonNode* childToRemove)
|
||||
{
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->removeChild(childToRemove);
|
||||
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||
|
||||
if (content.asContainer.child == childToRemove)
|
||||
@ -121,4 +133,17 @@ void JsonNode::writeObjectTo(JsonWriter& writer)
|
||||
{
|
||||
writer.writeEmptyObject();
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::setAsProxyOfSelf()
|
||||
{
|
||||
JsonBuffer* buffer = content.asContainer.buffer;
|
||||
if (!buffer) return;
|
||||
|
||||
JsonNode* newNode = buffer->createNode();
|
||||
if (!newNode) return;
|
||||
|
||||
*newNode = *this;
|
||||
|
||||
setAsProxyOf(newNode);
|
||||
}
|
@ -18,6 +18,7 @@ class JsonNode
|
||||
JSON_BOOLEAN,
|
||||
JSON_STRING,
|
||||
JSON_LONG,
|
||||
JSON_PROXY,
|
||||
JSON_DOUBLE_0_DECIMALS,
|
||||
JSON_DOUBLE_1_DECIMAL,
|
||||
JSON_DOUBLE_2_DECIMALS,
|
||||
@ -126,11 +127,13 @@ public:
|
||||
|
||||
JsonBuffer* getContainerBuffer()
|
||||
{
|
||||
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
|
||||
}
|
||||
|
||||
JsonNode* getContainerChild()
|
||||
{
|
||||
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
|
||||
}
|
||||
|
||||
@ -148,6 +151,23 @@ public:
|
||||
|
||||
void removeChild(JsonNode* childToRemove);
|
||||
|
||||
void 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;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
JsonNode* next;
|
||||
JsonNodeContent content;
|
||||
@ -155,4 +175,12 @@ private:
|
||||
|
||||
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
||||
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
||||
|
||||
void setAsProxyOfSelf();
|
||||
|
||||
void setAsProxyOf(JsonNode* target)
|
||||
{
|
||||
type = JSON_PROXY;
|
||||
content.asProxy.target = target;
|
||||
}
|
||||
};
|
37
srcs/Internals/JsonNodeWrapper.h
Normal file
37
srcs/Internals/JsonNodeWrapper.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonNode.h"
|
||||
class JsonValue;
|
||||
|
||||
class JsonNodeWrapper
|
||||
{
|
||||
friend JsonValue;
|
||||
|
||||
public:
|
||||
JsonNodeWrapper()
|
||||
: _node(0)
|
||||
{
|
||||
}
|
||||
|
||||
explicit JsonNodeWrapper(JsonNode* node)
|
||||
: _node(node)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void duplicate(const JsonNodeWrapper& other)
|
||||
{
|
||||
if (!_node)
|
||||
{
|
||||
_node = other._node;
|
||||
}
|
||||
else
|
||||
{
|
||||
_node->duplicate(other._node);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode* _node;
|
||||
};
|
||||
|
Reference in New Issue
Block a user