2014-09-27 14:51:50 +02:00
|
|
|
#include "JsonObject.h"
|
|
|
|
#include "JsonNode.h"
|
|
|
|
#include "JsonValue.h"
|
|
|
|
|
2014-09-27 15:04:06 +02:00
|
|
|
void JsonValue::operator=(bool value)
|
|
|
|
{
|
|
|
|
if (!_node) return;
|
|
|
|
|
|
|
|
_node->type = JSON_BOOLEAN;
|
|
|
|
_node->content.asBoolean = value;
|
|
|
|
}
|
|
|
|
|
2014-09-27 15:24:16 +02:00
|
|
|
void JsonValue::operator=(char const* value)
|
|
|
|
{
|
|
|
|
if (!_node) return;
|
|
|
|
|
|
|
|
_node->type = JSON_STRING;
|
|
|
|
_node->content.asString = value;
|
|
|
|
}
|
|
|
|
|
2014-09-27 14:59:02 +02:00
|
|
|
void JsonValue::operator=(double value)
|
|
|
|
{
|
|
|
|
if (!_node) return;
|
|
|
|
|
|
|
|
_node->type = JSON_DOUBLE_2_DECIMALS;
|
|
|
|
_node->content.asDouble = value;
|
|
|
|
}
|
2014-09-27 14:51:50 +02:00
|
|
|
|
|
|
|
void JsonValue::operator=(int value)
|
|
|
|
{
|
|
|
|
if (!_node) return;
|
|
|
|
|
|
|
|
_node->type = JSON_INTEGER;
|
|
|
|
_node->content.asInteger = value;
|
|
|
|
}
|
|
|
|
|
2014-09-27 16:18:40 +02:00
|
|
|
void JsonValue::operator=(const JsonObject& object)
|
|
|
|
{
|
2014-09-28 21:18:43 +02:00
|
|
|
setAsProxyTo(object._node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::operator=(JsonValue const& value)
|
|
|
|
{
|
|
|
|
if (!_node)
|
2014-09-28 21:04:59 +02:00
|
|
|
{
|
2014-09-28 21:18:43 +02:00
|
|
|
_node = value._node;
|
|
|
|
return;
|
2014-09-28 21:04:59 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 21:18:43 +02:00
|
|
|
JsonNodeType nodeType = value._node ? value._node->type : JSON_UNDEFINED;
|
|
|
|
|
|
|
|
|
|
|
|
switch (nodeType)
|
|
|
|
{
|
|
|
|
case JSON_UNDEFINED:
|
|
|
|
_node->type = JSON_UNDEFINED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_INTEGER:
|
|
|
|
_node->type = JSON_INTEGER;
|
|
|
|
_node->content.asInteger = value._node->content.asInteger;
|
|
|
|
break;
|
|
|
|
|
2014-09-28 21:22:20 +02:00
|
|
|
case JSON_DOUBLE_0_DECIMALS:
|
|
|
|
|
2014-09-28 21:18:43 +02:00
|
|
|
case JSON_OBJECT:
|
|
|
|
case JSON_ARRAY:
|
|
|
|
case JSON_PROXY:
|
|
|
|
setAsProxyTo(value._node);
|
2014-09-28 21:22:20 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
*_node = *value._node;
|
|
|
|
break;
|
2014-09-28 21:18:43 +02:00
|
|
|
}
|
2014-09-27 16:18:40 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 15:25:00 +02:00
|
|
|
JsonValue::operator bool() const
|
2014-09-27 15:04:06 +02:00
|
|
|
{
|
2014-09-28 21:04:59 +02:00
|
|
|
const JsonNode* node = getActualNode();
|
|
|
|
|
|
|
|
if (!node || node->type != JSON_BOOLEAN) return 0;
|
2014-09-27 15:04:06 +02:00
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
return node->content.asBoolean;
|
2014-09-27 15:04:06 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 15:25:00 +02:00
|
|
|
JsonValue::operator char const*() const
|
2014-09-27 15:24:16 +02:00
|
|
|
{
|
2014-09-28 21:04:59 +02:00
|
|
|
const JsonNode* node = getActualNode();
|
2014-09-27 15:24:16 +02:00
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
if (!node || node->type != JSON_STRING) return 0;
|
|
|
|
|
|
|
|
return node->content.asString;
|
2014-09-27 15:24:16 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 15:25:00 +02:00
|
|
|
JsonValue::operator double() const
|
2014-09-27 14:59:02 +02:00
|
|
|
{
|
2014-09-28 21:04:59 +02:00
|
|
|
const JsonNode* node = getActualNode();
|
|
|
|
|
|
|
|
if (!node || node->type < JSON_DOUBLE_0_DECIMALS) return 0;
|
2014-09-27 14:59:02 +02:00
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
return node->content.asDouble;
|
2014-09-27 14:59:02 +02:00
|
|
|
}
|
2014-09-27 14:51:50 +02:00
|
|
|
|
2014-09-27 15:25:00 +02:00
|
|
|
JsonValue::operator int() const
|
2014-09-27 14:51:50 +02:00
|
|
|
{
|
2014-09-28 21:04:59 +02:00
|
|
|
const JsonNode* node = getActualNode();
|
|
|
|
|
|
|
|
if (!node || node->type != JSON_INTEGER) return 0;
|
2014-09-27 14:51:50 +02:00
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
return node->content.asInteger;
|
2014-09-27 16:18:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue::operator JsonObject() const
|
|
|
|
{
|
2014-09-28 21:04:59 +02:00
|
|
|
return JsonObject(getActualNode());
|
|
|
|
}
|
|
|
|
|
2014-09-28 21:18:43 +02:00
|
|
|
void JsonValue::setAsProxyTo(JsonNode* target)
|
|
|
|
{
|
|
|
|
if (_node)
|
|
|
|
{
|
|
|
|
_node->type = JSON_PROXY;
|
|
|
|
_node->content.asProxy.target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
_node = target;
|
|
|
|
}
|
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
JsonNode* JsonValue::getActualNode() const
|
|
|
|
{
|
|
|
|
JsonNode* target = _node;
|
|
|
|
|
|
|
|
while (target && target->type == JSON_PROXY)
|
|
|
|
target = target->content.asProxy.target;
|
|
|
|
|
|
|
|
return target;
|
2014-09-27 14:51:50 +02:00
|
|
|
}
|