JsonContainer.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "JsonContainer.h"
  2. #include "JsonBuffer.h"
  3. #include "Internals/StringBuilder.h"
  4. #include "Internals/CompactJsonWriter.h"
  5. #include "Internals/PrettyJsonWriter.h"
  6. using namespace ArduinoJson::Internals;
  7. size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
  8. {
  9. StringBuilder sb(buffer, bufferSize);
  10. return printTo(sb);
  11. }
  12. size_t JsonContainer::printTo(Print& p) const
  13. {
  14. CompactJsonWriter writer(p);
  15. _node->writeTo(writer);
  16. return writer.bytesWritten();
  17. }
  18. size_t JsonContainer::prettyPrintTo(char* buffer, size_t bufferSize) const
  19. {
  20. StringBuilder sb(buffer, bufferSize);
  21. return prettyPrintTo(sb);
  22. }
  23. size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
  24. {
  25. PrettyJsonWriter writer(p);
  26. _node->writeTo(writer);
  27. return writer.bytesWritten();
  28. }
  29. JsonNode* JsonContainer::createNode(JsonNodeType type)
  30. {
  31. if (!_node) return 0;
  32. JsonBuffer* buffer = _node->content.asContainer.buffer;
  33. return buffer->createNode(type);
  34. }
  35. bool JsonContainer::checkNodeType(JsonNodeType expectedType)
  36. {
  37. return _node && _node->type == expectedType;
  38. }
  39. bool JsonContainer::operator==(const JsonContainer & other) const
  40. {
  41. return _node == other._node;
  42. }
  43. void JsonContainer::addChild(JsonNode* newChild)
  44. {
  45. JsonNode* lastChild = _node->content.asContainer.child;
  46. if (!lastChild)
  47. {
  48. _node->content.asContainer.child = newChild = newChild;
  49. return;
  50. }
  51. while (lastChild->next)
  52. lastChild = lastChild->next;
  53. lastChild->next = newChild;
  54. }
  55. void JsonContainer::removeChildAfter(JsonNode* child, JsonNode* previous)
  56. {
  57. if (previous)
  58. previous->next = child->next;
  59. else
  60. _node->content.asContainer.child = child->next;
  61. }
  62. size_t JsonContainer::size() const
  63. {
  64. int size = 0;
  65. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  66. {
  67. size++;
  68. }
  69. return size;
  70. }