JsonObject.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include "../include/ArduinoJson/JsonObject.hpp"
  7. #include <new> // required for placement new
  8. #include <string.h> // for strcmp
  9. #include "../include/ArduinoJson/JsonBuffer.hpp"
  10. #include "../include/ArduinoJson/JsonArray.hpp"
  11. #include "../include/ArduinoJson/JsonValue.hpp"
  12. #include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
  13. #include "../include/ArduinoJson/Internals/StringBuilder.hpp"
  14. using namespace ArduinoJson;
  15. using namespace ArduinoJson::Internals;
  16. JsonObject JsonObject::_invalid(NULL);
  17. int JsonObject::size() const {
  18. int nodeCount = 0;
  19. for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
  20. return nodeCount;
  21. }
  22. JsonValue &JsonObject::at(const char *key) {
  23. JsonObjectNode *node = getNodeAt(key);
  24. return node ? node->pair.value : JsonValue::invalid();
  25. }
  26. const JsonValue &JsonObject::at(const char *key) const {
  27. JsonObjectNode *node = getNodeAt(key);
  28. return node ? node->pair.value : JsonValue::invalid();
  29. }
  30. JsonValue &JsonObject::operator[](const char *key) {
  31. JsonObjectNode *node = getOrCreateNodeAt(key);
  32. return node ? node->pair.value : JsonValue::invalid();
  33. }
  34. void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
  35. JsonArray &JsonObject::createNestedArray(char const *key) {
  36. if (!_buffer) return JsonArray::invalid();
  37. JsonArray &array = _buffer->createArray();
  38. add(key, array);
  39. return array;
  40. }
  41. JsonObject &JsonObject::createNestedObject(const char *key) {
  42. if (!_buffer) return JsonObject::invalid();
  43. JsonObject &object = _buffer->createObject();
  44. add(key, object);
  45. return object;
  46. }
  47. JsonObjectNode *JsonObject::getNodeAt(const char *key) const {
  48. for (JsonObjectNode *node = _firstNode; node; node = node->next) {
  49. if (!strcmp(node->pair.key, key)) return node;
  50. }
  51. return NULL;
  52. }
  53. JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
  54. JsonObjectNode *existingNode = getNodeAt(key);
  55. if (existingNode) return existingNode;
  56. JsonObjectNode *newNode = createNode(key);
  57. if (newNode) addNode(newNode);
  58. return newNode;
  59. }
  60. JsonObjectNode *JsonObject::createNode(const char *key) {
  61. if (!_buffer) return NULL;
  62. void *ptr = _buffer->alloc(sizeof(JsonObjectNode));
  63. return ptr ? new (ptr) JsonObjectNode(key) : NULL;
  64. }
  65. void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
  66. if (!_firstNode) {
  67. _firstNode = nodeToAdd;
  68. } else {
  69. JsonObjectNode *lastNode = _firstNode;
  70. while (lastNode->next) lastNode = lastNode->next;
  71. lastNode->next = nodeToAdd;
  72. }
  73. }
  74. void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
  75. if (!nodeToRemove) return;
  76. if (nodeToRemove == _firstNode) {
  77. _firstNode = nodeToRemove->next;
  78. } else {
  79. for (JsonObjectNode *node = _firstNode; node; node = node->next)
  80. if (node->next == nodeToRemove) node->next = nodeToRemove->next;
  81. }
  82. }
  83. template <typename T>
  84. void JsonObject::writeTo(T &writer) const {
  85. JsonObjectNode *node = _firstNode;
  86. if (node) {
  87. writer.beginObject();
  88. for (;;) {
  89. writer.writeString(node->pair.key);
  90. writer.writeColon();
  91. node->pair.value.writeTo(writer);
  92. node = node->next;
  93. if (!node) break;
  94. writer.writeComma();
  95. }
  96. writer.endObject();
  97. } else {
  98. writer.writeEmptyObject();
  99. }
  100. }
  101. template void JsonObject::writeTo(JsonWriter &writer) const;
  102. template void JsonObject::writeTo(PrettyJsonWriter &writer) const;