| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "JsonObject.h"
- #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=(char const* value)
- {
- if (!_node) return;
- _node->type = JSON_STRING;
- _node->content.asString = value;
- }
- void JsonValue::operator=(double value)
- {
- if (!_node) return;
- _node->type = JSON_DOUBLE_2_DECIMALS;
- _node->content.asDouble = value;
- }
- void JsonValue::operator=(int value)
- {
- if (!_node) return;
- _node->type = JSON_INTEGER;
- _node->content.asInteger = value;
- }
- void JsonValue::operator=(const JsonObject& object)
- {
- setAsProxyTo(object._node);
- }
- void JsonValue::operator=(JsonValue const& value)
- {
- if (!_node)
- {
- _node = value._node;
- return;
- }
-
- 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;
- case JSON_DOUBLE_0_DECIMALS:
- case JSON_OBJECT:
- case JSON_ARRAY:
- case JSON_PROXY:
- setAsProxyTo(value._node);
- default:
- *_node = *value._node;
- break;
- }
- }
- JsonValue::operator bool() const
- {
- const JsonNode* node = getActualNode();
- if (!node || node->type != JSON_BOOLEAN) return 0;
- return node->content.asBoolean;
- }
- JsonValue::operator char const*() const
- {
- const JsonNode* node = getActualNode();
- if (!node || node->type != JSON_STRING) return 0;
- return node->content.asString;
- }
- JsonValue::operator double() const
- {
- const JsonNode* node = getActualNode();
- if (!node || node->type < JSON_DOUBLE_0_DECIMALS) return 0;
- return node->content.asDouble;
- }
- JsonValue::operator int() const
- {
- const JsonNode* node = getActualNode();
- if (!node || node->type != JSON_INTEGER) return 0;
- return node->content.asInteger;
- }
- JsonValue::operator JsonObject() const
- {
- return JsonObject(getActualNode());
- }
- void JsonValue::setAsProxyTo(JsonNode* target)
- {
- if (_node)
- {
- _node->type = JSON_PROXY;
- _node->content.asProxy.target = target;
- }
- _node = target;
- }
- JsonNode* JsonValue::getActualNode() const
- {
- JsonNode* target = _node;
- while (target && target->type == JSON_PROXY)
- target = target->content.asProxy.target;
- return target;
- }
|