JsonValue.cpp 2.6 KB

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