📝 added nicer example

This commit is contained in:
Niels Lohmann 2017-01-24 14:55:51 +01:00
parent 781fd09f2d
commit 50a3f3b301

View file

@ -457,9 +457,10 @@ namespace ns {
}; };
} }
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
// convert to JSON: copy each value into the JSON object // convert to JSON: copy each value into the JSON object
json j; json j;
ns::person p = createSomeone();
j["name"] = p.name; j["name"] = p.name;
j["address"] = p.address; j["address"] = p.address;
j["age"] = p.age; j["age"] = p.age;
@ -477,12 +478,19 @@ ns::person p {
It works, but that's quite a lot of boilerplate... Hopefully, there's a better way: It works, but that's quite a lot of boilerplate... Hopefully, there's a better way:
```cpp ```cpp
// person -> json // create a person
ns::person p = createPerson(); ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};
// conversion: person -> json
json j = p; json j = p;
// json -> person std::cout << j << std::endl;
auto p2 = j.get<ns::person>(); // {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
// conversion: json -> person
ns::person p2 = j;
// that's it
assert(p == p2); assert(p == p2);
``` ```