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

json: switch json() to readJson() #82

Closed
wants to merge 1 commit 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
18 changes: 10 additions & 8 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
}
}

const JsonObject& FirebaseCall::json() {
//TODO(edcoyne): This is not efficient, we should do something smarter with
JsonObject& FirebaseCall::parseJson() {
// TODO(edcoyne): This is not efficient, we should do something smarter with
//the buffers.
buffer_ = DynamicJsonBuffer();
return buffer_.parseObject(response());
// NOTE(proppy): this effectively void the response_ buffer.
return buffer_.parseObject(const_cast<char*>(response_.c_str()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While efficient I think we should just make a mutable copy of the response for now to pass in. This seems super "premature optimization" hack.

Copy link
Contributor Author

@proppy proppy Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe &response_[0] would be cleaner and get rid of the const cast. The issue with an intermediate copy is that JsonObject relies on the underlying string for the data storage, so we'd have to add a new member.

}

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

JsonObject& FirebaseGet::readJson() {
return parseJson();
}


// 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,
HTTPClient* http)
: FirebaseCall(host, auth, "POST", path, value, http) {
if (!error()) {
name_ = json()["name"].as<const char*>();
name_ = parseJson()["name"].as<const char*>();;
}
}

Expand Down
24 changes: 7 additions & 17 deletions src/Firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class FirebaseError {
public:
FirebaseError() {}
FirebaseError(int code, const String& message) : code_(code), message_(message) {
}
}
operator bool() const { return code_ != 0; }
int code() const { return code_; }
const String& message() const { return message_; }
private:
private:
int code_ = 0;
String message_ = "";
};
Expand All @@ -77,20 +77,15 @@ class FirebaseCall {
FirebaseCall() {}
FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
const String& data = "",
const String& data = "",
HTTPClient* http = NULL);
const FirebaseError& error() const {
return error_;
}

const String& response() {
return response_;
}

const JsonObject& json();

protected:
JsonObject& parseJson();
HTTPClient* http_;
private:
FirebaseError error_;
String response_;
DynamicJsonBuffer buffer_;
Expand All @@ -101,19 +96,14 @@ class FirebaseGet : public FirebaseCall {
FirebaseGet() {}
FirebaseGet(const String& host, const String& auth,
const String& path, HTTPClient* http = NULL);

private:
String json_;
JsonObject& readJson();
};

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 Expand Up @@ -155,7 +145,7 @@ class FirebaseStream : public FirebaseCall {
};

// Read next json encoded `event` from stream.
Event read(String& event);
Event read(String& event);

const FirebaseError& error() const {
return _error;
Expand Down