JsonNode.hpp 5.1 KB

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