JsonContainer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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()
  30. {
  31. if (!_node) return 0;
  32. JsonBuffer* buffer = _node->getContainerBuffer();
  33. if (!buffer) return 0;
  34. return buffer->createNode();
  35. }
  36. bool JsonContainer::operator==(const JsonContainer & other) const
  37. {
  38. return _node->getContainerChild() == other._node->getContainerChild();
  39. }
  40. void JsonContainer::addChild(JsonNode* childToAdd)
  41. {
  42. if (_node)
  43. _node->addChildToContainer(childToAdd);
  44. }
  45. void JsonContainer::removeChild(JsonNode* childToRemove)
  46. {
  47. if (_node)
  48. _node->removeChildFromContainer(childToRemove);
  49. }
  50. size_t JsonContainer::size() const
  51. {
  52. int size = 0;
  53. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  54. {
  55. size++;
  56. }
  57. return size;
  58. }