gpt4 book ai didi

c++ - boost 属性 write_json 不正确的行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:59 25 4
gpt4 key购买 nike

我正在为 Boost 属性树编写一个 JSON 包装器。目前的重点是将生成的 JSON 写入字符串或文件。

使用 boost::property_tree::json_parser::write_json(ss, *pt) 生成的属性树被写成一个字符串。

但是这个方法不理解什么是真、假、空或数字。一切都转换为字符串。

阅读 Boost 文档,这是库的一个限制。有什么办法可以修改这种行为吗?

最佳答案

Link此链接中包含问题的修复程序。

它涉及更改 boost 代码,因此我尝试了另一种选择。我的解决方案涉及正则表达式:

std::string JSONObject::toString() const
{
boost::regex exp("\"(null|true|false|[0-9]+(\\.[0-9]+)?)\"");
std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, *pt);
std::string rv = boost::regex_replace(ss.str(), exp, "$1");

return rv;
}

基本上我会搜索关键字:true、false、null 和任何类型的数字。匹配项将替换为不带引号的内容。

关于c++ - boost 属性 write_json 不正确的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com