diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index f8d23c96..9ea81e97 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -5,6 +5,7 @@ class JsonBuffer; enum JsonNodeType { JSON_UNDEFINED, + JSON_PROXY, JSON_NULL, JSON_ARRAY, JSON_OBJECT, @@ -42,5 +43,10 @@ struct JsonNode JsonBuffer* buffer; } asObject; + struct + { + JsonNode* target; + } asProxy; + } content; }; \ No newline at end of file diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index e7a4d202..1979a26b 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -51,6 +51,11 @@ JsonValue JsonObject::operator[](char const* key) return JsonValue(node); } +bool JsonObject::operator==(JsonObject const& other) const +{ + return _node == other._node; +} + JsonNode* JsonObject::getOrCreateNodeAt(char const* key) { if (!_node || _node->type != JSON_OBJECT) return 0; diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index 4cb3da04..5b4e5657 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -22,10 +22,7 @@ public: JsonValue operator[](const char* key); - bool operator== (const JsonObject& other) const - { - return _node == other._node; - } + bool operator==(const JsonObject& other) const; private: JsonNode* _node; diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index 581461ba..4be9ff15 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -36,40 +36,62 @@ void JsonValue::operator=(int value) void JsonValue::operator=(const JsonObject& object) { - if (!_node) return; - + if (_node) + { + _node->type = JSON_PROXY; + _node->content.asProxy.target = object._node; + } + _node = object._node; } JsonValue::operator bool() const { - if (!_node || _node->type != JSON_BOOLEAN) return 0; + const JsonNode* node = getActualNode(); - return _node->content.asBoolean; + if (!node || node->type != JSON_BOOLEAN) return 0; + + return node->content.asBoolean; } JsonValue::operator char const*() const { - if (!_node || _node->type != JSON_STRING) return 0; + const JsonNode* node = getActualNode(); - return _node->content.asString; + if (!node || node->type != JSON_STRING) return 0; + + return node->content.asString; } JsonValue::operator double() const { - if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0; + const JsonNode* node = getActualNode(); - return _node->content.asDouble; + if (!node || node->type < JSON_DOUBLE_0_DECIMALS) return 0; + + return node->content.asDouble; } JsonValue::operator int() const { - if (!_node || _node->type != JSON_INTEGER) return 0; + const JsonNode* node = getActualNode(); - return _node->content.asInteger; + if (!node || node->type != JSON_INTEGER) return 0; + + return node->content.asInteger; } JsonValue::operator JsonObject() const { - return JsonObject(_node); + return JsonObject(getActualNode()); +} + +JsonNode* JsonValue::getActualNode() const +{ + JsonNode* target = _node; + + while (target && target->type == JSON_PROXY) + target = target->content.asProxy.target; + + return target; } \ No newline at end of file diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index 9bc3a878..d3232d63 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -30,5 +30,7 @@ public: private: JsonNode* _node; + + JsonNode* getActualNode() const; };