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

from_json for non default constructible class with dependency injection #1819

Closed
rvishna opened this issue Oct 29, 2019 · 4 comments
Closed
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@rvishna
Copy link

rvishna commented Oct 29, 2019

I have a class without a non default constructor like this:

class SomeStruct
{
public:
    SomeStruct() = delete;
    SomeStruct(std::shared_ptr<ContextData) : contextData_(contextData) { }
private:
    std::shared_ptr<ContextData> contextData_;
}

ContextData is a class that encapsulates dependencies, i.e., data I wish to shared between classes. In my main(), I would create a new ContextData object based on data from, say, a configuration file, and pass it around to classes that need this data.

Is there a way to implement from_json() for SomeStruct? I saw the example in the demo for a move constructible type, but I don't understand how I can capture additional data needed by the constructor of my class that is not coming from the json object argument to from_json().

@nlohmann
Copy link
Owner

nlohmann commented Nov 1, 2019

Your type needs to be move-constructible. Then you can follow the example from the documentation: /~https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Nov 1, 2019
@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

@rvishna
Copy link
Author

rvishna commented Nov 18, 2019

I conclude that for a struct to be json serializable, it must be default-constructible or move-constructible, which implies that the only way to achieve dependency injection is to do:

struct SomeStruct
{
    std::shared_ptr<DataStruct> dependencies_;
    void setData(std::shared_ptr<DataStruct> dependencies) 
    {
        dependencies_ = dependencies;
    }
};

instead of this:

struct SomeStruct
{
    std::shared_ptr<DataStruct> dependencies_;
    SomeStruct(std::shared_ptr<DataStruct> dependencies)
    : dependencies_(dependencies)
    {
    }
};

Is this correct? This is what I'm doing now. The downside is that between when the object of SomeStruct is constructed and when the client code decides to call setData(), some members of SomeStruct would have unassigned values. If this is the only way to achieve to_json() and from_json() of SomeStruct, I am happy to have the issue closed.

@stale
Copy link

stale bot commented Dec 18, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 18, 2019
@stale stale bot closed this as completed Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants