diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index 406f6db7..c5c05fea 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -7,7 +7,11 @@ JsonObject JsonBuffer::createObject() { JsonNode* node = createNode(JSON_OBJECT); - return JsonObject(this, node); + + if (node) + node->content.asObject.buffer = this; + + return JsonObject(node); } JsonNode* JsonBuffer::createNode(JsonNodeType type) diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index 516b3828..f8d23c96 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -1,5 +1,7 @@ #pragma once +class JsonBuffer; + enum JsonNodeType { JSON_UNDEFINED, @@ -37,7 +39,8 @@ struct JsonNode struct { JsonNode* child; - } asObjectNode; + JsonBuffer* buffer; + } asObject; } content; }; \ No newline at end of file diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index e992febb..e7a4d202 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -33,7 +33,7 @@ size_t JsonObject::size() { - JsonNode* firstChild = _node->content.asObjectNode.child; + JsonNode* firstChild = _node->content.asObject.child; int size = 0; @@ -55,7 +55,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key) { if (!_node || _node->type != JSON_OBJECT) return 0; - JsonNode* firstChild = _node->content.asObjectNode.child; + JsonNode* firstChild = _node->content.asObject.child; JsonNode* lastChild = 0; for (JsonNode* child = firstChild; child; child = child->next) @@ -68,16 +68,18 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key) 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.value = newValueNode; if (lastChild) lastChild->next = newKeyNode; else - _node->content.asObjectNode.child = newKeyNode; + _node->content.asObject.child = newKeyNode; return newValueNode; } \ No newline at end of file diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index 3482f122..32d48515 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -1,6 +1,5 @@ #pragma once -class JsonBuffer; class JsonValue; struct JsonNode; @@ -12,8 +11,8 @@ public: { } - JsonObject(JsonBuffer* buffer, JsonNode* node) - : _buffer(buffer), _node(node) + JsonObject(JsonNode* node) + : _node(node) { } @@ -22,7 +21,7 @@ public: JsonValue operator[](const char* key); private: - JsonBuffer* _buffer; JsonNode* _node; + JsonNode* getOrCreateNodeAt(char const* key); }; \ No newline at end of file diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index 53ebf820..00ceb086 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -1,8 +1,7 @@ #pragma once +struct JsonNode; //class JsonBuffer; -//class JsonNode; -//class JsonObject; class JsonValue {