14
14
// limitations under the License.
15
15
//
16
16
#include " Firebase.h"
17
+ #include " third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonParser.hpp"
17
18
18
19
// Detect whether stable version of HTTP library is installed instead of
19
20
// master branch and patch in missing status and methods.
@@ -38,6 +39,22 @@ String makeFirebaseURL(const String& path, const String& auth) {
38
39
return url;
39
40
}
40
41
42
+ template <typename T>
43
+ T decodeJsonValue (JsonBuffer *buf, const String& json) {
44
+ return ArduinoJson::Internals::parse<T>(json.c_str ());
45
+ }
46
+
47
+ template <>
48
+ String decodeJsonValue<String>(JsonBuffer *buf, const String& json) {
49
+ // ugly workaround because ArduinoJson doesn't expose a way to
50
+ // decode json string literals.
51
+ String fakeArray (" [" );
52
+ fakeArray+=json;
53
+ fakeArray+=" ]" ;
54
+ JsonArray& arr = buf->parseArray (const_cast <char *>(fakeArray.c_str ()));
55
+ return arr[0 ];
56
+ }
57
+
41
58
} // namespace
42
59
43
60
Firebase::Firebase (const String& host) : host_(host) {
@@ -132,16 +149,33 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth,
132
149
: FirebaseCall(host, auth, " GET" , path, " " , http) {
133
150
}
134
151
152
+ bool FirebaseGet::readBool () {
153
+ return decodeJsonValue<bool >(&buffer_, response_);
154
+ }
155
+
156
+ int FirebaseGet::readInt () {
157
+ return decodeJsonValue<int >(&buffer_, response_);
158
+ }
159
+
160
+ float FirebaseGet::readFloat () {
161
+ return decodeJsonValue<float >(&buffer_, response_);
162
+ }
163
+
164
+ double FirebaseGet::readDouble () {
165
+ return decodeJsonValue<double >(&buffer_, response_);
166
+ }
167
+
168
+ String FirebaseGet::readString () {
169
+ return decodeJsonValue<String>(&buffer_, response_);
170
+ }
171
+
135
172
// FirebaseSet
136
173
FirebaseSet::FirebaseSet (const String& host, const String& auth,
137
174
const String& path, const String& value,
138
175
HTTPClient* http)
139
176
: FirebaseCall(host, auth, " PUT" , path, value, http) {
140
- if (!error ()) {
141
- // TODO: parse json
142
- json_ = response ();
143
- }
144
177
}
178
+
145
179
// FirebasePush
146
180
FirebasePush::FirebasePush (const String& host, const String& auth,
147
181
const String& path, const String& value,
0 commit comments