| 310 | } |
| 311 | |
| 312 | void variant_example4() |
| 313 | { |
| 314 | using variant_type = std::variant<std::nullptr_t, int, double, bool, std::string>; |
| 315 | |
| 316 | std::vector<variant_type> v = {nullptr, 10, 5.1, true, std::string("Hello World")}; |
| 317 | |
| 318 | std::string buffer; |
| 319 | jsoncons::encode_json_pretty(v, buffer); |
| 320 | std::cout << "(1)\n" << buffer << "\n\n"; |
| 321 | |
| 322 | auto v2 = jsoncons::decode_json<std::vector<variant_type>>(buffer); |
| 323 | |
| 324 | auto visitor = [](auto&& arg) { |
| 325 | using T = std::decay_t<decltype(arg)>; |
| 326 | if constexpr (std::is_same_v<T, std::nullptr_t>) |
| 327 | std::cout << "nullptr " << arg << '\n'; |
| 328 | else if constexpr (std::is_same_v<T, int>) |
| 329 | std::cout << "int " << arg << '\n'; |
| 330 | else if constexpr (std::is_same_v<T, double>) |
| 331 | std::cout << "double " << arg << '\n'; |
| 332 | else if constexpr (std::is_same_v<T, bool>) |
| 333 | std::cout << "bool " << arg << '\n'; |
| 334 | else if constexpr (std::is_same_v<T, std::string>) |
| 335 | std::cout << "std::string " << arg << '\n'; |
| 336 | }; |
| 337 | |
| 338 | std::cout << "(2)\n"; |
| 339 | for (const auto& item : v2) |
| 340 | { |
| 341 | std::visit(visitor, item); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void distinguish_by_type() |
| 346 | { |
no test coverage detected