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

Injecting class during serialization #1584

Closed
pkartner opened this issue Apr 25, 2019 · 6 comments
Closed

Injecting class during serialization #1584

pkartner opened this issue Apr 25, 2019 · 6 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@pkartner
Copy link

pkartner commented Apr 25, 2019

I have a data class that I want to use during serialization to map a string value to something else.

static void from_json(const json& j, T& value) 
{
    // I want to use my data class here to map the value returned from json
    j.at("somevalue");
}

Is this currently possible with the library?

@stale
Copy link

stale bot commented May 25, 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 May 25, 2019
@nlohmann
Copy link
Owner

Did you try the approach described in https://github.com/nlohmann/json#arbitrary-types-conversions? Can you provide an example?

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 26, 2019
@pkartner
Copy link
Author

pkartner commented May 28, 2019

I'm not 100% sure what you mean. My prefered solution would be something like this.

void from_json(const json& j, person& p, const data& d) {
    auto value = j.at("name").get();
    p.name = d.get(value);
}

@stale
Copy link

stale bot commented Jun 27, 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 Jun 27, 2019
@nlohmann
Copy link
Owner

I'm afraid there is no API for this at the moment, I'd have no idea how to add it.

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 29, 2019
@stale
Copy link

stale bot commented Jul 29, 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 Jul 29, 2019
@stale stale bot closed this as completed Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question 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