gpt4 book ai didi

c++ - 无法使用 Nlohmann 的 JSON 获取对象内部的 vector

转载 作者:行者123 更新时间:2023-12-04 13:51:01 25 4
gpt4 key购买 nike

我有以下代码(简化):

namespace nlohmann {
// https://github.com/nlohmann/json/issues/1749#issuecomment-772996219
template <class T>
void to_json(nlohmann::json &j, const std::optional<T> &v) {
if (v.has_value())
j = *v;
else
j = nullptr;
}

template <class T>
void from_json(const nlohmann::json &j, std::optional<T> &v) {
if (j.is_null())
v = std::nullopt;
else
v = j.get<T>(); /* ERROR LOCATION */
}
} // namespace nlohmann

namespace discordpp{
class Component {
public:
Component(const std::optional<std::vector<Component>> &components)
: components(components) {}

std::optional<std::vector<Component>> components;

// (Resolved) NLOHMANN_DEFINE_TYPE_INTRUSIVE(Component, components)
friend void to_json(nlohmann::json &nlohmann_json_j,
const Component &nlohmann_json_t) {
nlohmann_json_j["components"] = nlohmann_json_t.components;
}
friend void from_json(const nlohmann::json &nlohmann_json_j,
Component &nlohmann_json_t) {
nlohmann_json_j.at("components").get_to(nlohmann_json_t.components); /* ERROR LOCATION */
}
};
}// namespace discordpp
编译返回错误 error: no matching function for call to ‘nlohmann::basic_json<>::get<std::vector<discordpp::Component, std::allocator<discordpp::Component> > >() const’也许我可以预先声明来解决这个问题?
尝试设置这样的 adl_serializer,同样的问题
namespace nlohmann {
template<> struct adl_serializer<discordpp::Component> {
static void to_json(json &nlohmann_json_j, const discordpp::Component &nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(
NLOHMANN_JSON_TO, type, custom_id, disabled, style, label, emoji,
url, options, placeholder, min_values, max_values, components))
}

static void from_json(const json &nlohmann_json_j, discordpp::Component &nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(
NLOHMANN_JSON_FROM, type, custom_id, disabled, style, label, emoji,
url, options, placeholder, min_values, max_values, components))
}
};
} // namespace nlohmann
我将其更改为共享指针 vector ,现在我得到 error: no matching function for call to ‘nlohmann::basic_json<>::get<discordpp::Component>() const’adl_serializer我写信来处理它
template <typename T> struct adl_serializer<std::vector<std::shared_ptr<T>>> {
static void to_json(json &j, const std::vector<std::shared_ptr<T>> &v) {
j = json::array();
for (auto t : v) {
j.push_back(*t);
}
}

static void from_json(const json &j, std::vector<std::shared_ptr<T>> &v) {
if (!j.is_array()) {
throw json::type_error::create(317, "j must be an array", j);
}
for (auto t : j) {
v.push_back(std::make_shared<T>(j.get<T>())); /* Error is here */
}
}
};
镜像于 https://github.com/nlohmann/json/discussions/3047
编辑:
完整源代码(进行递归克隆): https://github.com/DiscordPP/echo-bot/tree/dev-objects
构建失败: https://github.com/DiscordPP/echo-bot/runs/3799394029?check_suite_focus=true
编辑 2:当我这样做时很好 std::vector<std::shared_ptr<Component>> components; ,它似乎无法处理 2 个第三方解析器,也许吧?

最佳答案

我认为问题在于您尝试拥有类型为 std::optional<std::vector<Component>> 的成员在类 Component , 和 std::optional不适用于不完整的类型。将其替换为 std::unique_ptr<std::vector<Component>>应该管用。

关于c++ - 无法使用 Nlohmann 的 JSON 获取对象内部的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69421674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com