JsonValue.cpp 2.8 KB

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