简体   繁体   English

我怎样才能`nlohmann::json::get <std::shared_ptr<const t> &gt;`? </std::shared_ptr<const>

[英]How can I `nlohmann::json::get<std::shared_ptr<const T>>`?

I've defined the following serializer stack:我已经定义了以下序列化程序堆栈:

namespace discordpp {
using Snowflake = uint64_t;
}
namespace nlohmann {
template <> struct adl_serializer<discordpp::Snowflake> {
    static void to_json(json &j, const discordpp::Snowflake sf) {
        j = std::to_string(sf);
    }
    
    static void from_json(const json &j, discordpp::Snowflake sf) {
        std::istringstream(j.get<std::string>()) >> sf;
    }
};
template <typename T> struct adl_serializer<std::shared_ptr<T>> {
    static void from_json(json &j, std::shared_ptr<T> &ptr) {
        if (j.is_null()) {
            ptr == nullptr;
        } else {
            ptr = std::make_shared<T>(j.get<T>());
        }
    }
    static void to_json(json &j, const std::shared_ptr<T> &ptr) {
        if (ptr.get()) {
            j = *ptr;
        } else {
            j = nullptr;
        }
    }
};
template <typename T> struct adl_serializer<std::shared_ptr<const T>> {
    static void from_json(json &j, std::shared_ptr<const T> &ptr) {
        if (j.is_null()) {
            ptr == nullptr;
        } else {
            ptr = std::make_shared<const T>(j.get<T>());
        }
    }
    static void to_json(json &j, const std::shared_ptr<const T> &ptr) {
        if (ptr.get()) {
            j = *ptr;
        } else {
            j = nullptr;
        }
    }
};
template <typename T> struct adl_serializer<std::optional<T>> {
    static void to_json(json &j, const std::optional<T> &opt) {
        if (opt.has_value()) {
            j = nullptr;
        } else {
            j = *opt;
        }
    }

    static void from_json(const json &j, std::optional<T> &opt) {
        if (j.is_null()) {
            opt = std::nullopt;
        } else {
            opt = j.get<T>();
        }
    }
};
}

And I'm poking around with things like so:我正在四处寻找这样的事情:

class MessageIn : protected util::ObjectIn {
  public:
    ...
    opt<sptr<const Snowflake>> guild_id;
    ...
}
void from_json(const json &j, MessageIn *m) {
    ...
    j["guild_id"].get<Snowflake>();
    j["guild_id"].get<const Snowflake>();
    j["guild_id"].get<sptr<const Snowflake>>();
    m->guild_id = j["guild_id"].get<opt<sptr<const Snowflake>>>();
    ...
}

My compiler is throwing an error on the j["guild_id"].get<sptr<const Snowflake>>();我的编译器在j["guild_id"].get<sptr<const Snowflake>>();上抛出错误line with error: no matching function for call to 'nlohmann::basic_json<>::get<discordpp::sptr<const long unsigned int> >() const' .error: no matching function for call to 'nlohmann::basic_json<>::get<discordpp::sptr<const long unsigned int> >() const' Have I missed something?我错过了什么吗?

The std::shared_ptr<const T> synthesizer is not needed.不需要std::shared_ptr<const T>合成器。

In the from_json methods of both std::shared_ptr sythesizers the json parameter wasn't static.在两个std::shared_ptr合成器的from_json方法中, json参数不是 static。

Boy do I feel silly.男孩,我觉得自己很傻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换 const std::shared_ptr<const t> 进入 boost::shared_ptr<t></t></const> - convert const std::shared_ptr<const T> into boost::shared_ptr<T> 从std :: shared_ptr是否有隐式转换 <T> 到std :: shared_ptr <const T> ? - Is there an implicit conversion from std::shared_ptr<T> to std::shared_ptr<const T>? 传递一个 std::shared_ptr<t> 到采用 std::shared_ptr 的 function<const t> ?</const></t> - Passing a std::shared_ptr<T> to a function that takes a std::shared_ptr<const T>? C ++:std :: shared_ptr和有什么不一样 <T> 和std :: shared_ptr <T const> ? - C++: What is the difference between std::shared_ptr<T> and std::shared_ptr<T const>? 正在使用 std::vector&lt; std::shared_ptr<const T> &gt; 反模式? - Is using std::vector< std::shared_ptr<const T> > an antipattern? 为什么我可以将 0 转换为 std::shared_ptr<T> 但不是1? - Why can I convert 0 to an std::shared_ptr<T> but not 1? 如何将const std :: shared_ptr插入到std :: map中 - How to insert a const std::shared_ptr into a std::map 如何接受派生的`shared_ptr`到接受`T const&`的构造函数? - How can I accept a derived `shared_ptr` to a constructor accepting `T const &`? 如何中断此std :: shared_ptr参考周期? - How can I break this std::shared_ptr reference cycle? 如何创建一个到std :: vector的shared_ptr? - How can I create a shared_ptr to a std::vector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM