Made toXml and toJson non-member function.

pull/2/head
Tatsuhiro Tsujikawa 2011-10-18 00:55:58 +09:00
parent e700d2bb1f
commit 4f0a45abe2
4 changed files with 23 additions and 19 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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<RpcResponse>& results,
const std::string& callback,

View File

@ -31,13 +31,13 @@ void RpcResponseTest::testToJson()
SharedHandle<String> 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("<?xml version=\"1.0\"?>"
"<methodResponse>"