JsonObject.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
  60. newKeyNode->content.asKey.key = key;
  61. newKeyNode->content.asKey.value = newValueNode;
  62. if (lastChild)
  63. lastChild->next = newKeyNode;
  64. else
  65. _node->content.asObject.child = newKeyNode;
  66. return newValueNode;
  67. }
  68. size_t JsonObject::printTo(char* buffer, size_t bufferSize) const
  69. {
  70. StringBuilder sb(buffer, bufferSize);
  71. return printTo(sb);
  72. }
  73. size_t JsonObject::printTo(Print& p) const
  74. {
  75. size_t n = 0;
  76. n += p.write('{');
  77. JsonNode* firstChild = _node->content.asObject.child;
  78. for (JsonNode* child = firstChild; child; child = child->next)
  79. {
  80. const char* childKey = child->content.asKey.key;
  81. const char* childValue = child->content.asKey.value->content.asString;
  82. n += EscapedString::printTo(childKey, p);
  83. n += p.write(':');
  84. n += EscapedString::printTo(childValue, p);
  85. if (child->next)
  86. {
  87. n += p.write(',');
  88. }
  89. }
  90. n += p.write('}');
  91. return n;
  92. }