Question:
With below sample code, able to create a json string and print as below, my requirements is to print into a single line(below as well) with no space or tab, can i do it using boost or need to explicitly remove from string.
#include "boost/property_tree/json_parser.hpp"
#include <boost/property_tree/ptree.hpp>
#include <sstream>
#include <iostream>
int main()
{
boost::property_tree::ptree root;
root.put("sessionId", "123456");
root.put("applicationName", "Sample_Appliation");
root.put("applicationId", "null");
root.put("logMessage", "Sample Message");
root.put("loggingTime", "2020-03-17T13:26:45.013");
std::stringstream ss;
boost::property_tree::write_json(ss, root);
std::cout << ss.str() << std::endl;
return 0;
}
Output:
{
"sessionId": "123456",
"applicationName": "Sample_Appliation",
"applicationId": "null",
"logMessage": "Sample Message",
"loggingTime": "2020-03-17T13:26:45.013"
}
Expected:
{"sessionId": "123456","applicationName": "Sample_Appliation","applicationId": "null","logMessage": "Sample Message","loggingTime": "2020-03-17T13:26:45.013"}
There is a third parameter to boost::property_tree::write_json named pretty with default value true, just pass false as a third parameter to disable pretty printing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With