Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

add typed get #81

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ String makeFirebaseURL(const String& path, const String& auth) {
return url;
}

template<typename T>
T decodeJsonValue(JsonBuffer *buf, const String& json) {
return ArduinoJson::Internals::parse<T>(json.c_str());
}

template<>
String decodeJsonValue<String>(JsonBuffer *buf, const String& json) {
// ugly workaround because ArduinoJson doesn't expose a way to
// decode json string literals.
String fakeArray("[");
fakeArray+=json;
fakeArray+="]";
JsonArray& arr = buf->parseArray(const_cast<char*>(fakeArray.c_str()));
return arr[0];
}

} // namespace

Firebase::Firebase(const String& host) : host_(host) {
Expand Down Expand Up @@ -132,16 +148,33 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth,
: FirebaseCall(host, auth, "GET", path, "", http) {
}

bool FirebaseGet::readBool() {
return decodeJsonValue<bool>(&buffer_, response_);
}

int FirebaseGet::readInt() {
return decodeJsonValue<int>(&buffer_, response_);
}

float FirebaseGet::readFloat() {
return decodeJsonValue<float>(&buffer_, response_);
}

double FirebaseGet::readDouble() {
return decodeJsonValue<double>(&buffer_, response_);
}

String FirebaseGet::readString() {
return decodeJsonValue<String>(&buffer_, response_);
}

// FirebaseSet
FirebaseSet::FirebaseSet(const String& host, const String& auth,
const String& path, const String& value,
HTTPClient* http)
: FirebaseCall(host, auth, "PUT", path, value, http) {
if (!error()) {
// TODO: parse json
json_ = response();
}
}

// FirebasePush
FirebasePush::FirebasePush(const String& host, const String& auth,
const String& path, const String& value,
Expand Down
10 changes: 5 additions & 5 deletions src/Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ class FirebaseGet : public FirebaseCall {
FirebaseGet(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);

private:
String json_;
bool readBool();
int readInt();
float readFloat();
double readDouble();
String readString();
};

class FirebaseSet: public FirebaseCall {
public:
FirebaseSet() {}
FirebaseSet(const String& host, const String& auth,
const String& path, const String& value, HTTPClient* http = NULL);

private:
String json_;
};

class FirebasePush : public FirebaseCall {
Expand Down