JsonContainer.cpp 1.7 KB

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