JsonContainer.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (_node == other._node) return true;
  44. if (!_node || !other._node) return false;
  45. return _node->getProxyTarget() == other._node->getProxyTarget();
  46. }
  47. void JsonContainer::addChild(JsonNode* childToAdd)
  48. {
  49. if (_node)
  50. _node->addChild(childToAdd);
  51. }
  52. void JsonContainer::removeChild(JsonNode* childToRemove)
  53. {
  54. if (_node)
  55. _node->removeChild(childToRemove);
  56. }
  57. size_t JsonContainer::size() const
  58. {
  59. int size = 0;
  60. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  61. {
  62. size++;
  63. }
  64. return size;
  65. }