|
1 | 1 | #include <iostream>
|
2 | 2 | #include <pre/json/from_json.hpp>
|
| 3 | +#include <pre/json/mapping.hpp> |
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
3 | 6 |
|
4 | 7 | struct customer {
|
5 | 8 | std::string name;
|
6 | 9 | size_t money_spent;
|
7 | 10 | std::vector<std::string> interests;
|
| 11 | + std::optional<bool> is_private; // this property shall be mapped to the "private" key in the json |
| 12 | + std::vector<int> numbers; |
8 | 13 | };
|
9 | 14 |
|
10 | 15 | BOOST_FUSION_ADAPT_STRUCT(customer,
|
11 | 16 | name,
|
12 | 17 | money_spent,
|
13 |
| - interests) |
| 18 | + interests, |
| 19 | + is_private, |
| 20 | + numbers) |
14 | 21 |
|
15 | 22 | int main() {
|
16 | 23 |
|
| 24 | + |
17 | 25 | std::string string_to_deserialize =
|
18 |
| - "{\"interests\":[\"sport articles\"], \"money_spent\":50, \"name\":\"Mrs. Fraulein\"}"; |
| 26 | + "{\"interests\":[\"sport articles\"], \"money_spent\":50, \"name\":\"Mrs. Fraulein\", \"private\": true, \"numbers\": [1,2,3]}"; |
| 27 | + |
| 28 | + // classic deserialization |
| 29 | + { |
| 30 | + customer my_customer = pre::json::from_json<customer>(string_to_deserialize); |
| 31 | + |
| 32 | + std::cout << "Customer " << my_customer.name << " spent " << |
| 33 | + my_customer.money_spent << std::endl; |
| 34 | + } |
| 35 | + |
| 36 | + // with mapping deserialization |
| 37 | + { |
| 38 | + customer my_customer = pre::json::from_json<customer>(string_to_deserialize, [](nlohmann::json& jdoc) { |
| 39 | + jdoc["is_private"] = jdoc["private"]; |
| 40 | + jdoc.erase("private"); |
| 41 | + }); |
| 42 | + |
| 43 | + std::cout << "Customer " << my_customer.name << " spent " << |
| 44 | + my_customer.money_spent << " is private:" << std::to_string(my_customer.is_private.value()) << std::endl; |
| 45 | + } |
| 46 | + |
| 47 | + // with mapping helpers |
| 48 | + { |
| 49 | + customer my_customer = pre::json::from_json<customer>(string_to_deserialize, [](nlohmann::json& jdoc) { |
| 50 | + pre::json::remap_property(jdoc, "private", "is_private"); |
| 51 | + }); |
| 52 | + |
| 53 | + std::cout << "Customer " << my_customer.name << " spent " << |
| 54 | + my_customer.money_spent << " is private:" << std::to_string(my_customer.is_private.value()) << std::endl; |
| 55 | + } |
19 | 56 |
|
20 |
| - customer my_customer = pre::json::from_json<customer>(string_to_deserialize); |
| 57 | + // with mapping and json_pointer helpers |
| 58 | + { |
| 59 | + std::string string_to_deserialize = |
| 60 | + "{\"interests\":[\"sport articles\"], \"money_spent\":50, \"name\":\"Mrs. Fraulein\", \"private\": true, \"numbers\": [1,2,3]}"; |
21 | 61 |
|
22 |
| - std::cout << "Customer " << my_customer.name << " spent " << |
23 |
| - my_customer.money_spent << std::endl; |
| 62 | + customer my_customer = pre::json::from_json<customer>(string_to_deserialize, [](nlohmann::json& jdoc) { |
| 63 | + pre::json::remap_property(jdoc, "/private"_json_pointer, "/is_private"_json_pointer); |
| 64 | + pre::json::remap_property(jdoc, "/numbers/0"_json_pointer, "/numbersNew/2"_json_pointer); |
| 65 | + pre::json::remap_property(jdoc, "/money_spent"_json_pointer, "/numbers/0"_json_pointer); |
| 66 | + jdoc["money_spent"] = 0; |
| 67 | + std::cout << jdoc.dump(2) << std::endl; |
| 68 | + }); |
| 69 | + } |
24 | 70 |
|
25 | 71 | return 0;
|
26 | 72 | }
|
0 commit comments