JsonObject.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #pragma once
  7. #include "Internals/JsonPrintable.hpp"
  8. #include "Internals/List.hpp"
  9. #include "Internals/ReferenceType.hpp"
  10. #include "JsonPair.hpp"
  11. #define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
  12. (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
  13. namespace ArduinoJson {
  14. class JsonArray;
  15. class JsonBuffer;
  16. class JsonObject : public Internals::JsonPrintable<JsonObject>,
  17. public Internals::ReferenceType,
  18. public Internals::List<JsonPair> {
  19. friend class JsonBuffer;
  20. public:
  21. typedef const char *key_type;
  22. typedef JsonPair value_type;
  23. JsonVariant &at(key_type key);
  24. const JsonVariant &at(key_type key) const;
  25. JsonVariant &operator[](key_type key);
  26. const JsonVariant &operator[](key_type key) const { return at(key); }
  27. void remove(key_type key);
  28. template <typename T>
  29. void add(key_type key, T value) {
  30. add(key).set(value);
  31. }
  32. void add(key_type key, JsonArray &array) { add(key).set(array); }
  33. void add(key_type key, JsonObject &object) { add(key).set(object); }
  34. JsonArray &createNestedArray(key_type key);
  35. JsonObject &createNestedObject(key_type key);
  36. static JsonObject &invalid() { return _invalid; }
  37. template <typename T>
  38. void writeTo(T &writer) const;
  39. private:
  40. // constructor is private, instance must be created via JsonBuffer
  41. explicit JsonObject(JsonBuffer *buffer) : List(buffer) {}
  42. JsonVariant &add(key_type key) { return (*this)[key]; }
  43. node_type *getNodeAt(key_type key) const;
  44. node_type *getOrCreateNodeAt(key_type key);
  45. static JsonObject _invalid;
  46. };
  47. }