JsonArrayBase.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #pragma once
  6. #include "JsonPrintable.h"
  7. namespace ArduinoJson
  8. {
  9. namespace Generator
  10. {
  11. class JsonArrayBase : public JsonPrintable
  12. {
  13. public:
  14. JsonArrayBase(JsonValue* items, int capacity)
  15. : items(items), capacity(capacity), count(0)
  16. {
  17. }
  18. void add(const Printable& nestedObject)
  19. {
  20. if (count < capacity)
  21. items[count++] = nestedObject;
  22. }
  23. template<typename T>
  24. void add(T value)
  25. {
  26. if (count < capacity)
  27. items[count++] = value;
  28. }
  29. template<int DIGITS>
  30. void add(double value)
  31. {
  32. if (count >= capacity) return;
  33. JsonValue& v = items[count++];
  34. v.set<DIGITS>(value);
  35. }
  36. virtual size_t printTo(Print& p) const;
  37. using JsonPrintable::printTo;
  38. private:
  39. JsonValue* items;
  40. int capacity, count;
  41. };
  42. }
  43. }