how to convert json to object with "new" keyword #2565
-
Hi.
and my question is: how to convert the json to struct/ Object with "new" keyword : i.e;( thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This works: #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
namespace ns {
struct person {
std::string name;
std::string address;
int age;
};
void to_json(json& j, const person& p) {
j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
}
void from_json(const json& j, person& p) {
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
}
int main()
{
json j = R"({"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"})"_json;
auto* p = new ns::person(j);
std::cout << p->name << ", " << p->address << ", " << p->age << std::endl;
} |
Beta Was this translation helpful? Give feedback.
This works: