diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index c5c05fea..f90e5f07 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -1,8 +1,10 @@ #include "JsonBuffer.h" -#include "JsonNode.h" -#include "JsonObject.h" + #include // for memset +#include "JsonNode.h" +#include "JsonObject.h" +#include "JsonValue.h" JsonObject JsonBuffer::createObject() { @@ -14,6 +16,12 @@ JsonObject JsonBuffer::createObject() return JsonObject(node); } +JsonValue JsonBuffer::createValue() +{ + JsonNode* node = createNode(JSON_UNDEFINED); + return JsonValue(node); +} + JsonNode* JsonBuffer::createNode(JsonNodeType type) { JsonNode* node = allocateNode(); diff --git a/srcs/JsonBuffer.h b/srcs/JsonBuffer.h index 0d8d1009..ce4399ff 100644 --- a/srcs/JsonBuffer.h +++ b/srcs/JsonBuffer.h @@ -4,6 +4,7 @@ #include "JsonNode.h" class JsonObject; +class JsonValue; struct JsonNode; class JsonBuffer @@ -14,6 +15,8 @@ public: // virtual ~JsonBuffer() = 0; JsonObject createObject(); + JsonValue createValue(); + protected: virtual JsonNode* allocateNode() = 0; diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index 32d48515..4cb3da04 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -5,6 +5,8 @@ struct JsonNode; class JsonObject { + friend JsonValue; + public: JsonObject() : _node(0) @@ -20,6 +22,11 @@ public: JsonValue operator[](const char* key); + bool operator== (const JsonObject& other) const + { + return _node == other._node; + } + private: JsonNode* _node; diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index 7055182c..581461ba 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -34,6 +34,13 @@ void JsonValue::operator=(int value) _node->content.asInteger = value; } +void JsonValue::operator=(const JsonObject& object) +{ + if (!_node) return; + + _node = object._node; +} + JsonValue::operator bool() const { if (!_node || _node->type != JSON_BOOLEAN) return 0; @@ -60,4 +67,9 @@ JsonValue::operator int() const if (!_node || _node->type != JSON_INTEGER) return 0; return _node->content.asInteger; +} + +JsonValue::operator JsonObject() const +{ + return JsonObject(_node); } \ No newline at end of file diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index 00ceb086..9bc3a878 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -1,12 +1,16 @@ #pragma once struct JsonNode; -//class JsonBuffer; class JsonValue { public: + explicit JsonValue() + : _node(0) + { + } + explicit JsonValue(JsonNode* node) : _node(node) { @@ -16,14 +20,15 @@ public: void operator=(const char*); void operator=(double); void operator=(int); - + void operator=(const JsonObject&); + operator bool() const; operator const char*() const; operator double() const; operator int() const; + operator JsonObject() const; private: - //JsonBuffer& _buffer; JsonNode* _node; }; diff --git a/tests/JsonObjectTests.cpp b/tests/JsonObjectTests.cpp index 6e690b36..ae81bdeb 100644 --- a/tests/JsonObjectTests.cpp +++ b/tests/JsonObjectTests.cpp @@ -66,4 +66,16 @@ TEST_F(JsonObjectTests, CanStoreStrings) EXPECT_STREQ("h3110", (const char*) object["hello"]); EXPECT_STREQ("w0r1d", (const char*) object["world"]); +} + +TEST_F(JsonObjectTests, CanStoreInnerObjects) +{ + JsonObject innerObject1 = json.createObject(); + JsonObject innerObject2 = json.createObject(); + + object["hello"] = innerObject1; + object["world"] = innerObject2; + + EXPECT_EQ(innerObject1, (JsonObject) object["hello"]); + EXPECT_EQ(innerObject2, (JsonObject) object["world"]); } \ No newline at end of file diff --git a/tests/JsonValueTests.cpp b/tests/JsonValueTests.cpp new file mode 100644 index 00000000..c6be6b07 --- /dev/null +++ b/tests/JsonValueTests.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +class JsonValueTests : public ::testing::Test +{ +protected: + virtual void SetUp() + { + jsonValue = json.createValue(); + } + + StaticJsonBuffer<42> json; + JsonValue jsonValue; +}; + + +TEST_F(JsonValueTests, CanStoreInteger) +{ + jsonValue = 123; + + EXPECT_EQ(123, (int) jsonValue); +} + +TEST_F(JsonValueTests, CanStoreDouble) +{ + jsonValue = 123.45; + + EXPECT_EQ(123.45, (double) jsonValue); +} + +TEST_F(JsonValueTests, CanStoreTrue) +{ + jsonValue = true; + EXPECT_TRUE((bool) jsonValue); +} + +TEST_F(JsonValueTests, CanStoreFalse) +{ + jsonValue = false; + EXPECT_FALSE((bool) jsonValue); +} + +TEST_F(JsonValueTests, CanStoreString) +{ + jsonValue = "hello"; + + EXPECT_STREQ("hello", (const char*) jsonValue); +} + +TEST_F(JsonValueTests, CanStoreObject) +{ + JsonObject innerObject1 = json.createObject(); + + jsonValue = innerObject1; + + EXPECT_EQ(innerObject1, (JsonObject) jsonValue); +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index a278b7ee..7db8b342 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -86,6 +86,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 592f014f..582073a8 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -30,5 +30,8 @@ Source Files + + Source Files + \ No newline at end of file