diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index 53085109..0e6f58ca 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -7,8 +7,7 @@ enum JsonNodeType JSON_ARRAY, JSON_OBJECT, JSON_KEY, - JSON_TRUE, - JSON_FALSE, + JSON_BOOLEAN, JSON_STRING, JSON_INTEGER, JSON_DOUBLE_0_DECIMALS, @@ -24,6 +23,7 @@ struct JsonNode union { + bool asBoolean; double asDouble; int asInteger; diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index e2c4b7d8..4a75a7cb 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -6,22 +6,6 @@ struct JsonNode; class JsonObject { - // friend class JsonValue; - // - //public: - // JsonObject(JsonBuffer& buffer, JsonNode& node) - // : _buffer(buffer), _node(node) - // { - // } - // - // JsonObject createObject(const char* key) - // { - // JsonObject innerObject = _buffer.createObject(); - // addNodeAt(key, innerObject._node); - // return innerObject; - // } - // - public: JsonObject(JsonBuffer* buffer, JsonNode* node) @@ -37,8 +21,4 @@ private: JsonBuffer* _buffer; JsonNode* _node; JsonNode* getOrCreateNodeAt(char const* key); - - // - // // TODO: pull up - // void appendChild(JsonNode& newChild); }; \ No newline at end of file diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index 0e5b7deb..e9add453 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -2,6 +2,14 @@ #include "JsonNode.h" #include "JsonValue.h" +void JsonValue::operator=(bool value) +{ + if (!_node) return; + + _node->type = JSON_BOOLEAN; + _node->content.asBoolean = value; +} + void JsonValue::operator=(double value) { if (!_node) return; @@ -18,6 +26,13 @@ void JsonValue::operator=(int value) _node->content.asInteger = value; } +JsonValue::operator bool() +{ + if (!_node || _node->type != JSON_BOOLEAN) return 0; + + return _node->content.asBoolean; +} + JsonValue::operator double() { if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0; diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index 39e5b2c7..d6f9cbe6 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -7,17 +7,17 @@ class JsonValue { public: - - JsonValue(JsonNode* node) + + explicit JsonValue(JsonNode* node) : _node(node) { } - // void operator=(const JsonObject& object); + void operator=(bool); void operator=(double); void operator=(int); - - // operator JsonObject(); + + operator bool(); operator double(); operator int(); diff --git a/tests/JsonObjectTests.cpp b/tests/JsonObjectTests.cpp index 4f528615..796e7c29 100644 --- a/tests/JsonObjectTests.cpp +++ b/tests/JsonObjectTests.cpp @@ -52,4 +52,18 @@ TEST(JsonObjectTests, GivenAnDoubleStored_WhenRetreivingTheValue_ThenTheValueIsT EXPECT_EQ(123.45, (double) object["hello"]); EXPECT_EQ(456.78, (double) object["world"]); +} + + +TEST(JsonObjectTests, GivenABooleanStored_WhenRetreivingTheValue_ThenTheValueIsTheSame) +{ + StaticJsonBuffer<42> json; + + JsonObject object = json.createObject(); + + object["hello"] = true; + object["world"] = false; + + EXPECT_TRUE((bool) object["hello"]); + EXPECT_FALSE((bool) object["world"]); } \ No newline at end of file