【问题标题】:Can't get_to vector of object inside itself with Nlohmann's JSON无法使用 Nlohmann 的 JSON 在自身内部获取对象向量
【发布时间】:2021-11-24 00:49:05
【问题描述】:

我有以下代码(简化):

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&lt;&gt;::get&lt;std::vector&lt;discordpp::Component, std::allocator&lt;discordpp::Component&gt; &gt; &gt;() 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

我将其更改为共享指针向量,现在我在 adl_serializer 中得到了 error: no matching function for call to ‘nlohmann::basic_json&lt;&gt;::get&lt;discordpp::Component&gt;() const’ 我写来处理它

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&lt;std::shared_ptr&lt;Component&gt;&gt; components; 时很好,它似乎无法处理 2 个第三方解析器,也许?

【问题讨论】:

    标签: c++ type-conversion nlohmann-json


    【解决方案1】:

    我对@Niels 的回答做了一些思考,我意识到friend void from_json 正在采用已经构造的Component&amp;,但Component 没有默认构造函数。我添加了Component() : components(std::nullopt) {},我们开始比赛了!

    【讨论】:

      【解决方案2】:

      我认为问题在于您在Component 的向量上调用get_to,但您无法将json 转换为Component。

      一个解决方案是添加一个构造函数:

      explicit Component(const nlohmann::json &nlohmann_json_j = nlohmann::json()) 
      {
          // do whatever you have to do
      }
      

      请注意,我添加了= nlohmann::json(),因为std::vector&lt;Component&gt; 需要默认构造函数。

      注意:我测试了这种方法,它按预期工作。

      现场演示:

      https://replit.com/@ichramm/somerandomtest

      编译:

      clang++-7 -Iinclude -std=c++17 -o main main.cpp && ./main
      

      更新:我正在添加更多详细信息,因为看起来问题尚未完全理解。

      问题与std::optional无关。

      问题是有一个 组件向量,它应该是从 json 向量创建的。

      您没有提供从/向 json 读取/写入选项的方法,但您没有提供读取/写入类型为 Component 的对象的方法。

      在j.get&lt;T&gt;();这一行中,T是std::vector&lt;Component&gt;,而j可以认为是std::vector&lt;Json&gt;。

      或者,您可以为您的类型Component 创建一个函数from_json。我还没有测试过,但看起来它可以工作。

      【讨论】:

      • 据我了解,这是from_json 通常能够提供的转换,我想当您尝试这样做时检测会中断。
      • 你是对的,但也错了。我将详细说明我的答案。
      • 虽然说Component 没有from_json 的错误来自from_json 的Component,但这就是我感到困惑的地方。
      • 在这种情况下,我倾向于支持 nlohmann 的回答,他指出成员变量的类型不完整。
      【解决方案3】:

      我认为问题在于您尝试在类Component 中拥有一个类型为std::optional&lt;std::vector&lt;Component&gt;&gt; 的成员,而std::optional 不适用于不完整的类型。将其替换为 std::unique_ptr&lt;std::vector&lt;Component&gt;&gt; 应该可以。

      【讨论】:

      • 当我尝试实现它时会吐出error: no matching function for call to ‘nlohmann::basic_json&lt;&gt;::get_to(std::shared_ptr&lt;std::vector&lt;Component&gt; &gt;&amp;) const’,我还需要一些方法来指示该字段不存在,而不仅仅是null,否则 DIScord API 会向我发送错误。我刚刚意识到使用adl_serializer 可能根本不起作用,嗯。
      • 是的,也许我对使用内置宏感到乐观,因为我有一些字段需要省略,一些需要为空,还有一些需要两者兼而有之。跨度>
      • (当然,使用智能指针需要额外的代码。但核心应该是不完整类型。这也是我们不能在类json中拥有std::unordered_map&lt;string, json&gt;的原因.
      • 在那种情况下,即使我排除了nlohmann_json_j.at("components").get_to(nlohmann_json_t.components);,它似乎也无法编译,但它确实
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      相关资源
      最近更新 更多