JsonObject.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "JsonObject.h"
  2. #include <string.h> // for strcmp
  3. #include "EscapedString.h"
  4. #include "JsonBuffer.h"
  5. #include "JsonValue.h"
  6. #include "JsonNode.h"
  7. #include "StringBuilder.h"
  8. using namespace ArduinoJson::Internals;
  9. size_t JsonObject::size()
  10. {
  11. JsonNode* firstChild = _node->content.asObject.child;
  12. int size = 0;
  13. for (JsonNode* child = firstChild; child; child = child->next)
  14. {
  15. size++;
  16. }
  17. return size;
  18. }
  19. JsonValue JsonObject::operator[](char const* key)
  20. {
  21. JsonNode* node = getOrCreateNodeAt(key);
  22. return JsonValue(node);
  23. }
  24. void JsonObject::remove(char const* key)
  25. {
  26. JsonNode* firstChild = _node->content.asObject.child;
  27. JsonNode* lastChild = 0;
  28. for (JsonNode* child = firstChild; child; child = child->next)
  29. {
  30. const char* childKey = child->content.asKey.key;
  31. if (!strcmp(childKey, key))
  32. {
  33. if (lastChild)
  34. lastChild->next = child->next;
  35. else
  36. _node->content.asObject.child = child->next;
  37. }
  38. lastChild = child;
  39. }
  40. }
  41. bool JsonObject::operator==(JsonObject const& other) const
  42. {
  43. return _node == other._node;
  44. }
  45. JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
  46. {
  47. if (!_node || _node->type != JSON_OBJECT) return 0;
  48. JsonNode* firstChild = _node->content.asObject.child;
  49. JsonNode* lastChild = 0;
  50. for (JsonNode* child = firstChild; child; child = child->next)
  51. {
  52. const char* childKey = child->content.asKey.key;
  53. if (!strcmp(childKey, key))
  54. return child->content.asKey.value;
  55. lastChild = child;
  56. }
  57. JsonBuffer* buffer = _node->content.asObject.buffer;
  58. JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
  59. if (!newValueNode) return 0;
  60. JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
  61. if (!newKeyNode) return 0;
  62. newKeyNode->content.asKey.key = key;
  63. newKeyNode->content.asKey.value = newValueNode;
  64. if (lastChild)
  65. lastChild->next = newKeyNode;
  66. else
  67. _node->content.asObject.child = newKeyNode;
  68. return newValueNode;
  69. }
  70. size_t JsonObject::printTo(char* buffer, size_t bufferSize) const
  71. {
  72. StringBuilder sb(buffer, bufferSize);
  73. return printTo(sb);
  74. }
  75. size_t JsonObject::printTo(Print& p) const
  76. {
  77. size_t n = 0;
  78. n += p.write('{');
  79. JsonNode* firstChild = _node->content.asObject.child;
  80. for (JsonNode* child = firstChild; child; child = child->next)
  81. {
  82. const char* childKey = child->content.asKey.key;
  83. JsonNode* childValue = child->content.asKey.value;
  84. n += EscapedString::printTo(childKey, p);
  85. n += p.write(':');
  86. switch (childValue->type)
  87. {
  88. case JSON_STRING:
  89. n += EscapedString::printTo(childValue->content.asString, p);
  90. break;
  91. case JSON_INTEGER:
  92. n += p.print(childValue->content.asInteger);
  93. break;
  94. case JSON_BOOLEAN:
  95. n += p.print(childValue->content.asBoolean ? "true" : "false");
  96. break;
  97. }
  98. if (childValue->type >= JSON_DOUBLE_0_DECIMALS)
  99. n += p.print(childValue->content.asDouble, childValue->type - JSON_DOUBLE_0_DECIMALS);
  100. if (child->next)
  101. {
  102. n += p.write(',');
  103. }
  104. }
  105. n += p.write('}');
  106. return n;
  107. }