Add a method to disable unicode escaping in json string (#491)

This commit is contained in:
An Tao 2020-06-24 08:11:32 +08:00 committed by GitHub
parent ccd51d289e
commit 8f6269b208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<HttpRequestImpl>(nullptr);
req->setMethod(drogon::Get);

View File

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