JsonValue.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "JsonValue.h"
  2. #include "JsonArray.h"
  3. #include "JsonObject.h"
  4. #include "Internals/JsonNode.h"
  5. void JsonValue::operator=(bool value)
  6. {
  7. if (!_node) return;
  8. _node->type = JSON_BOOLEAN;
  9. _node->content.asBoolean = value;
  10. }
  11. void JsonValue::operator=(char const* value)
  12. {
  13. if (!_node) return;
  14. _node->type = JSON_STRING;
  15. _node->content.asString = value;
  16. }
  17. void JsonValue::operator=(double value)
  18. {
  19. set(value, 2);
  20. }
  21. void JsonValue::set(double value, int decimals)
  22. {
  23. if (!_node) return;
  24. _node->type = (JsonNodeType) (JSON_DOUBLE_0_DECIMALS + decimals);
  25. _node->content.asDouble = value;
  26. }
  27. void JsonValue::operator=(int value)
  28. {
  29. if (!_node) return;
  30. _node->type = JSON_INTEGER;
  31. _node->content.asInteger = value;
  32. }
  33. void JsonValue::operator=(const JsonContainer& object)
  34. {
  35. setAsProxyTo(object._node);
  36. }
  37. void JsonValue::operator=(JsonValue const& value)
  38. {
  39. if (!_node)
  40. {
  41. _node = value._node;
  42. return;
  43. }
  44. JsonNodeType nodeType = value._node ? value._node->type : JSON_UNDEFINED;
  45. switch (nodeType)
  46. {
  47. case JSON_UNDEFINED:
  48. _node->type = JSON_UNDEFINED;
  49. break;
  50. case JSON_INTEGER:
  51. _node->type = JSON_INTEGER;
  52. _node->content.asInteger = value._node->content.asInteger;
  53. break;
  54. case JSON_DOUBLE_0_DECIMALS:
  55. case JSON_OBJECT:
  56. case JSON_ARRAY:
  57. case JSON_PROXY:
  58. setAsProxyTo(value._node);
  59. break;
  60. default:
  61. *_node = *value._node;
  62. break;
  63. }
  64. }
  65. JsonValue::operator bool() const
  66. {
  67. const JsonNode* node = getActualNode();
  68. if (!node || node->type != JSON_BOOLEAN) return 0;
  69. return node->content.asBoolean;
  70. }
  71. JsonValue::operator char const*() const
  72. {
  73. const JsonNode* node = getActualNode();
  74. if (!node || node->type != JSON_STRING) return 0;
  75. return node->content.asString;
  76. }
  77. JsonValue::operator double() const
  78. {
  79. const JsonNode* node = getActualNode();
  80. if (!node || node->type < JSON_DOUBLE_0_DECIMALS) return 0;
  81. return node->content.asDouble;
  82. }
  83. JsonValue::operator int() const
  84. {
  85. const JsonNode* node = getActualNode();
  86. if (!node || node->type != JSON_INTEGER) return 0;
  87. return node->content.asInteger;
  88. }
  89. JsonValue::operator JsonArray() const
  90. {
  91. return JsonArray(getActualNode());
  92. }
  93. JsonValue::operator JsonObject() const
  94. {
  95. return JsonObject(getActualNode());
  96. }
  97. void JsonValue::setAsProxyTo(JsonNode* target)
  98. {
  99. if (_node)
  100. {
  101. _node->type = JSON_PROXY;
  102. _node->content.asProxy.target = target;
  103. }
  104. _node = target;
  105. }
  106. JsonNode* JsonValue::getActualNode() const
  107. {
  108. JsonNode* target = _node;
  109. while (target && target->type == JSON_PROXY)
  110. target = target->content.asProxy.target;
  111. return target;
  112. }