diff --git a/srcs/Internals/JsonNode.cpp b/srcs/Internals/JsonNode.cpp index 2c3e4e1c..216220d2 100644 --- a/srcs/Internals/JsonNode.cpp +++ b/srcs/Internals/JsonNode.cpp @@ -146,4 +146,21 @@ void JsonNode::setAsProxyOfSelf() *newNode = *this; setAsProxyOf(newNode); +} + +void JsonNode::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; + } } \ No newline at end of file diff --git a/srcs/Internals/JsonNode.h b/srcs/Internals/JsonNode.h index b8d234d6..f66ccc85 100644 --- a/srcs/Internals/JsonNode.h +++ b/srcs/Internals/JsonNode.h @@ -152,26 +152,16 @@ public: return type == JSON_PROXY ? content.asProxy.target : this; } + bool isArray() + { + return type == JSON_ARRAY; + } + void addChild(JsonNode* childToAdd); 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; - } - } + void duplicate(JsonNode* other); private: JsonNode* next; diff --git a/srcs/JsonArray.h b/srcs/JsonArray.h index 2f64c9fe..27d8cd49 100644 --- a/srcs/JsonArray.h +++ b/srcs/JsonArray.h @@ -25,5 +25,10 @@ public: JsonArray createNestedArray(); JsonObject createNestedObject(); + + bool success() + { + return _node && _node->isArray(); + } }; diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index 254ab9ea..8c07cec1 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -1,9 +1,7 @@ #include "JsonBuffer.h" #include -#include // for memset -#include "JsonObject.h" #include "JsonValue.h" #include "Internals/JsonNode.h" @@ -20,7 +18,19 @@ JsonNode* JsonBuffer::createNode() return new (node) JsonNode(); } -JsonArray JsonBuffer::parseArray(char const *string) +JsonArray JsonBuffer::parseArray(const char* json) { - return JsonArray(); + JsonNode* root; + + if (json[0] == '[') + { + root = createNode(); + root->setAsArray(this); + } + else + { + root = 0; + } + + return JsonArray(root); } \ No newline at end of file diff --git a/tests/JsonArray_Parser_Tests.cpp b/tests/JsonArray_Parser_Tests.cpp index 8fd41080..9f88d40e 100644 --- a/tests/JsonArray_Parser_Tests.cpp +++ b/tests/JsonArray_Parser_Tests.cpp @@ -11,5 +11,14 @@ TEST_F(JsonArray_Parser_Tests, EmptyArray) { JsonArray array = json.parseArray("[]"); + EXPECT_TRUE(array.success()); + EXPECT_EQ(0, array.size()); +} + +TEST_F(JsonArray_Parser_Tests, Garbage) +{ + JsonArray array = json.parseArray("%*$£¤"); + + EXPECT_FALSE(array.success()); EXPECT_EQ(0, array.size()); } \ No newline at end of file