JsonObjectBase.h 1.4 KB

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