Fixed inner object bug

This commit is contained in:
Benoit Blanchon
2014-09-28 21:04:59 +02:00
parent bc44c36385
commit e190b20ae1
5 changed files with 47 additions and 15 deletions

View File

@ -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;
};

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -30,5 +30,7 @@ public:
private:
JsonNode* _node;
JsonNode* getActualNode() const;
};