JsonHashTableBase.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #pragma once
  6. #include "JsonObjectBase.h"
  7. #include "EscapedString.h"
  8. namespace ArduinoJson
  9. {
  10. namespace Generator
  11. {
  12. class JsonHashTableBase : public JsonObjectBase
  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 JsonObjectBase::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 count;
  46. int capacity;
  47. };
  48. }
  49. }