|
1 | 1 | /** |
2 | | - * @file json_jpath_demo.cpp |
| 2 | + * |
| 3 | + * @file jpath.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2025, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
3 | 12 | * @brief Demonstrates JSON navigation and mutation with JPath helpers. |
4 | 13 | * |
5 | 14 | * This example showcases how to use `jset()` and `jget()` to manipulate |
|
42 | 51 |
|
43 | 52 | int main() |
44 | 53 | { |
45 | | - using namespace vix::json; |
| 54 | + using namespace vix::json; |
46 | 55 |
|
47 | | - // --------------------------------------------------------------------- |
48 | | - // Start with an empty JSON object |
49 | | - // --------------------------------------------------------------------- |
50 | | - Json j = obj(); |
| 56 | + // --------------------------------------------------------------------- |
| 57 | + // Start with an empty JSON object |
| 58 | + // --------------------------------------------------------------------- |
| 59 | + Json j = obj(); |
51 | 60 |
|
52 | | - // --------------------------------------------------------------------- |
53 | | - // Use jset() to assign values via JPath |
54 | | - // Automatically creates missing objects/arrays |
55 | | - // --------------------------------------------------------------------- |
56 | | - jset(j, "user.langs[2]", "cpp"); // -> [null, null, "cpp"] |
57 | | - jset(j, "user.profile.name", "Gaspard"); // creates nested object |
58 | | - jset(j, R"(user["display.name"])", "Ada L."); // handles quoted keys |
| 61 | + // --------------------------------------------------------------------- |
| 62 | + // Use jset() to assign values via JPath |
| 63 | + // Automatically creates missing objects/arrays |
| 64 | + // --------------------------------------------------------------------- |
| 65 | + jset(j, "user.langs[2]", "cpp"); // -> [null, null, "cpp"] |
| 66 | + jset(j, "user.profile.name", "Gaspard"); // creates nested object |
| 67 | + jset(j, R"(user["display.name"])", "Ada L."); // handles quoted keys |
59 | 68 |
|
60 | | - // --------------------------------------------------------------------- |
61 | | - // Access nested values via jget() |
62 | | - // --------------------------------------------------------------------- |
63 | | - if (auto v = jget(j, "user.langs[2]")) |
64 | | - { |
65 | | - std::cout << v->get<std::string>() << "\n"; // cpp |
66 | | - } |
| 69 | + // --------------------------------------------------------------------- |
| 70 | + // Access nested values via jget() |
| 71 | + // --------------------------------------------------------------------- |
| 72 | + if (auto v = jget(j, "user.langs[2]")) |
| 73 | + { |
| 74 | + std::cout << v->get<std::string>() << "\n"; // cpp |
| 75 | + } |
67 | 76 |
|
68 | | - // --------------------------------------------------------------------- |
69 | | - // Pretty-print the resulting JSON |
70 | | - // --------------------------------------------------------------------- |
71 | | - std::cout << dumps(j, 2) << "\n"; |
| 77 | + // --------------------------------------------------------------------- |
| 78 | + // Pretty-print the resulting JSON |
| 79 | + // --------------------------------------------------------------------- |
| 80 | + std::cout << dumps(j, 2) << "\n"; |
72 | 81 | } |
0 commit comments