【问题标题】:How to create a generic function with write_json and read_josn using C++ boost library如何使用 C++ boost 库创建带有 write_json 和 read_josn 的通用函数
【发布时间】:2021-03-28 16:53:31
【问题描述】:

我正在使用不同的函数,在执行每个函数时,我想创建 Json 消息进行通信,如下所示:

TestFunction1(string id)
{
    "message" : "MSG_TestFunction1",
        "id" : "1212"
}

TestFunction2(string id)
{
    "message" : "MSG_TestFunction2",
        "id" : "1213"
}

在这种情况下,我认为它不需要维护 .JSON 文件,因为我们可以在函数本身中创建 json 消息(例如在 TestFunction1 和 TestFunction2 等中)。

通过考虑所有这些,我使用带有 write_json 和 read_json 的 BOOST 库创建了以下示例。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include<string>

using namespace boost::property_tree;
using namespace std;

bool TestFunction1(std::string f_id)
{
    ptree strTree;
    ptree subject_info;
    ptree array1;
    array1.put("message", "MSG_TestFunction1");
    array1.put("id", f_id);
    
    subject_info.push_back(make_pair("", array1));

    stringstream s;
    write_json(s, array1);
    string outstr = s.str();

    stringstream stream(outstr);
    ptree strreadTree;
    try {
        read_json(stream, strreadTree);
    }
    catch (ptree_error& e) {
        return false;
    }
    return true;
}

int main()
{
    TestFunction1("1212");
    system("pause");

    return 0;
}

这是创建和解析 json 数据的正确方法吗? 另外请帮助我如何创建一个通用函数或一个具有读写 json 的类,以利用 TestFunction1、TestFunction2 等所有函数来创建和解析 json 数据。

提前致谢

【问题讨论】:

  • 您问题顶部的代码是什么意思?它不是有效的 C++,也不是有效的 JSON。
  • 一旦 TestFunction1 被执行,它会创建类似 {"message" : "MSG_TestFunction1", "id" : "1212"} 的 JSON 消息,类似 TestFunction2、TestFunction3 等。然后我们可以将此序列化的 JSON 消息发送到其他应用程序。因此,作为参考,我提到了函数(TestFunction1、TestFunction2..)中所需的 JSON 消息格式

标签: c++ json boost c++14


【解决方案1】:

请不要将属性树用作 JSON 库。

Boost 1.75.0 增加了一个 JSON 库!

正如另一位评论者所说,您的具体需求不是很清楚,所以让我简单地勾勒几个类似的用例:

#include <boost/json.hpp>
#include <boost/json/src.hpp> // for compiler explorer
#include <string>
#include <iostream>

namespace json = boost::json;

json::value TestFunction(std::string const id) {
    return json::object{
        {"message", "MSG_TestFunction1"},
        {"id", id}
    };
}

int main() {
    for (auto id : {"1212", "1213"}) {
        json::error_code ec;
        auto jsonString = serialize(TestFunction(id));
        auto roundtrip = json::parse(jsonString);

        std::cout << roundtrip << std::endl;
    }
}

打印Live On Compiler Explorer

{"message":"MSG_TestFunction1","id":"1212"}
{"message":"MSG_TestFunction1","id":"1213"}

奖金

请注意,与属性树不同,您有类型,这意味着 ID 不必是字符串:

for (auto id : {1212, 1213}) {
    std::cout << json::object{{"message", "MSG_TestFunction1"}, {"id", id}}
              << std::endl;
}

打印 (Live Again):

{"message":"MSG_TestFunction1","id":1212}
{"message":"MSG_TestFunction1","id":1213}

【讨论】:

  • 谢谢@sehe,详细的解释。我有一个疑问,一旦使用“序列化”创建了 JSON 消息,“auto roundtrip = json::parse(jsonString);”行是转换并加密为 json 对象。因为变量“往返”以不同的格式显示值。
  • 我的要求是,在为这个应用程序执行不同的 API 函数时(比如执行 TestFunction1,它会创建类似 {"message" : "MSG_TestFunction1", "id" : "1212"} 的 JSON 消息对于TestFunction2,TestFunction3等。然后我们可以将此序列化的JSON消息发送到其他应用程序。因此,作为参考,我提到了函数中所需的JSON消息格式(TestFunction1,TestFunction2..)跨度>
  • "因为变量 "roundtrip" 以不同的格式显示值" - 它甚至显示在哪里?如果您的意思是std::cout &lt;&lt; roundtrip; 之后的输出显然:那就是 JSON 文本。 JSON 库输出 C++ 代码是不正常的,JSON 也不是有效的 C++ 代码(这又是was already remarked by the other commenter
  • 我认为您拥有所需的所有“活动部件”。当然,添加一个更通用的函数来创建不同名称的消息是微不足道的:godbolt.org/z/rEEcGY8hG
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多