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

Commit 67523bd

Browse files
committed
json: switch json() to readJson()
- pass the response as a mutable buffer to get rid of the leaking strdup(). - rename the function to make it more obvious it's only meant to be called once. - delete obsolete json_ field. - make response_ and other FirebaseCall member private.
1 parent 6f1ec7d commit 67523bd

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

src/Firebase.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
118118
}
119119
}
120120

121-
const JsonObject& FirebaseCall::json() {
122-
//TODO(edcoyne): This is not efficient, we should do something smarter with
121+
JsonObject& FirebaseCall::parseJson() {
122+
// TODO(edcoyne): This is not efficient, we should do something smarter with
123123
//the buffers.
124124
buffer_ = DynamicJsonBuffer();
125-
return buffer_.parseObject(response());
125+
// NOTE(proppy): this effectively void the response_ buffer.
126+
return buffer_.parseObject(const_cast<char*>(response_.c_str()));
126127
}
127128

128129
// FirebaseGet
@@ -132,23 +133,24 @@ FirebaseGet::FirebaseGet(const String& host, const String& auth,
132133
: FirebaseCall(host, auth, "GET", path, "", http) {
133134
}
134135

136+
JsonObject& FirebaseGet::readJson() {
137+
return parseJson();
138+
}
139+
140+
135141
// FirebaseSet
136142
FirebaseSet::FirebaseSet(const String& host, const String& auth,
137143
const String& path, const String& value,
138144
HTTPClient* http)
139145
: FirebaseCall(host, auth, "PUT", path, value, http) {
140-
if (!error()) {
141-
// TODO: parse json
142-
json_ = response();
143-
}
144146
}
145147
// FirebasePush
146148
FirebasePush::FirebasePush(const String& host, const String& auth,
147149
const String& path, const String& value,
148150
HTTPClient* http)
149151
: FirebaseCall(host, auth, "POST", path, value, http) {
150152
if (!error()) {
151-
name_ = json()["name"].as<const char*>();
153+
name_ = parseJson()["name"].as<const char*>();;
152154
}
153155
}
154156

src/Firebase.h

+7-17
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class FirebaseError {
6363
public:
6464
FirebaseError() {}
6565
FirebaseError(int code, const String& message) : code_(code), message_(message) {
66-
}
66+
}
6767
operator bool() const { return code_ != 0; }
6868
int code() const { return code_; }
6969
const String& message() const { return message_; }
70-
private:
70+
private:
7171
int code_ = 0;
7272
String message_ = "";
7373
};
@@ -77,20 +77,15 @@ class FirebaseCall {
7777
FirebaseCall() {}
7878
FirebaseCall(const String& host, const String& auth,
7979
const char* method, const String& path,
80-
const String& data = "",
80+
const String& data = "",
8181
HTTPClient* http = NULL);
8282
const FirebaseError& error() const {
8383
return error_;
8484
}
85-
86-
const String& response() {
87-
return response_;
88-
}
89-
90-
const JsonObject& json();
91-
9285
protected:
86+
JsonObject& parseJson();
9387
HTTPClient* http_;
88+
private:
9489
FirebaseError error_;
9590
String response_;
9691
DynamicJsonBuffer buffer_;
@@ -101,19 +96,14 @@ class FirebaseGet : public FirebaseCall {
10196
FirebaseGet() {}
10297
FirebaseGet(const String& host, const String& auth,
10398
const String& path, HTTPClient* http = NULL);
104-
105-
private:
106-
String json_;
99+
JsonObject& readJson();
107100
};
108101

109102
class FirebaseSet: public FirebaseCall {
110103
public:
111104
FirebaseSet() {}
112105
FirebaseSet(const String& host, const String& auth,
113106
const String& path, const String& value, HTTPClient* http = NULL);
114-
115-
private:
116-
String json_;
117107
};
118108

119109
class FirebasePush : public FirebaseCall {
@@ -155,7 +145,7 @@ class FirebaseStream : public FirebaseCall {
155145
};
156146

157147
// Read next json encoded `event` from stream.
158-
Event read(String& event);
148+
Event read(String& event);
159149

160150
const FirebaseError& error() const {
161151
return _error;

0 commit comments

Comments
 (0)