c++ - Cpp : JSON parser in Cpp that provide support Serialize/Deserialize feature, 将 JSON 对象转换为用户定义的类?

标签 c++ json parsing jackson-databind rapidjson

我正在从事 native C++ 开发,并正在寻找可以处理复杂 JSON 文件并转换为类对象的 JSON 解析器。

  1. 我看过native benchmarks for JSON parsers可以在 C++ 中使用,并得出结论:考虑到处理时间和大小处理,RapidJSON 很受欢迎并且最适合。

  2. 我的要求是将 JSON 对象转换为用户定义的类,反之亦然。

  3. JacksonObjectmapper该类提供从基本 POJO(普通旧 Java 对象)或从通用 JSON 树模型 (JsonNode) 读取和写入 JSON 的功能,以及用于执行转换的相关功能。

问题:

  1. RapidJSON 或其他 JSON 解析器中是否有等效项允许我们配置序列化和反序列化功能(例如:Jackson JAVA 库是高度可定制的序列化和反序列化过程,将 JSON 对象转换为 Java 类)?
  2. 如果否,解决该问题的正确方法是什么?是否有唯一的方法来构建您自己的序列化器以转换为我们的自定义类?

注意:我查看了 stackoverflow 上的一些帖子,但没有找到能回答这个问题的帖子。

最佳答案

jsoncons , nlohmannThorsSerializer全部支持 JSON 和 C++ 对象之间的转换。下面显示了 jsoncons 和 nlohmann 的示例,Martin York 在另一篇文章中为他的 ThorsSerializer 提供了一个示例。我认为后者非常好,而且肯定因其简洁而获奖。本着 Oscar Wilde 名言“模仿是最真诚的奉承形式”的精神,我向 jsoncons 引入了宏 JSONCONS_ALL_MEMBER_TRAITS,并相应地修改了该示例。

考虑

const std::string s = R"(
[
    {
        "author" : "Haruki Murakami",
        "title" : "Kafka on the Shore",
        "price" : 25.17
    },
    {
        "author" : "Charles Bukowski",
        "title" : "Pulp",
        "price" : 22.48
    }
]
)";

namespace ns {
    struct book
    {
        std::string author;
        std::string title;
        double price;
    };
} // namespace ns

使用 jsoncons 在 s 之间进行转换和一个std::vector<ns::book> :

#include <jsoncons/json.hpp>

namespace jc = jsoncons;

// Declare the traits. Specify which data members need to be serialized.
JSONCONS_ALL_MEMBER_TRAITS(ns::book,author,title,price);

int main()
{
    std::vector<ns::book> book_list = jc::decode_json<std::vector<ns::book>>(s);

    std::cout << "(1)\n";
    for (const auto& item : book_list)
    {
        std::cout << item.author << ", " 
                  << item.title << ", " 
                  << item.price << "\n";
    }

    std::cout << "\n(2)\n";
    jc::encode_json(book_list, std::cout, jc::indenting::indent);
    std::cout << "\n\n";
}

输出:

(1)
Haruki Murakami, Kafka on the Shore, 25.17
Charles Bukowski, Pulp, 22.48

(2)
[
    {
        "author": "Haruki Murakami",
        "price": 25.17,
        "title": "Kafka on the Shore"
    },
    {
        "author": "Charles Bukowski",
        "price": 22.48,
        "title": "Pulp"
    }
]

使用 nlohmann 在 s 之间进行转换和一个std::vector<ns::book> :

#include <nlohmann/json.hpp>
#include <iomanip>

namespace nh = nlohmann;

// Provide from_json and to_json functions in the same namespace as your type   
namespace ns {
    void from_json(const nh::json& j, ns::book& val) 
    {
        j.at("author").get_to(val.author);
        j.at("title").get_to(val.title);
        j.at("price").get_to(val.price);
    }

    void to_json(nh::json& j, const ns::book& val) 
    {
        j["author"] = val.author;
        j["title"] = val.title;
        j["price"] = val.price;
    }
} // namespace ns

int main()
{
    nh::json j = nh::json::parse(s);

    std::vector<ns::book> book_list = j.get<std::vector<ns::book>>();
    std::cout << "\n(1)\n";
    for (const auto& item : book_list)
    {
        std::cout << item.author << ", " 
                  << item.title << ", " 
                  << item.price << "\n";
    }

    std::cout << "\n(2)\n";
    nh::json j2 = book_list;
    std::cout << std::setw(4) << j2 << "\n\n";
}

输出:

(1)
Haruki Murakami, Kafka on the Shore, 25.17
Charles Bukowski, Pulp, 22.48

(2)
[
    {
        "author": "Haruki Murakami",
        "price": 25.17,
        "title": "Kafka on the Shore"
    },
    {
        "author": "Charles Bukowski",
        "price": 22.48,
        "title": "Pulp"
    }
]

关于c++ - Cpp : JSON parser in Cpp that provide support Serialize/Deserialize feature, 将 JSON 对象转换为用户定义的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54318130/

相关文章:

javascript - 使用 Node js从另一个文件调用两个文件

java - 解析具有不同日期格式的日期字符串

c++ - 文件持久性设置

c++ - CFileFind 不考虑目录的第一个文件

json - 使用 Angular 服务传递 json 数据

ruby-on-rails - rails 5.1。 : cannot get JSON object from string

java - 'create table as ..' 查询的 JSqlParser

Scala 类型不匹配

C++:避免​​重复符号链接(symbolic link)器错误

c++ - 将 this 传递给 this 的类变量的方法