66

我正在尝试使用boost属性树write_json进行序列化,它将所有内容保存为字符串,并不是数据错误,但我每次都需要明确地转换它们并且我想在其他地方使用它们。(如在 python 或其他 C++ json(非 boost)库中)

这是一些示例代码,我得到的取决于语言环境:

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

4

7 回答 7

38

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_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:

  1. commented line 37 - escaping the quote '"'
  2. changed line 76 - so that it doesn't add quotes anymore: 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"
            }
        ]
    }
}
于 2010-05-18T20:52:26.407 回答
13

Boost 确认其实现没有 100% 符合 JSON 标准。检查以下链接以查看他们的解释: 制作保留 JSON 类型的 ptree 变体是未来的计划,但还很遥远。

于 2014-11-05T02:38:15.517 回答
6

我能想出的最简单和最干净的解决方案是生成带有占位符的 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}}

于 2015-02-28T11:29:11.993 回答
5

我最终在我的 utils 中添加了另一个函数来解决这个问题:

#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();
    }
} }

希望有帮助。

于 2019-05-31T12:50:22.727 回答
3

因为我们有 typedef basic_ptree<std::string, std::string> ptree; 在 boost 库中,boost 将始终将每个值序列化为字符串并将所有值解析为等效的字符串。

于 2014-03-10T11:23:42.263 回答
2

从输出的 JSON 可以清楚地看出,序列化程序使用某种 .toString() 方法将所有内容序列化为字符串 - 也就是说,它不知道每个成员的类型,因此将所有内容包含在“”中。

有关此问题的更多信息,请参阅使用属性树在 Boost 中创建 JSON 数组。

于 2010-05-18T13:22:45.030 回答
1

所有需要明确地为字符串定制翻译器的解决方案对我来说似乎很容易出错,因为它有时可能会忘记它。最好通过继承的某种重载方式让属性树的 put 方法隐式处理这个问题,但这是不可能的,因为它是一个模板,你必须确保树的所有方法的完全协方差。如果可能的话,通常应该避免更改 boost 库的东西作为一种解决方法。

到目前为止,我发现的最强大的没有黑客攻击的方法是(自 C++11 起):

  • 使用 boost-property 树<KeyType, std::variant<yourTypes>>
  • 为您的变体提供翻译器(详细信息请参见下面的链接!),但不要根据 JSON 特定内容“破解”内容!这些几乎是完全正交的方面!
  • 为 JSON 编写自己的阅读器和编写器,应该很容易从 Boost 中改编

优点:

  • 就 JSON 特定的细节而言,不需要任何技巧来影响属性树
  • 除了您自己的新类型的特化(变体翻译器)之外,不会对 Boost 库及其命名空间造成污染
  • 类型比基于自定义字符串的属性树方法更安全
  • 对于树的非频繁序列化的许多运行时场景应该更快

缺点:

  • 需要为行为的一个很小的细节付出一些努力
  • 在编译方面可能会慢一些
  • 对于频繁序列化和树的微小更改的运行时场景可能会慢一些(可以肯定地进行优化)
  • 将 json 读回树中是一种有疑问的哲学工作,以确保所用类型之间尽可能多的对称性(出于许多目的,而不是学术问题)

有关更多详细信息,例如,请参阅

http://marko-editor.com/articles/property_tree_store_anything/

您可以轻松地将其调整为变体使用。

于 2020-11-03T09:04:18.703 回答