Moved JsonObject._buffer into the JsonNode's content

This commit is contained in:
Benoit Blanchon
2014-09-27 15:34:34 +02:00
parent a7ff04db0e
commit bcc8cece24
5 changed files with 20 additions and 13 deletions

View File

@ -7,7 +7,11 @@
JsonObject JsonBuffer::createObject() JsonObject JsonBuffer::createObject()
{ {
JsonNode* node = createNode(JSON_OBJECT); JsonNode* node = createNode(JSON_OBJECT);
return JsonObject(this, node);
if (node)
node->content.asObject.buffer = this;
return JsonObject(node);
} }
JsonNode* JsonBuffer::createNode(JsonNodeType type) JsonNode* JsonBuffer::createNode(JsonNodeType type)

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
class JsonBuffer;
enum JsonNodeType enum JsonNodeType
{ {
JSON_UNDEFINED, JSON_UNDEFINED,
@ -37,7 +39,8 @@ struct JsonNode
struct struct
{ {
JsonNode* child; JsonNode* child;
} asObjectNode; JsonBuffer* buffer;
} asObject;
} content; } content;
}; };

View File

@ -33,7 +33,7 @@
size_t JsonObject::size() size_t JsonObject::size()
{ {
JsonNode* firstChild = _node->content.asObjectNode.child; JsonNode* firstChild = _node->content.asObject.child;
int size = 0; int size = 0;
@ -55,7 +55,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
{ {
if (!_node || _node->type != JSON_OBJECT) return 0; if (!_node || _node->type != JSON_OBJECT) return 0;
JsonNode* firstChild = _node->content.asObjectNode.child; JsonNode* firstChild = _node->content.asObject.child;
JsonNode* lastChild = 0; JsonNode* lastChild = 0;
for (JsonNode* child = firstChild; child; child = child->next) for (JsonNode* child = firstChild; child; child = child->next)
@ -68,16 +68,18 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
lastChild = child; lastChild = child;
} }
JsonNode* newValueNode = _buffer->createNode(JSON_UNDEFINED); JsonBuffer* buffer = _node->content.asObject.buffer;
JsonNode* newKeyNode = _buffer->createNode(JSON_KEY); JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
newKeyNode->content.asKey.key = key; newKeyNode->content.asKey.key = key;
newKeyNode->content.asKey.value = newValueNode; newKeyNode->content.asKey.value = newValueNode;
if (lastChild) if (lastChild)
lastChild->next = newKeyNode; lastChild->next = newKeyNode;
else else
_node->content.asObjectNode.child = newKeyNode; _node->content.asObject.child = newKeyNode;
return newValueNode; return newValueNode;
} }

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
class JsonBuffer;
class JsonValue; class JsonValue;
struct JsonNode; struct JsonNode;
@ -12,8 +11,8 @@ public:
{ {
} }
JsonObject(JsonBuffer* buffer, JsonNode* node) JsonObject(JsonNode* node)
: _buffer(buffer), _node(node) : _node(node)
{ {
} }
@ -22,7 +21,7 @@ public:
JsonValue operator[](const char* key); JsonValue operator[](const char* key);
private: private:
JsonBuffer* _buffer;
JsonNode* _node; JsonNode* _node;
JsonNode* getOrCreateNodeAt(char const* key); JsonNode* getOrCreateNodeAt(char const* key);
}; };

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
struct JsonNode;
//class JsonBuffer; //class JsonBuffer;
//class JsonNode;
//class JsonObject;
class JsonValue class JsonValue
{ {