为什么提振属性树write_json节省一切,字符串?是否有可能改变这种状况? [英] Why boost property tree write_json saves everything as string? Is it possible to change that?
问题描述
我想使用boost属性树write_json序列化,这样可以节省一切为字符串,这并不是说数据是错误的,但我需要每次都显式转换他们,我想别的地方使用它们。 (像Python或其他C ++ JSON(非增压)库)
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)
下面是一些示例code和我所得到的根据地区不同:
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();
和 my_string_to_send_somewhere_else
为某事。像这样的:
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
反正是有其保存为值,比如:KEY1:真正的
或KEY2:2.2
推荐答案
好吧,我解决了这个样子,(当然也不会适合每个人,因为它是一个黑客位,即需要进一步工作)。
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).
我写我自己write_json功能(简单地复制文件,json_parser.hpp和json_parser_write.hpp到我的项目),并在json_parser_write.hpp修改以下行:
I've wrote my own write_json function (simply copied the files, json_parser.hpp and json_parser_write.hpp to my project) and modified the following lines in json_parser_write.hpp:
- 注释行37 - 逃避报价'
- 改变行76 - 这样它不添加引号了:
流&LT;&LT; CH('')所述;&lt;数据&所述;&下; CH(''); ==>流&LT;&LT;数据;
比起值将除字符串正确保存,所以我写了自定义的翻译吧:
Than 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>());
完整的程序:
#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"
}
]
}
}
这篇关于为什么提振属性树write_json节省一切,字符串?是否有可能改变这种状况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!