json的object的key是string,但是值可以是任意类型,与C++中的普通map并不一致,因此对应的是map<string,variant>
基于:
可通过如下方式完成转换:
#include <iostream>
#include <string>
#include <variant>
#include <map>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace std;
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 st