You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Need help, I cannot get NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE to work with smart pointers.
See this small sample code to demonstrate the problem:
#include <iostream>
#include <memory>
#include "json.hpp"
using json = nlohmann::json;
namespace nlohmann {
template <typename T>
struct adl_serializer<std::shared_ptr<T>> {
static void to_json(json& j, const std::shared_ptr<T>& opt) {
if (opt.get()) {
j = *opt;
}
else {
j = nullptr;
}
}
static void from_json(json& j, std::shared_ptr<T>& ptr) {
if (j.is_null()) {
ptr.reset();
}
else {
ptr = std::make_shared<T>(j.get<T>());
}
}
};
}
typedef struct tag_Foo
{
unsigned int a;
unsigned int b;
} Foo;
typedef struct tagBar
{
std::shared_ptr<Foo> myFoo;
} Bar;
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Foo
, a
, b
);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Bar
, myFoo
);
int main()
{
std::shared_ptr<int> a = std::make_shared<int>(1);
std::shared_ptr<int> b;
json j = { a, b };
Bar bar;
json j2 = bar;
}
The following code produce following compilition error on VS2019 version 16.11.0):
1>DebugJsonSerialization.cpp
1>C:\Users\xxxxx\source\repos\DebugJsonSerialization\DebugJsonSerialization\DebugJsonSerialization.cpp(53,1): error C2672: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to': no matching overloaded function found
1>C:\Users\xxxxx\source\repos\DebugJsonSerialization\DebugJsonSerialization\DebugJsonSerialization.cpp(53,1): error C2783: 'ValueType &nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to(ValueType &) noexcept() const': could not deduce template argument for '__formal'
1>C:\Users\xxxxx\source\repos\DebugJsonSerialization\DebugJsonSerialization\json.hpp(20658): message : see declaration of 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<uint8_t>>>::get_to'
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
Need help, I cannot get
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
to work with smart pointers.See this small sample code to demonstrate the problem:
The following code produce following compilition error on VS2019 version 16.11.0):
What I am doing wrong, can anyone help ?
Beta Was this translation helpful? Give feedback.
All reactions