JSON: Switch from StyledWriter to StreamWriterBuilder master
authorChris Tallon <chris@vomp.tv>
Thu, 26 May 2022 15:41:10 +0000 (16:41 +0100)
committerChris Tallon <chris@vomp.tv>
Thu, 26 May 2022 15:41:10 +0000 (16:41 +0100)
httpdclient.c
vdrclient.c
vdrclient.h

index e9800710ceec0e12399f23df78b5e2055ba5cbb1..9bfe33f3791d42c2d14230cec3bd2fcbf1ca81ec 100644 (file)
@@ -6,6 +6,8 @@
 #include <fcntl.h>
 
 #include <memory>
+#include <string>
+#include <sstream>
 
 #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);
 
index 8a1d8b1c1b66d5c1819308c1adc7015d292f6884..669d3b91e7f5c870707057f469b6b6ed94699c54 100644 (file)
@@ -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;
 }
 
index 926f107b28a84466b4a48e43b77359350f471fa7..68e6f1cf871cf14475bb67c9f0b4eba57eec8fad 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef VDRCLIENT_H
 #define VDRCLIENT_H
 
+#include <sstream>
+
 #include <jsoncpp/json/json.h>
 
 // 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<Json::StreamWriter> jsonwriter;
 };
 
 #endif