Cpp:Cpp中的JSON解析器是否提供支持序列化/反序列化功能,将JSON对象转换为用户定义的类? [英] Cpp: JSON parser in Cpp that provide support Serialize/Deserialize feature, converting JSON objects to user-defined classes?
问题描述
我正在从事本机C ++开发,并正在寻找可以处理复杂JSON文件并转换为类对象的JSON解析器.
I'm working on native C++ development and looking for JSON parser that can handle complex JSON files and convert into class objects.
-
我查看了用于JSON解析器的本地基准,该版本在C ++和得出结论,考虑到处理时间和大小处理,RapidJSON很流行并且最适合.
I've looked at native benchmarks for JSON parsers available in C++ and came to conclusion that RapidJSON is popular and best fit considering processing time and size handling.
我的要求是将JSON对象转换为用户定义的类,反之亦然.
My requirement is to convert JSON objects to user defined classes and vice versa.
Jackson 具有
The Jackson has Objectmapper class that provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.
问题:
- RapidJSON或其他JSON解析器中是否存在等效项,可让我们配置序列化和反序列化功能(例如:Jackson JAVA库是高度可定制的序列化和反序列化过程,将JSON对象转换为Java类)?
- 如果否,解决该问题的正确方法是什么?有没有唯一的方法来构建自己的序列化器以转换为我们的自定义类?
- Is there an equivalent in RapidJSON or other JSON parser that allow us to configure Serialize and Deserialize feature (ex: Jackson JAVA library is the highly customizable serialization and deserialization process, converting JSON objects to Java classes)?
- If No, What's the right way to work around it? Is there only way to build your own serializer to convert to our custom classes?
注意:我看过关于stackoverflow的几篇文章,却找不到答案.
NOTE: I've looked at few posts on stackoverflow and did not find one that answers it.
推荐答案
jsoncons, nlohmann and ThorsSerializer all support conversion between JSON and C++ objects. Examples with jsoncons and nlohmann are shown below, Martin York has one for his ThorsSerializer in a separate posting. The latter in my opinion is very nice, and certainly wins the prize for brevity. In the spirit of Oscar Wilde's quote that "imitation is the sincerest form of flattery", I've introduced the macro JSONCONS_MEMBER_TRAITS_DECL to jsoncons, and modified that example accordingly.
考虑
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>
之间进行转换:
Using jsoncons to convert between s
and an std::vector<ns::book>
:
#include <jsoncons/json.hpp>
namespace jc = jsoncons;
// Declare the traits. Specify which data members need to be serialized.
JSONCONS_MEMBER_TRAITS_DECL(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>
之间进行转换:
Using nlohmann to convert between s
and an 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"
}
]
这篇关于Cpp:Cpp中的JSON解析器是否提供支持序列化/反序列化功能,将JSON对象转换为用户定义的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!