2022-04-25 22:40:45 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
2022-07-31 17:38:52 +02:00
|
|
|
using namespace nlohmann::literals;
|
2022-04-25 22:40:45 +02:00
|
|
|
|
|
|
|
namespace ns
|
|
|
|
{
|
|
|
|
struct person
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string address;
|
|
|
|
int age;
|
|
|
|
};
|
|
|
|
|
|
|
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, address, age)
|
|
|
|
} // namespace ns
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
|
|
|
|
|
|
|
|
// serialization: person -> json
|
|
|
|
json j = p;
|
|
|
|
std::cout << "serialization: " << j << std::endl;
|
|
|
|
|
|
|
|
// deserialization: json -> person
|
|
|
|
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json;
|
|
|
|
auto p2 = j2.get<ns::person>();
|
|
|
|
|
|
|
|
// incomplete deserialization:
|
|
|
|
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto p3 = j3.get<ns::person>();
|
|
|
|
}
|
|
|
|
catch (json::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << "deserialization failed: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|