Boost 속성 트리 write_json이 모든 것을 문자열로 저장하는 이유는 무엇입니까? 그것을 바꿀 수 있습니까?
부스트 속성 트리 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
sth입니다. 이렇게 :
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
같은 값으로 저장 어쨌든 거기 : "key1" : true
나 "key2" : 2.2
?
내가 생각 해낼 수있는 가장 간단하고 깨끗한 솔루션은 자리 표시 자와 함께 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);
}
결과는
{ "command": "getOrders", "arguments": { "pendingOnly": true, "someIntValue": 15}}
좋아, 나는 이것을 이렇게 해결했다 (물론 그것은 약간의 해킹이므로 추가 작업이 필요한 모든 사람에게 적합하지는 않습니다).
I의 한 내 자신의 작성 write_json
(단순히 파일을 복사 기능을 json_parser.hpp
하고 json_parser_write.hpp
내 프로젝트)과에서 다음 줄을 수정 json_parser_write.hpp
:
- 주석 처리 된 줄 37-따옴표 ' "'이스케이프
- 76 행 변경-더 이상 따옴표를 추가하지 않습니다.
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"
}
]
}
}
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.!
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.
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 " ".
See Creating JSON arrays in Boost using Property Trees for more about this problem .
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, "$1");
std::ofstream file;
file.open(path);
file << result;
file.close();
}
} }
Hope that helps.
'Development Tip' 카테고리의 다른 글
Haskell에 ML 스타일 모듈을 추가하는 데있어 가장 큰 이론적 어려움은 무엇입니까? (0) | 2020.12.05 |
---|---|
RequireJS로 지연 로딩을 달성하는 방법은 무엇입니까? (0) | 2020.12.05 |
Koa / Co / Bluebird 또는 Q / Generators / Promises / Thunks 상호 작용? (0) | 2020.12.05 |
AAR에 전 이적 종속성이 포함될 수 있습니까? (0) | 2020.12.05 |
xargs는 bash 별칭을 인식하지 못합니다. (0) | 2020.12.05 |