From 652560697d589965e03145bafd2251410da558b3 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Mon, 21 May 2018 18:54:00 +0800 Subject: [PATCH] add json Response --- examples/CMakeLists.txt | 2 +- .../static_link_example/JsonTestController.cc | 19 +++++++++++++++++++ .../static_link_example/JsonTestController.h | 15 +++++++++++++++ lib/inc/drogon/HttpResponse.h | 3 +++ lib/src/HttpResponseImpl.cc | 12 ++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 examples/static_link_example/JsonTestController.cc create mode 100755 examples/static_link_example/JsonTestController.h diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8081a21e..01e28c40 100755 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,3 +1,3 @@ -link_libraries(drogon trantor uuid pthread) +link_libraries(drogon trantor uuid pthread jsoncpp) AUX_SOURCE_DIRECTORY(static_link_example DIR_STATIC) add_executable(webapp ${DIR_STATIC}) diff --git a/examples/static_link_example/JsonTestController.cc b/examples/static_link_example/JsonTestController.cc new file mode 100755 index 00000000..b310ff59 --- /dev/null +++ b/examples/static_link_example/JsonTestController.cc @@ -0,0 +1,19 @@ +#include "JsonTestController.h" +#include +void JsonTestController::asyncHandleHttpRequest(const HttpRequest& req,std::functioncallback) +{ + Json::Value json; + json["path"]="json"; + json["name"]="json test"; + Json::Value array; + for(int i=0;i<5;i++) + { + Json::Value user; + user["id"]=i; + user["name"]="none"; + array.append(user); + } + json["rows"]=array; + auto resp=std::unique_ptr(HttpResponse::newHttpResponse(json)); + callback(*resp); +} \ No newline at end of file diff --git a/examples/static_link_example/JsonTestController.h b/examples/static_link_example/JsonTestController.h new file mode 100755 index 00000000..aedc9506 --- /dev/null +++ b/examples/static_link_example/JsonTestController.h @@ -0,0 +1,15 @@ +#pragma once + +#include +using namespace drogon; + +class JsonTestController:public drogon::HttpSimpleController +{ +public: + //TestController(){} + virtual void asyncHandleHttpRequest(const HttpRequest& req,std::functioncallback)override; + + PATH_LIST_BEGIN + PATH_ADD("/json"); + PATH_LIST_END +}; \ No newline at end of file diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index 3ada1d5b..02d45263 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -21,6 +21,8 @@ #pragma once #include +#include + using std::string; #define CT_APPLICATION_JSON 1 #define CT_TEXT_PLAIN 2 @@ -98,6 +100,7 @@ namespace drogon virtual std::string getBody() const=0; static HttpResponse* newHttpResponse(); + static HttpResponse* newHttpResponse(const Json::Value &data); }; diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 085641d6..e76eff92 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -26,6 +26,18 @@ HttpResponse* HttpResponse::newHttpResponse() { return new HttpResponseImpl; } + +HttpResponse* HttpResponse::newHttpResponse(const Json::Value &data) +{ + auto res=new HttpResponseImpl; + res->setStatusCode(HttpResponse::k200Ok); + res->setContentTypeCode(CT_APPLICATION_JSON); + Json::StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = ""; + res->setBody(writeString(builder, data)); + return res; +} const std::string HttpResponseImpl::web_content_type_to_string(uint8_t contenttype) { switch(contenttype) {