JsonNode.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "JsonNode.h"
  2. #include "JsonWriter.h"
  3. #include "../JsonArray.h"
  4. #include "../JsonObject.h"
  5. #include "../JsonBuffer.h"
  6. void JsonNode::writeTo(JsonWriter& writer)
  7. {
  8. switch (type)
  9. {
  10. case JSON_PROXY:
  11. content.asProxy.target->writeTo(writer);
  12. break;
  13. case JSON_ARRAY:
  14. writeArrayTo(writer);
  15. break;
  16. case JSON_OBJECT:
  17. writeObjectTo(writer);
  18. break;
  19. case JSON_STRING:
  20. writer.writeString(content.asString);
  21. break;
  22. case JSON_LONG:
  23. writer.writeInteger(content.asInteger);
  24. break;
  25. case JSON_BOOLEAN:
  26. writer.writeBoolean(content.asBoolean);
  27. break;
  28. default: // >= JSON_DOUBLE_0_DECIMALS
  29. writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
  30. break;
  31. }
  32. }
  33. void JsonNode::addChild(JsonNode* childToAdd)
  34. {
  35. if (type == JSON_PROXY)
  36. return content.asProxy.target->addChild(childToAdd);
  37. if (type != JSON_ARRAY && type != JSON_OBJECT)
  38. return;
  39. JsonNode* lastChild = content.asContainer.child;
  40. if (!lastChild)
  41. {
  42. content.asContainer.child = childToAdd;
  43. return;
  44. }
  45. while (lastChild->next)
  46. lastChild = lastChild->next;
  47. lastChild->next = childToAdd;
  48. }
  49. void JsonNode::removeChild(JsonNode* childToRemove)
  50. {
  51. if (type == JSON_PROXY)
  52. return content.asProxy.target->removeChild(childToRemove);
  53. if (type != JSON_ARRAY && type != JSON_OBJECT) return;
  54. if (content.asContainer.child == childToRemove)
  55. {
  56. content.asContainer.child = childToRemove->next;
  57. return;
  58. }
  59. for (JsonNode* child = content.asContainer.child; child; child = child->next)
  60. {
  61. if (child->next == childToRemove)
  62. child->next = childToRemove->next;
  63. }
  64. }
  65. void JsonNode::writeArrayTo(JsonWriter& writer)
  66. {
  67. JsonNode* child = content.asContainer.child;
  68. if (child)
  69. {
  70. writer.beginArray();
  71. for (;;)
  72. {
  73. child->writeTo(writer);
  74. child = child->next;
  75. if (!child) break;
  76. writer.writeComma();
  77. }
  78. writer.endArray();
  79. }
  80. else
  81. {
  82. writer.writeEmptyArray();
  83. }
  84. }
  85. void JsonNode::writeObjectTo(JsonWriter& writer)
  86. {
  87. JsonNode* child = content.asContainer.child;
  88. if (child)
  89. {
  90. writer.beginObject();
  91. for (;;)
  92. {
  93. writer.writeString(child->content.asKey.key);
  94. writer.writeColon();
  95. child->content.asKey.value->writeTo(writer);
  96. child = child->next;
  97. if (!child) break;
  98. writer.writeComma();
  99. }
  100. writer.endObject();
  101. }
  102. else
  103. {
  104. writer.writeEmptyObject();
  105. }
  106. }
  107. void JsonNode::setAsProxyOfSelf()
  108. {
  109. JsonBuffer* buffer = content.asContainer.buffer;
  110. if (!buffer) return;
  111. JsonNode* newNode = buffer->createNode();
  112. if (!newNode) return;
  113. *newNode = *this;
  114. setAsProxyOf(newNode);
  115. }