JsonNode.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. class JsonBuffer;
  3. class JsonWriter;
  4. class JsonNodeIterator;
  5. class JsonNode
  6. {
  7. friend class JsonNodeIterator;
  8. enum JsonNodeType
  9. {
  10. JSON_UNDEFINED,
  11. JSON_NULL,
  12. JSON_ARRAY,
  13. JSON_OBJECT,
  14. JSON_KEY_VALUE,
  15. JSON_BOOLEAN,
  16. JSON_STRING,
  17. JSON_LONG,
  18. JSON_PROXY,
  19. JSON_DOUBLE_0_DECIMALS,
  20. JSON_DOUBLE_1_DECIMAL,
  21. JSON_DOUBLE_2_DECIMALS,
  22. // etc.
  23. };
  24. union JsonNodeContent
  25. {
  26. bool asBoolean;
  27. double asDouble;
  28. long asInteger;
  29. const char* asString;
  30. struct
  31. {
  32. const char* key;
  33. JsonNode* value;
  34. } asKey;
  35. struct
  36. {
  37. JsonNode* child;
  38. JsonBuffer* buffer;
  39. } asContainer;
  40. struct
  41. {
  42. JsonNode* target;
  43. } asProxy;
  44. };
  45. public:
  46. JsonNode()
  47. : type(JSON_UNDEFINED), next(0)
  48. {
  49. }
  50. void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
  51. void setAsArray(JsonBuffer* buffer)
  52. {
  53. type = JSON_ARRAY;
  54. content.asContainer.child = 0;
  55. content.asContainer.buffer = buffer;
  56. }
  57. void setAsBoolean(bool value)
  58. {
  59. type = JSON_BOOLEAN;
  60. content.asBoolean = value;
  61. }
  62. void setAsLong(int value)
  63. {
  64. type = JSON_LONG;
  65. content.asInteger = value;
  66. }
  67. void setAsString(char const* value)
  68. {
  69. type = JSON_STRING;
  70. content.asString = value;
  71. }
  72. void setAsDouble(double value, int decimals)
  73. {
  74. type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
  75. content.asDouble = value;
  76. }
  77. void setAsObject(JsonBuffer* buffer)
  78. {
  79. type = JSON_OBJECT;
  80. content.asContainer.child = 0;
  81. content.asContainer.buffer = buffer;
  82. }
  83. void setAsObjectKeyValue(const char* key, JsonNode* value)
  84. {
  85. type = JSON_KEY_VALUE;
  86. content.asKey.key = key;
  87. content.asKey.value = value;
  88. }
  89. bool getAsBoolean()
  90. {
  91. return type == JSON_BOOLEAN ? content.asBoolean : false;
  92. }
  93. double getAsDouble()
  94. {
  95. return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
  96. }
  97. long getAsInteger()
  98. {
  99. return type == JSON_LONG ? content.asInteger : 0;
  100. }
  101. const char* getAsString()
  102. {
  103. return type == JSON_STRING ? content.asString : 0;
  104. }
  105. JsonBuffer* getContainerBuffer()
  106. {
  107. if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
  108. return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
  109. }
  110. JsonNode* getContainerChild()
  111. {
  112. if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
  113. return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
  114. }
  115. const char* getAsObjectKey()
  116. {
  117. return type == JSON_KEY_VALUE ? content.asKey.key : 0;
  118. }
  119. JsonNode* getAsObjectValue()
  120. {
  121. return type == JSON_KEY_VALUE ? content.asKey.value : 0;
  122. }
  123. JsonNode* getProxyTarget()
  124. {
  125. return type == JSON_PROXY ? content.asProxy.target : this;
  126. }
  127. void addChild(JsonNode* childToAdd);
  128. void removeChild(JsonNode* childToRemove);
  129. void duplicate(JsonNode* other)
  130. {
  131. if (!other)
  132. {
  133. type = JSON_UNDEFINED;
  134. }
  135. else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
  136. {
  137. other->setAsProxyOfSelf();
  138. setAsProxyOf(other->content.asProxy.target);
  139. }
  140. else
  141. {
  142. *this = *other;
  143. }
  144. }
  145. private:
  146. JsonNode* next;
  147. JsonNodeContent content;
  148. JsonNodeType type;
  149. inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
  150. inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
  151. void setAsProxyOfSelf();
  152. void setAsProxyOf(JsonNode* target)
  153. {
  154. type = JSON_PROXY;
  155. content.asProxy.target = target;
  156. }
  157. };