Allow easily forward declaring nlohmann::json type #4692
GustavLindberg99
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We're writing a library with a class that, besides its main functionality, has methods that allow converting instances to and from JSON:
Some of our library users need the JSON constructor and the toJson method, and need them to take/return nlohmann::json objects directly so that they can be integrated into larger JSON structures. Other library users, however, don't use JSON at all and don't want to have to download the nlohmann json library to use our library.
This means that we don't want to include any headers from nlohmann json in our own headers. The best would be to instead only forward declare nlohmann::json in foo.hpp like this:
That way users who need the JSON constructor and the toJson method will have
#include <nlohmann/json.hpp>
in their own code anyway, and users who don't use JSON don't have to download the nlohmann json library at all. (Of course we need#include <nlohmann/json.hpp>
in foo.cpp, but that's not a problem since our users just have to use our pre-compiled binary .a/.lib file without worrying about how we compiled it).The problem is that the above solution doesn't work, since nlohmann::json isn't declared as a class but as a typedef of a very complicated template. I know that you have a json_fwd.hpp file, but that doesn't help in our case because if we use it users who don't use JSON will still have to download it to be able to use our library.
It would be nice if you could make nlohmann::json an actual class instead of a typedef to make it easy to forward declare it. I googled and found this, so instead of doing
using json = basic_json<>;
you could do something like this:Beta Was this translation helpful? Give feedback.
All reactions