-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
From Version 2.1.1 to 3.6.1 serialize std::set #1676
Comments
It is not only the std::set. Its a problem with serialiize std containers. |
The example is incomplete (there is code missing, and there are syntax errors). Could you please provide a small example that does compile with 2.1.1? |
Sorry. Complete example:
|
This code compiles with Xcode 10.3 with the
I am not sure why this code would have compiled in earlier versions. #include "json.hpp"
#include <set>
#include <iostream>
using nlohmann::json;
using namespace std;
namespace pdx
{
using nlohmann::json;
using namespace std;
//enum class CyclicErrorAction
//{
// None,
// Stop,
// StopAll
//};
//
//NLOHMANN_JSON_SERIALIZE_ENUM( CyclicErrorAction,
//{
// { CyclicErrorAction::None, "None"},
// { CyclicErrorAction::Stop, "Stop"},
// { CyclicErrorAction::StopAll, "StopAll"},
//})
class MetaData
{
public:
string Name;
friend bool operator<(const MetaData& a, const MetaData& b) { return a.Name < b.Name; }
};
inline void to_json(json& j, const MetaData& metaData)
{
j["name"] = metaData.Name;
}
inline void from_json(const json& j, MetaData& metaData)
{
metaData.Name = j.at("name").get<decltype(metaData.Name)>();
}
class Settings
{
public:
//CyclicErrorAction OnCyclicError = CyclicErrorAction::None;
bool ShowAdditionalTrace = false;
std::string Source;
std::set<MetaData> Clients{};
};
inline void to_json(json& j, const Settings& settings)
{
j = json
{
//{ "onCyclicError", settings.OnCyclicError },
{ "showAdditionalTrace", settings.ShowAdditionalTrace },
{ "clients", settings.Clients },
{ "source", settings.Source },
};
}
inline void from_json(const json& j, Settings& settings)
{
//settings.OnCyclicError = j.at("onCyclicError").get<decltype(settings.OnCyclicError)>();
settings.ShowAdditionalTrace = j.at("showAdditionalTrace").get<decltype(settings.ShowAdditionalTrace)>();
settings.Source = j.at("source").get<decltype(settings.Source)>();
auto clients = j.at("clients").get<std::vector<MetaData>>();
for (const auto& client : clients)
{
settings.Clients.insert(client);
}
}
}
using namespace pdx;
int main()
{
std::cout << "Start!\n";
Settings settings {};
const auto j = json(settings);
const auto s = j.get<Settings>();
} |
Hi, oh so the order was the only problem. 😢 Thanks for your quick help. |
I upgraded to version 3.6.1 until this I serialized a std::set in this way
Now I get the errors:
Error C2440: 'initializing': cannot convert from 'initializer list' to 'nlohmann::json' (142)
Error C2672: 'nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer::get': no matching overloaded function found (160)
The text was updated successfully, but these errors were encountered: