diff --git a/src/Firebase.cpp b/src/Firebase.cpp index 701fa0dd..26f63c51 100644 --- a/src/Firebase.cpp +++ b/src/Firebase.cpp @@ -38,6 +38,22 @@ String makeFirebaseURL(const String& path, const String& auth) { return url; } +template +T decodeJsonValue(JsonBuffer *buf, const String& json) { + return ArduinoJson::Internals::parse(json.c_str()); +} + +template<> +String decodeJsonValue(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(fakeArray.c_str())); + return arr[0]; +} + } // namespace Firebase::Firebase(const String& host) : host_(host) { @@ -132,16 +148,33 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth, : FirebaseCall(host, auth, "GET", path, "", http) { } +bool FirebaseGet::readBool() { + return decodeJsonValue(&buffer_, response_); +} + +int FirebaseGet::readInt() { + return decodeJsonValue(&buffer_, response_); +} + +float FirebaseGet::readFloat() { + return decodeJsonValue(&buffer_, response_); +} + +double FirebaseGet::readDouble() { + return decodeJsonValue(&buffer_, response_); +} + +String FirebaseGet::readString() { + return decodeJsonValue(&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, diff --git a/src/Firebase.h b/src/Firebase.h index 92ddea86..3750b727 100644 --- a/src/Firebase.h +++ b/src/Firebase.h @@ -102,8 +102,11 @@ 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 { @@ -111,9 +114,6 @@ class FirebaseSet: public FirebaseCall { FirebaseSet() {} FirebaseSet(const String& host, const String& auth, const String& path, const String& value, HTTPClient* http = NULL); - - private: - String json_; }; class FirebasePush : public FirebaseCall {