JsonArray.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #pragma once
  6. #include "JsonObjectBase.h"
  7. #include "StringBuilder.h"
  8. namespace ArduinoJson
  9. {
  10. namespace Generator
  11. {
  12. template<int N>
  13. class JsonArray : public JsonObjectBase
  14. {
  15. public:
  16. JsonArray()
  17. {
  18. itemCount = 0;
  19. }
  20. template<typename T>
  21. void add(T value)
  22. {
  23. if (itemCount >= N) return;
  24. items[itemCount++].set(value);
  25. }
  26. template<int DIGITS>
  27. void add(double value)
  28. {
  29. if (itemCount >= N) return;
  30. Internals::JsonValue& v = items[itemCount++];
  31. v.set<DIGITS>(value);
  32. }
  33. using JsonObjectBase::printTo;
  34. private:
  35. Internals::JsonValue items[N];
  36. int itemCount;
  37. virtual size_t printTo(Print& p) const
  38. {
  39. size_t n = 0;
  40. n += p.write('[');
  41. for (int i = 0; i < itemCount; i++)
  42. {
  43. if (i > 0)
  44. {
  45. n += p.write(',');
  46. }
  47. n += items[i].printTo(p);
  48. }
  49. n += p.write(']');
  50. return n;
  51. }
  52. };
  53. }
  54. }