From 8f6269b2084b015852355e429d533febdf46086b Mon Sep 17 00:00:00 2001 From: An Tao Date: Wed, 24 Jun 2020 08:11:32 +0800 Subject: [PATCH] Add a method to disable unicode escaping in json string (#491) --- config.example.json | 2 ++ drogon_ctl/templates/config.csp | 2 ++ examples/simple_example/JsonTestController.cc | 1 + lib/inc/drogon/HttpAppFramework.h | 16 ++++++++++++++++ lib/src/ConfigLoader.cc | 3 +++ lib/src/HttpAppFrameworkImpl.h | 13 +++++++++++++ lib/src/HttpRequestImpl.cc | 4 ++++ lib/src/HttpResponseImpl.cc | 4 ++++ 8 files changed, 45 insertions(+) diff --git a/config.example.json b/config.example.json index 620af1d5..dd2dd8b2 100644 --- a/config.example.json +++ b/config.example.json @@ -136,6 +136,8 @@ //files is the path where the csp files locate. If the path isn't prefixed with /, it is relative //path of the current working directory. "dynamic_views_output_path": "", + //enable_unicode_escaping_in_json: true by default, enable unicode escaping in json. + "enable_unicode_escaping_in_json": true, //log: Set log output, drogon output logs to stdout by default "log": { //log_path: Log file path,empty by default,in which case,logs are output to the stdout diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index 0f6649ca..b788d57c 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -136,6 +136,8 @@ //files is the path where the csp files locate. If the path isn't prefixed with /, it is relative //path of the current working directory. "dynamic_views_output_path": "", + //enable_unicode_escaping_in_json: true by default, enable unicode escaping in json. + "enable_unicode_escaping_in_json": true, //log: Set log output, drogon output logs to stdout by default "log": { //log_path: Log file path,empty by default,in which case,logs are output to the stdout diff --git a/examples/simple_example/JsonTestController.cc b/examples/simple_example/JsonTestController.cc index 3b1dd898..bb5ddc27 100644 --- a/examples/simple_example/JsonTestController.cc +++ b/examples/simple_example/JsonTestController.cc @@ -13,6 +13,7 @@ void JsonTestController::asyncHandleHttpRequest( Json::Value user; user["id"] = i; user["name"] = "none"; + user["c_name"] = "张三"; array.append(user); } json["rows"] = array; diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 066a8660..9b2c4bda 100644 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -1058,12 +1058,28 @@ class HttpAppFramework : public trantor::NonCopyable */ virtual orm::DbClientPtr getFastDbClient( const std::string &name = "default") = 0; + /** * @brief Check if all database clients in the framework are available * (connect to the database successfully). */ virtual bool areAllDbClientsAvailable() const noexcept = 0; + /** + * @brief This method is to enable or disable the unicode escaping (\u) in + * the json string of HTTP responses or requests. it works (disable + * successfully) when the version of JsonCpp >= 1.9.3, the unicode escaping + * is enabled by default. + */ + virtual HttpAppFramework &setUnicodeEscapingInJson( + bool enable) noexcept = 0; + + /** + * @brief Check if the unicode escaping is used in the json string of HTTP + * requests and responses. + */ + virtual bool isUnicodeEscapingUsedInJson() const noexcept = 0; + /// Create a database client /** * @param dbType The database type is one of diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index fdbf48b4..40e051c9 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -367,6 +367,9 @@ static void loadApp(const Json::Value &app) } } #endif + auto unicodeEscaping = + app.get("enable_unicode_escaping_in_json", true).asBool(); + drogon::app().setUnicodeEscapingInJson(unicodeEscaping); // log loadLogSetting(app["log"]); // run as daemon diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 7eb44976..e6b2e1a7 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -373,6 +373,18 @@ class HttpAppFrameworkImpl : public HttpAppFramework return running_; } + virtual HttpAppFramework &setUnicodeEscapingInJson( + bool enable) noexcept override + { + usingUnicodeEscaping_ = enable; + return *this; + } + + virtual bool isUnicodeEscapingUsedInJson() const noexcept override + { + return usingUnicodeEscaping_; + } + virtual trantor::EventLoop *getLoop() const override; virtual trantor::EventLoop *getIOLoop(size_t id) const override; @@ -543,6 +555,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework bool useSendfile_{true}; bool useGzip_{true}; bool useBrotli_{false}; + bool usingUnicodeEscaping_{true}; size_t clientMaxBodySize_{1024 * 1024}; size_t clientMaxMemoryBodySize_{64 * 1024}; size_t clientMaxWebSocketMessageSize_{128 * 1024}; diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 197d68c1..f13ee255 100644 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -465,6 +465,10 @@ HttpRequestPtr HttpRequest::newHttpJsonRequest(const Json::Value &data) std::call_once(once, []() { builder["commentStyle"] = "None"; builder["indentation"] = ""; + if (!app().isUnicodeEscapingUsedInJson()) + { + builder["emitUTF8"] = true; + } }); auto req = std::make_shared(nullptr); req->setMethod(drogon::Get); diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 367c98fc..740ce322 100644 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -91,6 +91,10 @@ void HttpResponseImpl::generateBodyFromJson() std::call_once(once, []() { builder["commentStyle"] = "None"; builder["indentation"] = ""; + if (!app().isUnicodeEscapingUsedInJson()) + { + builder["emitUTF8"] = true; + } }); setBody(writeString(builder, *jsonPtr_)); }