JsonValue.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_OBJECT:
  50. case JSON_ARRAY:
  51. case JSON_PROXY:
  52. setAsProxyTo(value._node);
  53. }
  54. }
  55. JsonValue::operator bool() const
  56. {
  57. const JsonNode* node = getActualNode();
  58. if (!node || node->type != JSON_BOOLEAN) return 0;
  59. return node->content.asBoolean;
  60. }
  61. JsonValue::operator char const*() const
  62. {
  63. const JsonNode* node = getActualNode();
  64. if (!node || node->type != JSON_STRING) return 0;
  65. return node->content.asString;
  66. }
  67. JsonValue::operator double() const
  68. {
  69. const JsonNode* node = getActualNode();
  70. if (!node || node->type < JSON_DOUBLE_0_DECIMALS) return 0;
  71. return node->content.asDouble;
  72. }
  73. JsonValue::operator int() const
  74. {
  75. const JsonNode* node = getActualNode();
  76. if (!node || node->type != JSON_INTEGER) return 0;
  77. return node->content.asInteger;
  78. }
  79. JsonValue::operator JsonObject() const
  80. {
  81. return JsonObject(getActualNode());
  82. }
  83. void JsonValue::setAsProxyTo(JsonNode* target)
  84. {
  85. if (_node)
  86. {
  87. _node->type = JSON_PROXY;
  88. _node->content.asProxy.target = target;
  89. }
  90. _node = target;
  91. }
  92. JsonNode* JsonValue::getActualNode() const
  93. {
  94. JsonNode* target = _node;
  95. while (target && target->type == JSON_PROXY)
  96. target = target->content.asProxy.target;
  97. return target;
  98. }