Skip to content
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

Enable forward declaration of json by making json a class instead of a using declaration #997

Closed
pgoodman opened this issue Mar 6, 2018 · 9 comments
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@pgoodman
Copy link

pgoodman commented Mar 6, 2018

I would like to be able to be able to declare the conversion operators in a header without having to #include the json.h file. I would then define the conversion operators in a source file, which actually includes json.h.

Here is an example of what I want do do, but can't:

namespace nlohmann {
class json;
void to_json(json &j, const MyType &blah);
void from_json(const json &j, MyType &blah);
}  // namespace nlohmann

What is stopping me is using json = basic_json<>;. A simple fix should be class json : public basic_json<> {};.

@theodelrieu
Copy link
Contributor

Making this change would break everything, but since version 3.1.0 you can use the multiple header version, to include the nlohmann/json_fwd.hpp, which does what you want :)

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 6, 2018
@nlohmann
Copy link
Owner

nlohmann commented Mar 7, 2018

@pgoodman Did you have the chance to test whether include/nlohmann/json_fwd.hpp helps?

@pgoodman
Copy link
Author

pgoodman commented Mar 8, 2018

I didn't test the multiple header approach because I wanted to put the single header version into my project at third_party/JSON/json.h, rather than duplicate the nlohmann directory structure. I might change this in the future, though.

@gregmarr
Copy link
Contributor

gregmarr commented Mar 8, 2018

I would think that you could put both the single file and the fwd file in your project.

@pgoodman
Copy link
Author

pgoodman commented Mar 8, 2018

this had not occurred to me ;-)

@pgoodman
Copy link
Author

pgoodman commented Mar 8, 2018

That didn't work for me.

@theodelrieu
Copy link
Contributor

Did you include json_fwd.hpp?

@nlohmann nlohmann added state: needs more info the author of the issue needs to provide more details and removed solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 12, 2018
@nlohmann
Copy link
Owner

@pgoodman Could you provide more information like a small example program or the concrete error message?

@nlohmann nlohmann added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 2, 2018
@stale stale bot closed this as completed Apr 9, 2018
@pasbi
Copy link

pasbi commented Jun 6, 2018

copying both json/include/nlohmann/json_fwd.hpp and json/single_include/nlohmann/json.hpp into my src/json/ directory enables me to use #include "json/json_fwd.hpp" whenever I need the declarations and #include "json/json.hpp" whenever definitions are required.

@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

5 participants