-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Please post example of specialization for boost::filesystem #692
Comments
Did you put the struct into the |
I realized you pass the JSON value This may work: #include "src/json.hpp"
#include <boost/filesystem.hpp>
namespace nlohmann {
template<>
struct adl_serializer<boost::filesystem::path>
{
static void to_json(json& j, const boost::filesystem::path& path)
{
j = path.string();
}
static void from_json(const json& j, boost::filesystem::path& path)
{
path = j.get<std::string>();
}
};
}
int main()
{
...
} |
Thanks for the response! |
Hello, This means you specialized the You should specialize // JsonSpecializations.hpp
#include <json/json.hpp>
namespace nlohmann {
template<>
struct adl_serializer<boost::filesystem::path> {
/* ... */
};
} In your case, the library already instantiated the default I think that if you comment out your specialization, you will see a message saying:
|
Ok, if I comment out the section as you did, I get the same error about already instantiated/defined. Also, I had a separate file for this specialization of a third-party library, however I did also have So, there is indeed a slightly non-basic interaction in my case, because my custom classes have Below is one of those classes. Interestingly, I just discovered that when I added the ADL_Specialization, the specialization logic, the below fails to compile now. The question is, how does one organize specializations of both custom classes and third-party classes when the former depends on the latter. Do I have to aggregate all my custom class specialization methods into one big header along with the third-party ones? #ifndef GLOBAL_CONFIG_HPP_
#define GLOBAL_CONFIG_HPP_
#include <string>
#include <boost/filesystem.hpp>
#include <json.hpp>
using nlohmann::json;
namespace daemon
{
namespace config
{
namespace bfs = boost::filesystem;
struct global_config
{
std::string log_level;
bfs::path log_path;
bfs::path config_path;
};
void to_json(json& j, const global_config& gc)
{
j = json
{
{ "log_level", gc.log_level },
{ "log_path", gc.log_path },
{ "config_path", gc.config_path }
};
}
}
} |
Oh, I meant you needed to comment the whole specialization to see the I don't know how many third party classes you need to specialize As for your custom types that use third party types, you MUST include the header Finally, if you have to specialize |
Ok, finally got it working. It does seem it was a result of my lack of experience in C++. I did not realize I had to add the So, maybe it's worth updating the README documentation to explain that part about including the |
Yes I agree, the doc needs some clarifications for those use-cases! |
You gave an example for boost::optional which was very nice, however i spent some time trying to adapt with no success, here was my best guess.
Can you please advise?
The text was updated successfully, but these errors were encountered: