1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #pragma once
|
---|
9 |
|
---|
10 | #include "Arduino/String.hpp"
|
---|
11 | #include "Internals/JsonBufferAllocated.hpp"
|
---|
12 | #include "Internals/JsonPrintable.hpp"
|
---|
13 | #include "Internals/List.hpp"
|
---|
14 | #include "Internals/ReferenceType.hpp"
|
---|
15 | #include "JsonPair.hpp"
|
---|
16 | #include "TypeTraits/EnableIf.hpp"
|
---|
17 | #include "TypeTraits/IsFloatingPoint.hpp"
|
---|
18 | #include "TypeTraits/IsReference.hpp"
|
---|
19 | #include "TypeTraits/IsSame.hpp"
|
---|
20 |
|
---|
21 | // Returns the size (in bytes) of an object with n elements.
|
---|
22 | // Can be very handy to determine the size of a StaticJsonBuffer.
|
---|
23 | #define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
|
---|
24 | (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
|
---|
25 |
|
---|
26 | namespace ArduinoJson {
|
---|
27 |
|
---|
28 | // Forward declarations
|
---|
29 | class JsonArray;
|
---|
30 | class JsonBuffer;
|
---|
31 |
|
---|
32 | // A dictionary of JsonVariant indexed by string (char*)
|
---|
33 | //
|
---|
34 | // The constructor is private, instances must be created via
|
---|
35 | // JsonBuffer::createObject() or JsonBuffer::parseObject().
|
---|
36 | // A JsonObject can be serialized to a JSON string via JsonObject::printTo().
|
---|
37 | // It can also be deserialized from a JSON string via JsonBuffer::parseObject().
|
---|
38 | class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
---|
39 | public Internals::ReferenceType,
|
---|
40 | public Internals::List<JsonPair>,
|
---|
41 | public Internals::JsonBufferAllocated {
|
---|
42 | public:
|
---|
43 | // A meta-function that returns true if type T can be used in
|
---|
44 | // JsonObject::set()
|
---|
45 | template <typename T>
|
---|
46 | struct CanSet {
|
---|
47 | static const bool value = JsonVariant::IsConstructibleFrom<T>::value ||
|
---|
48 | TypeTraits::IsSame<T, String&>::value ||
|
---|
49 | TypeTraits::IsSame<T, const String&>::value;
|
---|
50 | };
|
---|
51 |
|
---|
52 | // Create an empty JsonArray attached to the specified JsonBuffer.
|
---|
53 | // You should not use this constructor directly.
|
---|
54 | // Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject().
|
---|
55 | FORCE_INLINE explicit JsonObject(JsonBuffer* buffer)
|
---|
56 | : Internals::List<JsonPair>(buffer) {}
|
---|
57 |
|
---|
58 | // Gets or sets the value associated with the specified key.
|
---|
59 | FORCE_INLINE JsonObjectSubscript<const char*> operator[](const char* key);
|
---|
60 | FORCE_INLINE JsonObjectSubscript<const String&> operator[](const String& key);
|
---|
61 |
|
---|
62 | // Gets the value associated with the specified key.
|
---|
63 | FORCE_INLINE JsonVariant operator[](JsonObjectKey key) const;
|
---|
64 |
|
---|
65 | // Sets the specified key with the specified value.
|
---|
66 | // bool set(TKey key, bool value);
|
---|
67 | // bool set(TKey key, char value);
|
---|
68 | // bool set(TKey key, long value);
|
---|
69 | // bool set(TKey key, int value);
|
---|
70 | // bool set(TKey key, short value);
|
---|
71 | // bool set(TKey key, float value);
|
---|
72 | // bool set(TKey key, double value);
|
---|
73 | // bool set(TKey key, const char* value);
|
---|
74 | template <typename T>
|
---|
75 | FORCE_INLINE bool set(
|
---|
76 | JsonObjectKey key, T value,
|
---|
77 | typename TypeTraits::EnableIf<
|
---|
78 | CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type* = 0) {
|
---|
79 | return setNodeAt<T>(key, value);
|
---|
80 | }
|
---|
81 | // bool set(Key, String&);
|
---|
82 | // bool set(Key, JsonArray&);
|
---|
83 | // bool set(Key, JsonObject&);
|
---|
84 | // bool set(Key, JsonVariant&);
|
---|
85 | template <typename T>
|
---|
86 | FORCE_INLINE bool set(
|
---|
87 | JsonObjectKey key, const T& value,
|
---|
88 | typename TypeTraits::EnableIf<CanSet<T&>::value>::type* = 0) {
|
---|
89 | return setNodeAt<T&>(key, const_cast<T&>(value));
|
---|
90 | }
|
---|
91 | // bool set(Key, float value, uint8_t decimals);
|
---|
92 | // bool set(Key, double value, uint8_t decimals);
|
---|
93 | template <typename TValue>
|
---|
94 | FORCE_INLINE bool set(
|
---|
95 | JsonObjectKey key, TValue value, uint8_t decimals,
|
---|
96 | typename TypeTraits::EnableIf<
|
---|
97 | TypeTraits::IsFloatingPoint<TValue>::value>::type* = 0) {
|
---|
98 | return setNodeAt<const JsonVariant&>(key, JsonVariant(value, decimals));
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Gets the value associated with the specified key.
|
---|
102 | FORCE_INLINE JsonVariant get(JsonObjectKey) const;
|
---|
103 |
|
---|
104 | // Gets the value associated with the specified key.
|
---|
105 | template <typename T>
|
---|
106 | FORCE_INLINE T get(JsonObjectKey) const;
|
---|
107 |
|
---|
108 | // Checks the type of the value associated with the specified key.
|
---|
109 | template <typename T>
|
---|
110 | FORCE_INLINE bool is(JsonObjectKey) const;
|
---|
111 |
|
---|
112 | // Creates and adds a JsonArray.
|
---|
113 | // This is a shortcut for JsonBuffer::createArray() and JsonObject::add().
|
---|
114 | FORCE_INLINE JsonArray& createNestedArray(JsonObjectKey key);
|
---|
115 |
|
---|
116 | // Creates and adds a JsonObject.
|
---|
117 | // This is a shortcut for JsonBuffer::createObject() and JsonObject::add().
|
---|
118 | FORCE_INLINE JsonObject& createNestedObject(JsonObjectKey key);
|
---|
119 |
|
---|
120 | // Tells weither the specified key is present and associated with a value.
|
---|
121 | FORCE_INLINE bool containsKey(JsonObjectKey key) const;
|
---|
122 |
|
---|
123 | // Removes the specified key and the associated value.
|
---|
124 | void remove(JsonObjectKey key);
|
---|
125 |
|
---|
126 | // Returns a reference an invalid JsonObject.
|
---|
127 | // This object is meant to replace a NULL pointer.
|
---|
128 | // This is used when memory allocation or JSON parsing fail.
|
---|
129 | static JsonObject& invalid() { return _invalid; }
|
---|
130 |
|
---|
131 | // Serialize the object to the specified JsonWriter
|
---|
132 | void writeTo(Internals::JsonWriter& writer) const;
|
---|
133 |
|
---|
134 | private:
|
---|
135 | // Returns the list node that matches the specified key.
|
---|
136 | node_type* getNodeAt(const char* key) const;
|
---|
137 |
|
---|
138 | node_type* getOrCreateNodeAt(const char* key);
|
---|
139 |
|
---|
140 | template <typename T>
|
---|
141 | FORCE_INLINE bool setNodeAt(JsonObjectKey key, T value);
|
---|
142 |
|
---|
143 | FORCE_INLINE bool setNodeKey(node_type*, JsonObjectKey key);
|
---|
144 |
|
---|
145 | template <typename T>
|
---|
146 | FORCE_INLINE bool setNodeValue(node_type*, T value);
|
---|
147 |
|
---|
148 | // The instance returned by JsonObject::invalid()
|
---|
149 | static JsonObject _invalid;
|
---|
150 | };
|
---|
151 | }
|
---|
152 |
|
---|
153 | #include "JsonObject.ipp"
|
---|