可以借助json实现map和struct的互转:
#include <iostream>
#include <string>
#include <variant>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
using VDdata = variant<int, string>;
using MData = map<string, VDdata>;
namespace nlohmann {
template <typename T, typename... Ts>
void variant_from_json(const nlohmann::json &j, std::variant<Ts...> &data) {
try {
data = j.get<T>();
} catch (...) {
}
}
template <typename... Ts>
struct adl_serializer<std::variant<Ts...>>
{
static void to_json(nlohmann::json &j, const std::variant<Ts...> &data) {
std::visit([&j](const auto &v) { j