diff --git a/srcs/Internals/JsonNode.cpp b/srcs/Internals/JsonNode.cpp index 509def30..a15ec897 100644 --- a/srcs/Internals/JsonNode.cpp +++ b/srcs/Internals/JsonNode.cpp @@ -34,6 +34,41 @@ void JsonNode::writeTo(JsonWriter& writer) } } +void JsonNode::addChildToContainer(JsonNode* childToAdd) +{ + if (type != JSON_ARRAY && type != JSON_OBJECT) return; + + JsonNode* lastChild = content.asContainer.child; + + if (!lastChild) + { + content.asContainer.child = childToAdd; + return; + } + + while (lastChild->next) + lastChild = lastChild->next; + + lastChild->next = childToAdd; +} + +void JsonNode::removeChildFromContainer(JsonNode* childToRemove) +{ + if (type != JSON_ARRAY && type != JSON_OBJECT) return; + + if (content.asContainer.child == childToRemove) + { + content.asContainer.child = childToRemove->next; + return; + } + + for (JsonNode* child = content.asContainer.child; child; child = child->next) + { + if (child->next == childToRemove) + child->next = childToRemove->next; + } +} + void JsonNode::writeArrayTo(JsonWriter& writer) { JsonNode* child = content.asContainer.child; diff --git a/srcs/Internals/JsonNode.h b/srcs/Internals/JsonNode.h index 8eb048c9..301ed67e 100644 --- a/srcs/Internals/JsonNode.h +++ b/srcs/Internals/JsonNode.h @@ -22,9 +22,13 @@ class JsonWriter; struct JsonNode { - JsonNode* next; - JsonNodeType type; // <- TODO: hide + JsonNode() + : type(JSON_UNDEFINED), next(0) + { + + } + JsonNode* next; void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer void setAsArray(JsonBuffer* buffer) @@ -112,42 +116,13 @@ struct JsonNode return type == JSON_KEY_VALUE ? content.asKey.value : 0; } - void addChildToContainer(JsonNode* childToAdd) - { - if (type != JSON_ARRAY && type != JSON_OBJECT) return; + void addChildToContainer(JsonNode* childToAdd); - JsonNode* lastChild = content.asContainer.child; - - if (!lastChild) - { - content.asContainer.child = childToAdd; - return; - } - - while (lastChild->next) - lastChild = lastChild->next; - - lastChild->next = childToAdd; - } - - void removeChildFromContainer(JsonNode* childToRemove) - { - if (type != JSON_ARRAY && type != JSON_OBJECT) return; - - if (content.asContainer.child == childToRemove) - { - content.asContainer.child = childToRemove->next; - return; - } - - for (JsonNode* child = content.asContainer.child; child; child = child->next) - { - if (child->next == childToRemove) - child->next = childToRemove->next; - } - } + void removeChildFromContainer(JsonNode* childToRemove); private: + JsonNodeType type; // <- TODO: hide + inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer diff --git a/srcs/JsonArray.cpp b/srcs/JsonArray.cpp index cfa988bc..4ddf1d51 100644 --- a/srcs/JsonArray.cpp +++ b/srcs/JsonArray.cpp @@ -43,7 +43,7 @@ void JsonArray::add(double value, int decimals) void JsonArray::add(long value) { - JsonNode* node = createNode(JSON_LONG); + JsonNode* node = createNode(); if (!node) return; node->setAsLong(value); diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index 050aaac4..0c8028c1 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -1,5 +1,6 @@ #include "JsonBuffer.h" +#include #include // for memset #include "JsonObject.h" @@ -8,16 +9,13 @@ JsonValue JsonBuffer::createValue() { - JsonNode* node = createNode(JSON_UNDEFINED); - return JsonValue(node); + return JsonValue(createNode()); } -JsonNode* JsonBuffer::createNode(JsonNodeType type) +JsonNode* JsonBuffer::createNode() { - JsonNode* node = allocateNode(); + void* node = allocateNode(); if (!node) return 0; - - memset(node, 0, sizeof(JsonNode)); - node->type = type; - return node; + + return new (node) JsonNode(); } \ No newline at end of file diff --git a/srcs/JsonBuffer.h b/srcs/JsonBuffer.h index 61339355..e4b3c416 100644 --- a/srcs/JsonBuffer.h +++ b/srcs/JsonBuffer.h @@ -27,9 +27,9 @@ public: JsonValue createValue(); protected: - virtual JsonNode* allocateNode() = 0; + virtual void* allocateNode() = 0; private: - JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED); + JsonNode* createNode(); }; diff --git a/srcs/JsonContainer.cpp b/srcs/JsonContainer.cpp index 25d39902..b66f9ad4 100644 --- a/srcs/JsonContainer.cpp +++ b/srcs/JsonContainer.cpp @@ -33,19 +33,14 @@ size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const return writer.bytesWritten(); } -JsonNode* JsonContainer::createNode(JsonNodeType type) +JsonNode* JsonContainer::createNode() { if (!_node) return 0; JsonBuffer* buffer = _node->getContainerBuffer(); if (!buffer) return 0; - return buffer->createNode(type); -} - -bool JsonContainer::checkNodeType(JsonNodeType expectedType) -{ - return _node && _node->type == expectedType; + return buffer->createNode(); } bool JsonContainer::operator==(const JsonContainer & other) const diff --git a/srcs/JsonContainer.h b/srcs/JsonContainer.h index dc2a7279..ce5da173 100644 --- a/srcs/JsonContainer.h +++ b/srcs/JsonContainer.h @@ -53,9 +53,7 @@ protected: void addChild(JsonNode*); void removeChild(JsonNode*); - JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED); - - bool checkNodeType(JsonNodeType expectedType); + JsonNode* createNode(); JsonNode* _node; }; diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 83f5aaf9..de82ed1b 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -51,8 +51,6 @@ JsonObject JsonObject::createNestedObject(char const* key) JsonNode* JsonObject::getOrCreateNodeAt(const char* key) { - if (!checkNodeType(JSON_OBJECT)) return 0; - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) { const char* childKey = it->getAsObjectKey(); @@ -61,10 +59,10 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key) return it->getAsObjectValue(); } - JsonNode* newValueNode = createNode(JSON_UNDEFINED); + JsonNode* newValueNode = createNode(); if (!newValueNode) return 0; - JsonNode* newKeyNode = createNode(JSON_KEY_VALUE); + JsonNode* newKeyNode = createNode(); if (!newKeyNode) return 0; newKeyNode->setAsObjectKeyValue(key, newValueNode); diff --git a/srcs/StaticJsonBuffer.h b/srcs/StaticJsonBuffer.h index 3769b211..91e29851 100644 --- a/srcs/StaticJsonBuffer.h +++ b/srcs/StaticJsonBuffer.h @@ -28,7 +28,7 @@ public: } protected: - virtual JsonNode* allocateNode() + virtual void* allocateNode() { if (_size >= CAPACITY) return 0; diff --git a/tests/JsonValueTests.cpp b/tests/JsonValueTests.cpp index 80fc99fd..baa4ab14 100644 --- a/tests/JsonValueTests.cpp +++ b/tests/JsonValueTests.cpp @@ -104,6 +104,7 @@ TEST_F(JsonValueTests, ObjectsAreCopiedByReference) jsonValue2 = jsonValue1; object["hello"] = "world"; + jsonValue1 = 0; EXPECT_EQ(1, ((JsonObject) jsonValue2).size()); } @@ -114,6 +115,7 @@ TEST_F(JsonValueTests, ArraysAreCopiedByReference) jsonValue1 = array; jsonValue2 = jsonValue1; + jsonValue1 = 0; array.add("world");