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

Converting to array not working #843

Closed
hsdk123 opened this issue Nov 25, 2017 · 7 comments
Closed

Converting to array not working #843

hsdk123 opened this issue Nov 25, 2017 · 7 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@hsdk123
Copy link

hsdk123 commented Nov 25, 2017

Converting from an std array to json works fine.
However, the other way round gives errors.

json j;
std::array<int, 4> ar = {1, 1, 1, 1};
j = ar; // works fine
ar = j; // breaks

I'm given the error that 'insert' doesn't exist for array.
Considering we're given j.size(), I believe this shouldn't be something that should be breaking.
(since I can already work around this by creating a loop that adds individual elements)

@nlohmann
Copy link
Owner

Duplicates #553

@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label Nov 25, 2017
@nlohmann
Copy link
Owner

On the other hand: a conversion that checks the bounds and throws an exception in case the array does not match the JSON‘s size would be better here.

@nlohmann nlohmann added kind: enhancement/improvement and removed solution: duplicate the issue is a duplicate; refer to the linked issue instead labels Nov 25, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Nov 25, 2017
@hsdk123
Copy link
Author

hsdk123 commented Nov 25, 2017

@nlohmann
Thanks for the quick reply - yup, checking bounds and throwing exceptions if outside, sound like an implementation that would be helpful and functional.

@nlohmann
Copy link
Owner

Are you using the develop version? There, https://github.com/nlohmann/json/blob/develop/src/json.hpp#L1235 implements the conversion:

template<typename BasicJsonType, typename T, std::size_t N>
void from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr, priority_tag<2> /*unused*/)
{
    for (std::size_t i = 0; i < N; ++i)
    {
        arr[i] = j.at(i).template get<T>();
    }
}

Up to N elements are copied to the std::array. If the JSON array is too short, an exception is raised. If the std::array is shorter than the JSON array, no further values are copied.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Nov 25, 2017
@nlohmann nlohmann removed this from the Release 3.0.0 milestone Nov 25, 2017
@hsdk123
Copy link
Author

hsdk123 commented Nov 28, 2017

@nlohmann
I'm using the latest 'release' - I'm currently trying to use the library for production code, but it's going to be difficult to get use permission for code being marked as being in the develop branch.

I see the final release was in February, are there plans on a nearby official release?

@nlohmann
Copy link
Owner

I hope to get the 3.0.0 release out this year.

@nlohmann
Copy link
Owner

I'm closing this issue as there is nothing I can do here right now.

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

2 participants