JsonArray.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "ArduinoJson/JsonArray.hpp"
  2. #include "ArduinoJson/JsonObject.hpp"
  3. #include "ArduinoJson/JsonValue.hpp"
  4. using namespace ArduinoJson;
  5. using namespace ArduinoJson::Internals;
  6. JsonValue JsonArray::operator[](int index) const {
  7. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
  8. if (!index)
  9. return JsonValue(*it);
  10. index--;
  11. }
  12. return JsonValue();
  13. }
  14. void JsonArray::add(bool value) {
  15. JsonNode *node = createNode();
  16. if (!node)
  17. return;
  18. node->setAsBoolean(value);
  19. addChild(node);
  20. }
  21. void JsonArray::add(char const *value) {
  22. JsonNode *node = createNode();
  23. if (!node)
  24. return;
  25. node->setAsString(value);
  26. addChild(node);
  27. }
  28. void JsonArray::add(double value, int decimals) {
  29. JsonNode *node = createNode();
  30. if (!node)
  31. return;
  32. node->setAsDouble(value, decimals);
  33. addChild(node);
  34. }
  35. void JsonArray::add(long value) {
  36. JsonNode *node = createNode();
  37. if (!node)
  38. return;
  39. node->setAsLong(value);
  40. addChild(node);
  41. }
  42. // TODO: we should have the same issue as in JsonValue
  43. void JsonArray::add(JsonContainer nestedContainer) {
  44. JsonNode *node = createNode();
  45. if (!node)
  46. return;
  47. node->duplicate(nestedContainer._node);
  48. addChild(node);
  49. }
  50. JsonArray JsonArray::createNestedArray() {
  51. JsonNode *node = createNode();
  52. if (node) {
  53. node->setAsArray(_node->getContainerBuffer());
  54. addChild(node);
  55. }
  56. return JsonArray(node);
  57. }
  58. JsonObject JsonArray::createNestedObject() {
  59. JsonNode *node = createNode();
  60. if (node) {
  61. node->setAsObject(_node->getContainerBuffer());
  62. addChild(node);
  63. }
  64. return JsonObject(node);
  65. }
  66. JsonArrayIterator JsonArray::begin() {
  67. if (!_node)
  68. return end();
  69. return JsonArrayIterator(_node->getContainerChild());
  70. }