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

Unpack list of integers to a std::vector<int> #1460

Closed
p-i- opened this issue Jan 27, 2019 · 5 comments
Closed

Unpack list of integers to a std::vector<int> #1460

p-i- opened this issue Jan 27, 2019 · 5 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@p-i-
Copy link

p-i- commented Jan 27, 2019

Is there any way to unpack:

{
    "my_list" : [1,2,3]
}

into a std:vector<int> ?

I can't find any mention, and std::vector<int> v = j["my_list"]; fails, as does j["my_list"].get<std::vector<int>>()

(Crosslinked to https://stackoverflow.com/questions/54389742/use-nlohmann-json-to-unpack-list-of-integers-to-a-stdvectorint)

@nlohmann
Copy link
Owner

I cannot reproduce the problem. This code works:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {
    json j = R"(
    {
        "my_list" : [1,2,3]
    }
    )"_json;

    auto v = j["my_list"].get<std::vector<int>>();
    
    for (auto i : v)
    {
        std::cout << i << std::endl;
    }
}

Output:

1
2
3

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

Which version of the library are you using? What is the exact error message you encounter?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 27, 2019
@p-i-
Copy link
Author

p-i- commented Jan 28, 2019

My apologies. It works on my end also. I failed to isolate a testcase and my JSON was malformed.

Might I make a couple of suggestions?

Firstly, in the documentation, it would be nice to present packing and unpacking hand-in-hand.
e.g. as you have presented above. Initially when I read the documentation, it was easy to access how to pack things, but really hard to figure out how to un-pack them.

Maybe an "Unpacking Examples" section?

Also, I wonder if It might be possible to overload assignment= operator on std::vector<T>, which would allow auto v = j["my_list"];

@theodelrieu
Copy link
Contributor

Please look at #958 for more information.

@stale
Copy link

stale bot commented Feb 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 Feb 27, 2019
@stale stale bot closed this as completed Mar 6, 2019
@nlohmann nlohmann removed state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated labels Mar 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants