MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / variant_example

Function variant_example

examples/src/json_traits_variant_examples.cpp:196–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194)
195
196void variant_example()
197{
198 std::string input = R"(
199{
200 "owner": "Rodrigo",
201 "items": [
202 {
203 "name": "banana",
204 "color": "YELLOW"
205 },
206 {
207 "size": 40,
208 "material": "wool"
209 },
210 {
211 "name": "apple",
212 "color": "RED"
213 },
214 {
215 "size": 40,
216 "material": "cotton"
217 }
218 ]
219}
220 )";
221
222 ns::Basket basket = jsoncons::decode_json<ns::Basket>(input);
223 std::cout << basket.owner() << "\n\n";
224
225 std::cout << "(1)\n";
226 for (const auto& var : basket.items())
227 {
228 std::visit([](auto&& arg) {
229 using T = std::decay_t<decltype(arg)>;
230 if constexpr (std::is_same_v<T, ns::Fruit>)
231 std::cout << "Fruit " << arg << '\n';
232 else if constexpr (std::is_same_v<T, ns::Fabric>)
233 std::cout << "Fabric " << arg << '\n';
234 }, var);
235 }
236
237 std::string output;
238 jsoncons::encode_json_pretty(basket, output);
239 std::cout << "(2)\n" << output << "\n\n";
240}
241
242void variant_example2()
243{

Callers 1

mainFunction · 0.85

Calls 3

encode_json_prettyFunction · 0.85
ownerMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected