Added JSON_PROXY to copy arrays and objects by reference

This commit is contained in:
Benoit Blanchon
2014-10-09 18:20:40 +02:00
parent bf2d726746
commit 35eaa55b3a
12 changed files with 138 additions and 66 deletions

View File

@ -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;
}
};