#include <fcntl.h>
#include <memory>
+#include <string>
+#include <sstream>
#include "httpdclient.h"
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);
: configDir(_configDir)
{
logger = spd::get("jsonserver_spdlog");
+
+ Json::StreamWriterBuilder jsonWriterBuilder;
+ // jsonWriterBuilder["commentStyle"] = "None";
+ // jsonWriterBuilder["indentation"] = " ";
+ jsonwriter.reset(jsonWriterBuilder.newStreamWriter());
}
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;
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;
}
#ifndef VDRCLIENT_H
#define VDRCLIENT_H
+#include <sstream>
+
#include <jsoncpp/json/json.h>
// Log docs: https://github.com/gabime/spdlog
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;
}
*/
};
+
+ std::shared_ptr<Json::StreamWriter> jsonwriter;
};
#endif