为什么 Boost 属性树 write_json 将所有内容保存为字符串?有可能改变吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2855741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Boost property tree write_json save everything as string? Is it possible to change that?
提问by pprzemek
I'm trying to serialize using boost property tree write_json, it saves everything as strings, it's not that data are wrong, but I need to cast them explicitly every time and I want to use them somewhere else. (like in python or other C++ json (non boost) library)
我正在尝试使用 boost 属性树 write_json 进行序列化,它将所有内容保存为字符串,并不是数据错误,但我每次都需要显式转换它们,并且我想在其他地方使用它们。(如在 python 或其他 C++ json(非 boost)库中)
here is some sample code and what I get depending on locale:
这是一些示例代码以及我根据语言环境得到的内容:
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();
and my_string_to_send_somewhere_elseis sth. like this:
并且my_string_to_send_somewhere_else是…… 像这样:
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
Is there anyway to save them as the values, like:
"key1" : trueor "key2" : 2.2?
无论如何将它们保存为值,例如:
"key1" : true或"key2" : 2.2?
采纳答案by sbile
The simplest and cleanest solution that i could come up with was generating the JSON with placeholders and in the end string replacing with the actual value ditching the extra quotes.
我能想到的最简单、最干净的解决方案是生成带有占位符的 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);
}
And the result is
结果是
{"command":"getOrders","arguments":{"pendingOnly":true,"someIntValue":15}}
{"command":"getOrders","arguments":{"pendingOnly":true,"someIntValue":15}}
回答by pprzemek
Ok, I've solved it like this, (of course it won't suite for everybody, as it is a bit of a hack, that need further work).
好的,我已经这样解决了,(当然它不会适合所有人,因为它有点黑客,需要进一步的工作)。
I've wrote my own write_jsonfunction (simply copied the files, json_parser.hppand json_parser_write.hppto my project) and modified the following lines in json_parser_write.hpp:
[我已写我自己的write_json功能(简单复制的文件,json_parser.hpp并json_parser_write.hpp到我的项目),并修改了以下几行json_parser_write.hpp:
- commented line 37 - escaping the quote '"'
- changed line 76 - so that it doesn't add quotes anymore:
stream << Ch('"') << data << Ch('"'); ==> stream << data;
- 注释第 37 行 - 转义引号 '"'
- 更改了第 76 行 - 使其不再添加引号:
stream << Ch('"') << data << Ch('"'); ==> stream << data;
Then values will be saved properly except for strings, so I wrote custom translator for it:
然后值将被正确保存,除了字符串,所以我为它编写了自定义翻译器:
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 +'"'; }
};
and simply saved string using:
并简单地使用以下方法保存字符串:
elem2.put<std::string>("key2", "asdf", my_id_translator<std::string>());
complete program:
完整程序:
#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;
}
result :)
结果 :)
{
"path1":
{
"path2":
[
{
"int": 10,
"bool": true
},
{
"double": 2.2,
"string": "some string"
}
]
}
}
回答by Alex Sed
Boost confirms its implementation has no 100% conformance to JSON standard. Check the following link to see their explanation: Making a ptree variant that preserves JSON types is a future plan, but far off.!
Boost 确认其实现没有 100% 符合 JSON 标准。检查以下链接以查看他们的解释: 制作保留 JSON 类型的 ptree 变体是未来的计划,但还很遥远。!
回答by Josbert Lonnee
As we have typedef basic_ptree<std::string, std::string> ptree; in the boost libraries, boost will always serialize each value as string and parse all values to a string equivalent.
因为我们有 typedef basic_ptree<std::string, std::string> ptree; 在 boost 库中,boost 总是将每个值序列化为字符串,并将所有值解析为等效的字符串。
回答by Sean Kinsey
From the outputted JSON it is clear that the serializer serializes everything to strings using some sort of .toString() method - that is, its unaware of the type of each member and so encloses everything in " ".
从输出的 JSON 中可以清楚地看出,序列化程序使用某种 .toString() 方法将所有内容序列化为字符串——也就是说,它不知道每个成员的类型,因此将所有内容都包含在“”中。
See Creating JSON arrays in Boost using Property Treesfor more about this problem .
有关此问题的更多信息,请参阅使用属性树在 Boost 中创建 JSON 数组。
回答by Sean Kinsey
I ended up adding another function to my utils to solve this:
我最终向我的实用程序添加了另一个函数来解决这个问题:
#include <string>
#include <regex>
#include <boost/property_tree/json_parser.hpp>
namespace bpt = boost::property_tree;
typedef bpt::ptree JSON;
namespace boost { namespace property_tree {
inline void write_jsonEx(const std::string & path, const JSON & ptree)
{
std::ostringstream oss;
bpt::write_json(oss, ptree);
std::regex reg("\\"([0-9]+\.{0,1}[0-9]*)\\"");
std::string result = std::regex_replace(oss.str(), reg, "");
std::ofstream file;
file.open(path);
file << result;
file.close();
}
} }
Hope that helps.
希望有帮助。

