From 7c310bac7294973f483b42710441ee141d7ec36a Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Thu, 26 May 2022 16:41:10 +0100 Subject: [PATCH] JSON: Switch from StyledWriter to StreamWriterBuilder --- httpdclient.c | 10 +++++++--- vdrclient.c | 12 ++++++++---- vdrclient.h | 6 +++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/httpdclient.c b/httpdclient.c index e980071..9bfe33f 100644 --- a/httpdclient.c +++ b/httpdclient.c @@ -6,6 +6,8 @@ #include #include +#include +#include #include "httpdclient.h" @@ -281,12 +283,14 @@ MHD_Result HTTPDClient::processPOST() if (strcmp(url, "/jsonserver")) return sendStockResponse(404); if (getVars["req"].empty()) return sendStockResponse(400); - std::string returnData; + std::stringstream returnData; - bool success = vdrclient.process(getVars["req"], postFields, returnData); + bool success = vdrclient.process(getVars["req"], postFields, &returnData); if (!success) return sendStockResponse(500); - struct MHD_Response* response = MHD_create_response_from_buffer(strlen(returnData.c_str()), (void *)returnData.c_str(), MHD_RESPMEM_MUST_COPY); + std::string returnDataStr = returnData.str(); + + struct MHD_Response* response = MHD_create_response_from_buffer(strlen(returnDataStr.c_str()), (void *)returnDataStr.c_str(), MHD_RESPMEM_MUST_COPY); MHD_add_response_header(response, "Content-Type", "application/json"); MHD_Result ret = MHD_queue_response(mhd_connection, MHD_HTTP_OK, response); diff --git a/vdrclient.c b/vdrclient.c index 8a1d8b1..669d3b9 100644 --- a/vdrclient.c +++ b/vdrclient.c @@ -30,6 +30,11 @@ VDRClient::VDRClient(const std::string& _configDir) : configDir(_configDir) { logger = spd::get("jsonserver_spdlog"); + + Json::StreamWriterBuilder jsonWriterBuilder; + // jsonWriterBuilder["commentStyle"] = "None"; + // jsonWriterBuilder["indentation"] = " "; + jsonwriter.reset(jsonWriterBuilder.newStreamWriter()); } VDRClient::~VDRClient() @@ -37,7 +42,7 @@ VDRClient::~VDRClient() logger->debug("VDRClient destructor"); } -bool VDRClient::process(std::string& request, PFMap& postFields, std::string& returnString) +bool VDRClient::process(std::string& request, PFMap& postFields, std::stringstream* returnStringStream) { Json::Value returnJSON; bool success = false; @@ -83,9 +88,8 @@ bool VDRClient::process(std::string& request, PFMap& postFields, std::string& re if (!success) return false; - Json::StyledWriter sw; - returnString = sw.write(returnJSON); - logger->debug("Done sw write"); + jsonwriter->write(returnJSON, returnStringStream); + logger->debug("Done jsonwriter write"); return true; } diff --git a/vdrclient.h b/vdrclient.h index 926f107..68e6f1c 100644 --- a/vdrclient.h +++ b/vdrclient.h @@ -1,6 +1,8 @@ #ifndef VDRCLIENT_H #define VDRCLIENT_H +#include + #include // Log docs: https://github.com/gabime/spdlog @@ -25,7 +27,7 @@ class VDRClient VDRClient(const std::string& configDir); ~VDRClient(); - bool process(std::string& request, PFMap& postFields, std::string& returnData); + bool process(std::string& request, PFMap& postFields, std::stringstream* returnData); private: const std::string& configDir; @@ -86,6 +88,8 @@ class VDRClient } */ }; + + std::shared_ptr jsonwriter; }; #endif -- 2.39.2