JsonArray.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #pragma once
  7. #include "Internals/JsonPrintable.hpp"
  8. #include "Internals/List.hpp"
  9. #include "Internals/ReferenceType.hpp"
  10. #include "JsonVariant.hpp"
  11. // Returns the size (in bytes) of an array with n elements.
  12. // Can be very handy to determine the size of a StaticJsonBuffer.
  13. #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
  14. (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type))
  15. namespace ArduinoJson {
  16. // Forward declaration
  17. class JsonObject;
  18. class JsonBuffer;
  19. // An array of JsonVariant.
  20. // Can be converted to a JSON string via JsonArray::printTo().
  21. // Can be extracted from a JSON string JsonBuffer::parseArray().
  22. class JsonArray : public Internals::JsonPrintable<JsonArray>,
  23. public Internals::ReferenceType,
  24. public Internals::List<JsonVariant> {
  25. // JsonBuffer is a friend because it needs to call the private constructor of
  26. // JsonArray from createArray() and parseArray().
  27. friend class JsonBuffer;
  28. public:
  29. // Returns the JsonVariant at the specified index (synonym for at())
  30. value_type &operator[](int index) const { return at(index); }
  31. // Returns the JsonVariant at the specified index (synonym for operator[])
  32. value_type &at(int index) const;
  33. // Adds an uninitialized JsonVariant at the end of the array.
  34. value_type &add();
  35. // Adds the specified value at the end of the array.
  36. template <typename T>
  37. void add(T value) {
  38. add().set(value);
  39. }
  40. // Adds the specified double value at the end of the array.
  41. // The value will be printed with the specified number of decimal digits.
  42. void add(double value, uint8_t decimals) { add().set(value, decimals); }
  43. // Adds a reference to the specified JsonArray at the end of the array.
  44. void add(JsonArray &array) { add().set(array); }
  45. // Adds a reference to the specified JsonObject at the end of the array.
  46. void add(JsonObject &obejct) { add().set(obejct); }
  47. // Creates a JsonArray and adds a reference at the end of the array.
  48. // It's a shortcut for JsonBuffer::createArray() and JsonArray::add()
  49. JsonArray &createNestedArray();
  50. // Creates a JsonObject and adds a reference at the end of the array.
  51. // It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
  52. JsonObject &createNestedObject();
  53. // Returns a reference an invalid JsonArray.
  54. // This is used when memory allocation or JSON parsing fail.
  55. static JsonArray &invalid() { return _invalid; }
  56. // Writes the array to a JsonWriter.
  57. template <typename T>
  58. void writeTo(T &writer) const;
  59. private:
  60. // Create an empty JsonArray attached to the specified JsonBuffer.
  61. // Constructor is private: instance must be created via a JsonBuffer
  62. explicit JsonArray(JsonBuffer *buffer) : List(buffer) {}
  63. // The instance returned by JsonArray::invalid()
  64. static JsonArray _invalid;
  65. };
  66. }