我想使用boost属性树write_json序列化,这样可以节省一切为字符串,这并不是说数据是错误的,但我需要每次都明确地投下他们,我想别的地方使用它们。 (如在Python或其它C ++ JSON(非升压)库)
这里是一些示例代码,我得到根据地区不同,什么:
boost::property_tree::ptree root, arr, elem1, elem2;
elem1.put<int>("key0", 0);
elem1.put<bool>("key1", true);
elem2.put<float>("key2", 2.2f);
elem2.put<double>("key3", 3.3);
arr.push_back( std::make_pair("", elem1) );
arr.push_back( std::make_pair("", elem2) );
root.put_child("path1.path2", arr);
std::stringstream ss;
write_json(ss, root);
std::string my_string_to_send_somewhare_else = ss.str();
和my_string_to_send_somewhere_else
是某事。 像这样:
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
反正是有其保存为值,比如: "key1" : true
或"key2" : 2.2
?
好吧,我已经解决了它这样的,(当然也不会适合每个人,因为它是一个黑客位的,需要进一步的工作)。
我写我自己write_json
功能(简单地复制文件, json_parser.hpp
和json_parser_write.hpp
到我的项目),并在修改以下行json_parser_write.hpp
:
- 注释行37 - 逃避报价'“
- 更改后的行76 -这样它不添加引号了:
stream << Ch('"') << data << Ch('"'); ==> stream << data;
stream << Ch('"') << data << Ch('"'); ==> stream << data;
然后,值将正确地保存,除了字符串,所以我写了自定义的翻译吧:
template <typename T>
struct my_id_translator
{
typedef T internal_type;
typedef T external_type;
boost::optional<T> get_value(const T &v) { return v.substr(1, v.size() - 2) ; }
boost::optional<T> put_value(const T &v) { return '"' + v +'"'; }
};
并简单地使用保存的字符串:
elem2.put<std::string>("key2", "asdf", my_id_translator<std::string>());
完整的程序:
#include <iostream>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include "property_tree/json_parser.hpp" // copied the headers
template <typename T>
struct my_id_translator
{
typedef T internal_type;
typedef T external_type;
boost::optional<T> get_value(const T &v) { return v.substr(1, v.size() - 2) ; }
boost::optional<T> put_value(const T &v) { return '"' + v +'"'; }
};
int main(int, char *[])
{
using namespace std;
using boost::property_tree::ptree;
using boost::property_tree::basic_ptree;
try
{
ptree root, arr,elem2;
basic_ptree<std::string, std::string> elem1;
elem1.put<int>("int", 10 );
elem1.put<bool>("bool", true);
elem2.put<double>("double", 2.2);
elem2.put<std::string>("string", "some string", my_id_translator<std::string>());
arr.push_back( std::make_pair("", elem1) );
arr.push_back( std::make_pair("", elem2) );
root.put_child("path1.path2", arr);
std::stringstream ss;
write_json(ss, root);
std::string my_string_to_send_somewhere_else = ss.str();
cout << my_string_to_send_somewhere_else << endl;
}
catch (std::exception & e)
{
cout << e.what();
}
return 0;
}
结果:)
{
"path1":
{
"path2":
[
{
"int": 10,
"bool": true
},
{
"double": 2.2,
"string": "some string"
}
]
}
}
升压确认其实施没有100%符合标准的JSON。 查看以下链接看到自己的解释: 制作,保留JSON类型的一个ptree中变体是未来的计划,但不远了。 !
我能想出用占位符,并在端线与实际值开沟额外的引号替换生成JSON的最简单和干净的解决方案。
static string buildGetOrdersCommand() {
ptree root;
ptree element;
element.put<string>("pendingOnly", ":pendingOnly");
element.put<string>("someIntValue", ":someIntValue");
root.put("command", "getOrders");
root.put_child("arguments", element);
std::ostringstream buf;
write_json(buf, root, false);
buf << std::endl;
string json = buf.str();
replace(json, ":pendingOnly", "true");
replace(json, ":someIntValue", std::to_string(15));
return json;
}
static void replace(string& json, const string& placeholder, const string& value) {
boost::replace_all<string>(json, "\"" + placeholder + "\"", value);
}
其结果是
{ “命令”: “getOrders”, “参数”:{ “pendingOnly”:真 “someIntValue”:15}}
正如我们的typedef basic_ptree <的std :: string,的std :: string> ptree中; 在Boost库,提升永远连载每个值作为串并分析所有值的等效字符串。
从输出JSON很显然,串行使用某种的ToString()方法的序列化一切字符串 - 也就是说,它不知道每个成员的类型,因此在封闭“”一切。
请参阅使用属性树在加速创建JSON阵列为更多关于这个问题。
文章来源: Why does Boost property tree write_json save everything as string? Is it possible to change that?