JsonContainer.cpp 1.7 KB

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