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

How do I forward nlohmann::json declaration? #899

Closed
aquacrew opened this issue Jan 3, 2018 · 3 comments
Closed

How do I forward nlohmann::json declaration? #899

aquacrew opened this issue Jan 3, 2018 · 3 comments

Comments

@aquacrew
Copy link

aquacrew commented Jan 3, 2018

Hello everybody,

I'm having a question - let me describe the scenario:

I plan to use JSON for Modern C++ to serialize objects used in a library. Other projects using this library should have no knowledge about the method used to save data. So it should be possible to replace the mechanismn by something else in the future without any change of code on the client side. Therefor I don't want to include json.hpp in my library header files.
But forward declaration failed - maybe you can help me to do it the right way. I tried

namespace nlohmann {
  class json;
}

Error: using typedef-name 'using json = class nlohmann::basic_json<>' after 'class'

Then I tried

namespace nlohmann {
  template<class T> class basic_json;
  using json = basic_json<>;
}

Error: wrong number of template arguments (0, should be 1)

I'm aiming to create an abstract template, which can be specialized in classes to be serialized.

namespace MyTest {
  template<class T> class JSON {
  protected:
    virtual void Serialize(nlohmann::json&, const T&) const = 0;
    virtual void Deserialize(const nlohmann::json&, T&)     = 0;
  };
}

Then I want to use it this way:

namespace MyTest {
  class Attribute : public JSON<Attribute> {
  private:
    virtual void                  Serialize(nlohmann::json&, const Attribute&) const;
    virtual void                  Deserialize(const nlohmann::json&, Attribute&);
  public:
    virtual void                  Serialize() const;
    virtual void                  Deserialize();
  };
}

I would be happy to have an answer for my question or any suggestions how to achieve my goal. Mybe you could add info about forward declaration to the description.

@gregmarr
Copy link
Contributor

gregmarr commented Jan 3, 2018

See #700 and #823

@aquacrew
Copy link
Author

aquacrew commented Jan 3, 2018

This is not was I was hoping for, but thanks for the answer anyway.

@aquacrew aquacrew closed this as completed Jan 3, 2018
@gregmarr
Copy link
Contributor

gregmarr commented Jan 3, 2018

@aquacrew until such time as the library does it itself, you should be able to copy the forward declaration from #700 into your own code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants