Skip to content

Commit 32d96b9

Browse files
committed
add typed get
See FirebaseExtended#60.
1 parent 6f1ec7d commit 32d96b9

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

Diff for: src/Firebase.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515
//
1616
#include "Firebase.h"
17+
#include "third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonParser.hpp"
1718

1819
// Detect whether stable version of HTTP library is installed instead of
1920
// master branch and patch in missing status and methods.
@@ -38,6 +39,22 @@ String makeFirebaseURL(const String& path, const String& auth) {
3839
return url;
3940
}
4041

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+
4158
} // namespace
4259

4360
Firebase::Firebase(const String& host) : host_(host) {
@@ -132,16 +149,33 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth,
132149
: FirebaseCall(host, auth, "GET", path, "", http) {
133150
}
134151

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+
135172
// FirebaseSet
136173
FirebaseSet::FirebaseSet(const String& host, const String& auth,
137174
const String& path, const String& value,
138175
HTTPClient* http)
139176
: FirebaseCall(host, auth, "PUT", path, value, http) {
140-
if (!error()) {
141-
// TODO: parse json
142-
json_ = response();
143-
}
144177
}
178+
145179
// FirebasePush
146180
FirebasePush::FirebasePush(const String& host, const String& auth,
147181
const String& path, const String& value,

Diff for: src/Firebase.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ class FirebaseGet : public FirebaseCall {
102102
FirebaseGet(const String& host, const String& auth,
103103
const String& path, HTTPClient* http = NULL);
104104

105-
private:
106-
String json_;
105+
bool readBool();
106+
int readInt();
107+
float readFloat();
108+
double readDouble();
109+
String readString();
107110
};
108111

109112
class FirebaseSet: public FirebaseCall {
110113
public:
111114
FirebaseSet() {}
112115
FirebaseSet(const String& host, const String& auth,
113116
const String& path, const String& value, HTTPClient* http = NULL);
114-
115-
private:
116-
String json_;
117117
};
118118

119119
class FirebasePush : public FirebaseCall {

0 commit comments

Comments
 (0)