waa*_*919 3 c++ json poco-libraries
我JSON使用这样的POCO库生成一个文件:
void writeToFile()
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object;
json->set("name", "foo");
json->set("address", "bar");
std::ostringstream oss;
Poco::JSON::Stringifier::stringify(json, oss);
std::ofstream ofs("output.json");
if (ofs.is_open() == true)
{
ofs << oss.str();
ofs.close();
}
}
Run Code Online (Sandbox Code Playgroud)
其中output.json包含:
{"name":"foo","address":"bar"}
Run Code Online (Sandbox Code Playgroud)
有什么办法POCO可以美化一个JSON?
所以输出会是这样的:
{
"name" : "foo",
"address" : "bar"
}
Run Code Online (Sandbox Code Playgroud)
As @Dmitry said on the comments, the parameters on the stringify() method would do:
static void stringify(
const Dynamic::Var & any,
std::ostream & out,
unsigned int indent = 0,
int step = - 1,
int options = Poco::JSON_WRAP_STRINGS
);
Run Code Online (Sandbox Code Playgroud)
Example:
Poco::JSON::Stringifier::stringify(json, oss, 4, -1, Poco::JSON_PRESERVE_KEY_ORDER);
Run Code Online (Sandbox Code Playgroud)