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

Function variant_example3

examples/src/json_traits_variant_examples.cpp:277–310  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

275}
276
277void variant_example3()
278{
279 using variant_type = std::variant<int, double, bool, ns::Color, std::string>;
280
281 std::vector<variant_type> vars = {100, 10.1, false, std::string("Hello World"), ns::Color::yellow};
282
283 std::string buffer;
284 jsoncons::encode_json_pretty(vars, buffer);
285
286 std::cout << "(1)\n" << buffer << "\n\n";
287
288 auto vars2 = jsoncons::decode_json<std::vector<variant_type>>(buffer);
289
290 auto visitor = [](auto&& arg) {
291 using T = std::decay_t<decltype(arg)>;
292 if constexpr (std::is_same_v<T, int>)
293 std::cout << "int " << arg << '\n';
294 else if constexpr (std::is_same_v<T, double>)
295 std::cout << "double " << arg << '\n';
296 else if constexpr (std::is_same_v<T, bool>)
297 std::cout << "bool " << arg << '\n';
298 else if constexpr (std::is_same_v<T, std::string>)
299 std::cout << "std::string " << arg << '\n';
300 else if constexpr (std::is_same_v<T, ns::Color>)
301 std::cout << "ns::Color " << arg << '\n';
302 };
303
304 std::cout << "(2)\n";
305 for (const auto& item : vars2)
306 {
307 std::visit(visitor, item);
308 }
309 std::cout << "\n";
310}
311
312void variant_example4()
313{

Callers 1

mainFunction · 0.85

Calls 1

encode_json_prettyFunction · 0.85

Tested by

no test coverage detected