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 to convert STL Vector (of user defined type) to Json #2072

Closed
rakeshhegde opened this issue Apr 29, 2020 · 2 comments
Closed

How to convert STL Vector (of user defined type) to Json #2072

rakeshhegde opened this issue Apr 29, 2020 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@rakeshhegde
Copy link

Here is what I am trying to do,

using json = nlohmann::json;
typedef struct xyz {
int array[1024];
} xyz_t;

int main()
{
vector<vector<vector<xyz_t>>> abc;

/*......
Assume abc is filled here
...... */

json j;
json["data"]["values"] = abc; /* This gives 'error: no match for ‘operator=’ */

/* How do overload =operator in above case or is there any better way to acheive same? */

return 0;
}

@nlohmann
Copy link
Owner

You need to define a conversion function, like

void to_json(json& j, const xyz_t& xyz)
{
    j["array"] = xyz.array;
}

Then you get the rest (like vectors of xyz_t) for free. See https://github.com/nlohmann/json#arbitrary-types-conversions for more information.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 29, 2020
@rakeshhegde
Copy link
Author

Thank you @nlohmann for the prompt response.
I tried this solution & it works. Closing this ticket.

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
Projects
None yet
Development

No branches or pull requests

2 participants