JsonHashTableBase.h 1.4 KB

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