From 4f0a45abe23f03ec59529a0a4a35878b743aa4dc Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 18 Oct 2011 00:55:58 +0900 Subject: [PATCH] Made toXml and toJson non-member function. --- src/HttpServerBodyCommand.cc | 4 ++-- src/RpcResponse.cc | 13 +++++++------ src/RpcResponse.h | 15 +++++++++------ test/RpcResponseTest.cc | 10 +++++----- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index 30fd14dc..6f6f02bb 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -113,7 +113,7 @@ void HttpServerBodyCommand::sendJsonRpcResponse const std::string& callback) { bool gzip = httpServer_->supportsGZip(); - std::string responseData = res.toJson(callback, gzip); + std::string responseData = rpc::toJson(res, callback, gzip); if(res.code == 0) { httpServer_->feedResponse(responseData, getJsonRpcContentType(!callback.empty())); @@ -220,7 +220,7 @@ bool HttpServerBodyCommand::execute() A2_LOG_INFO(fmt("Executing RPC method %s", req.methodName.c_str())); rpc::RpcResponse res = method->execute(req, e_); bool gzip = httpServer_->supportsGZip(); - std::string responseData = res.toXml(gzip); + std::string responseData = rpc::toXml(res, gzip); httpServer_->feedResponse(responseData, "text/xml"); addHttpServerResponseCommand(); #endif // ENABLE_XML_RPC diff --git a/src/RpcResponse.cc b/src/RpcResponse.cc index e2605c28..ca7107d9 100644 --- a/src/RpcResponse.cc +++ b/src/RpcResponse.cc @@ -145,19 +145,19 @@ RpcResponse& RpcResponse::operator=(const RpcResponse& c) return *this; } -std::string RpcResponse::toXml(bool gzip) const +std::string toXml(const RpcResponse& res, bool gzip) { if(gzip) { #ifdef HAVE_ZLIB GZipEncoder o; o.init(); - return encodeAll(o, code, param); + return encodeAll(o, res.code, res.param); #else // !HAVE_ZLIB abort(); #endif // !HAVE_ZLIB } else { std::stringstream o; - return encodeAll(o, code, param); + return encodeAll(o, res.code, res.param); } } @@ -189,19 +189,20 @@ OutputStream& encodeJsonAll } } // namespace -std::string RpcResponse::toJson(const std::string& callback, bool gzip) const +std::string toJson +(const RpcResponse& res, const std::string& callback, bool gzip) { if(gzip) { #ifdef HAVE_ZLIB GZipEncoder o; o.init(); - return encodeJsonAll(o, code, param, id, callback).str(); + return encodeJsonAll(o, res.code, res.param, res.id, callback).str(); #else // !HAVE_ZLIB abort(); #endif // !HAVE_ZLIB } else { std::stringstream o; - return encodeJsonAll(o, code, param, id, callback).str(); + return encodeJsonAll(o, res.code, res.param, res.id, callback).str(); } } diff --git a/src/RpcResponse.h b/src/RpcResponse.h index 76fd0184..7a305f54 100644 --- a/src/RpcResponse.h +++ b/src/RpcResponse.h @@ -64,14 +64,17 @@ struct RpcResponse { ~RpcResponse(); RpcResponse& operator=(const RpcResponse& c); - - std::string toXml(bool gzip = false) const; - - // Encodes RPC response in JSON. If callback is not empty, the - // resulting string is JSONP. - std::string toJson(const std::string& callback, bool gzip = false) const; }; +std::string toXml(const RpcResponse& response, bool gzip = false); + +// Encodes RPC response in JSON. If callback is not empty, the +// resulting string is JSONP. +std::string toJson +(const RpcResponse& response, + const std::string& callback, + bool gzip = false); + std::string toJsonBatch (const std::vector& results, const std::string& callback, diff --git a/test/RpcResponseTest.cc b/test/RpcResponseTest.cc index 68b9e90e..9dadecff 100644 --- a/test/RpcResponseTest.cc +++ b/test/RpcResponseTest.cc @@ -31,13 +31,13 @@ void RpcResponseTest::testToJson() SharedHandle id = String::g("9"); RpcResponse res(0, param, id); results.push_back(res); - std::string s = res.toJson("", false); + std::string s = toJson(res, "", false); CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\"," "\"jsonrpc\":\"2.0\"," "\"result\":[1]}"), s); // with callback - s = res.toJson("cb", false); + s = toJson(res, "cb", false); CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\"," "\"jsonrpc\":\"2.0\"," "\"result\":[1]})"), @@ -50,7 +50,7 @@ void RpcResponseTest::testToJson() param->put("message", "HELLO ERROR"); RpcResponse res(1, param, Null::g()); results.push_back(res); - std::string s = res.toJson("", false); + std::string s = toJson(res, "", false); CPPUNIT_ASSERT_EQUAL(std::string("{\"error\":{\"code\":1," "\"message\":\"HELLO ERROR\"}," "\"id\":null," @@ -58,7 +58,7 @@ void RpcResponseTest::testToJson() "}"), s); // with callback - s = res.toJson("cb", false); + s = toJson(res, "cb", false); CPPUNIT_ASSERT_EQUAL(std::string("cb({\"error\":{\"code\":1," "\"message\":\"HELLO ERROR\"}," "\"id\":null," @@ -103,7 +103,7 @@ void RpcResponseTest::testToXml() param->put("faultCode", Integer::g(1)); param->put("faultString", "No such method: make.hamburger"); RpcResponse res(1, param, Null::g()); - std::string s = res.toXml(false); + std::string s = toXml(res, false); CPPUNIT_ASSERT_EQUAL (std::string("" ""