| 240 | } |
| 241 | |
| 242 | void variant_example2() |
| 243 | { |
| 244 | using variant_type = std::variant<int, double, bool, std::string, ns::Color>; |
| 245 | |
| 246 | std::vector<variant_type> vars = {100, 10.1, false, std::string("Hello World"), ns::Color::yellow}; |
| 247 | |
| 248 | std::string buffer; |
| 249 | jsoncons::encode_json_pretty(vars, buffer); |
| 250 | |
| 251 | std::cout << "(1)\n" << buffer << "\n\n"; |
| 252 | |
| 253 | auto vars2 = jsoncons::decode_json<std::vector<variant_type>>(buffer); |
| 254 | |
| 255 | auto visitor = [](auto&& arg) { |
| 256 | using T = std::decay_t<decltype(arg)>; |
| 257 | if constexpr (std::is_same_v<T, int>) |
| 258 | std::cout << "int " << arg << '\n'; |
| 259 | else if constexpr (std::is_same_v<T, double>) |
| 260 | std::cout << "double " << arg << '\n'; |
| 261 | else if constexpr (std::is_same_v<T, bool>) |
| 262 | std::cout << "bool " << arg << '\n'; |
| 263 | else if constexpr (std::is_same_v<T, std::string>) |
| 264 | std::cout << "std::string " << arg << '\n'; |
| 265 | else if constexpr (std::is_same_v<T, ns::Color>) |
| 266 | std::cout << "ns::Color " << arg << '\n'; |
| 267 | }; |
| 268 | |
| 269 | std::cout << "(2)\n"; |
| 270 | for (const auto& item : vars2) |
| 271 | { |
| 272 | std::visit(visitor, item); |
| 273 | } |
| 274 | std::cout << "\n"; |
| 275 | } |
| 276 | |
| 277 | void variant_example3() |
| 278 | { |
no test coverage detected