diff --git a/srcs/JsonArray.h b/srcs/JsonArray.h index 230f5792..36d91796 100644 --- a/srcs/JsonArray.h +++ b/srcs/JsonArray.h @@ -16,7 +16,10 @@ public: // JsonValue operator[](int index); - // template - // void add(T value); + template + void add(T value) + { + addChild(createNode(JSON_UNDEFINED)); + } }; diff --git a/srcs/JsonContainer.cpp b/srcs/JsonContainer.cpp index 44c4e694..d992885c 100644 --- a/srcs/JsonContainer.cpp +++ b/srcs/JsonContainer.cpp @@ -32,12 +32,20 @@ bool JsonContainer::operator==(const JsonContainer & other) const return _node == other._node; } -void JsonContainer::insertChildAfter(JsonNode* newChild, JsonNode* previous) +void JsonContainer::addChild(JsonNode* newChild) { - if (previous) - previous->next = newChild; - else - _node->content.asContainer.child = newChild; + JsonNode* lastChild = _node->content.asContainer.child; + + if (!lastChild) + { + _node->content.asContainer.child = newChild = newChild; + return; + } + + while (lastChild->next) + lastChild = lastChild->next; + + lastChild->next = newChild; } void JsonContainer::removeChildAfter(JsonNode* child, JsonNode* previous) diff --git a/srcs/JsonContainer.h b/srcs/JsonContainer.h index d6715d82..2f6a586e 100644 --- a/srcs/JsonContainer.h +++ b/srcs/JsonContainer.h @@ -41,7 +41,7 @@ protected: return JsonNodeIterator(0); } - void insertChildAfter(JsonNode* newChild, JsonNode* insertAfterMe); + void addChild(JsonNode* newChild); void removeChildAfter(JsonNode* child, JsonNode* previous); JsonNode* createNode(JsonNodeType type); diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 3a1ef1ed..fc570c40 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -38,16 +38,12 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key) { if (!checkNodeType(JSON_OBJECT)) return 0; - JsonNode* lastChild = 0; - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) { const char* childKey = it->content.asKey.key; if (!strcmp(childKey, key)) return it->content.asKey.value; - - lastChild = *it; } JsonNode* newValueNode = createNode(JSON_UNDEFINED); @@ -59,7 +55,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key) newKeyNode->content.asKey.key = key; newKeyNode->content.asKey.value = newValueNode; - insertChildAfter(newKeyNode, lastChild); + addChild(newKeyNode); return newValueNode; } diff --git a/tests/JsonArray_Container_Tests.cpp b/tests/JsonArray_Container_Tests.cpp index 5c7d2b39..99b45c73 100644 --- a/tests/JsonArray_Container_Tests.cpp +++ b/tests/JsonArray_Container_Tests.cpp @@ -18,7 +18,7 @@ TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { EXPECT_EQ(0, array.size()); } -/* + TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) { array.add("hello"); @@ -27,7 +27,7 @@ TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) array.add("world"); EXPECT_EQ(2, array.size()); } - +/* TEST_F(JsonArray_Container_Tests, CanStoreIntegers) { array.add(123);