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

Writing an array into a nlohmann::json object #1578

Closed
somequery opened this issue Apr 22, 2019 · 3 comments
Closed

Writing an array into a nlohmann::json object #1578

somequery opened this issue Apr 22, 2019 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@somequery
Copy link

Hi,

I can create an array field with the following syntax, used inside a finction:

void MyF() {
   unsigned char iv[] = { 0x00, 0x11 };
   nlohmann::json items;
   items["IV"] = iv;
}

However, if I pass iv as a function argument, than the code doesn't compile:

void MyF(unsigned char iv[2]) {
  nlohmann::json items;
  items["IV"] = iv;
}

I get an error 'No match for operator ='

What syntax I should use in the second case? Also, how the json deals with the array size, as in this case the array is not \0 terminated. (contains binary data).

Thanks for help.

@pboettch
Copy link
Contributor

unsigned char iv[2] is seen behind the scene as pointer to allocated memory and as you rightly wrote has no information about the size.

There are several possibilities to achieve what you want to do but the best solution depends on your real problem. If iv has always 2 elements just use each of them directly.

Otherwise make it the argument a const std::vector<unsigned char> &. Then JSON will be able to convert it directly to an array (because std::vector contains the number of elements).

Please elaborate with more details to get a better answer.

@jaredgrubb
Copy link
Contributor

Passing C-style arrays as arguments to functions doesn't really work well (due to legacy C rules that require that arrays decay to pointers, so you're actually dealing with "MyF(unsigned char *iv)" which has no size information and doesn't even look like an "array").

As @pboettch says, you could pass it in as a std::vector. You could also use a std::array. As a last resort -- an ugly solution that I would not recommend -- you can pass the array by C++ reference, and then you'll also get what you expect (I'm not telling the syntax for this because it's awful and ugly and I really recommend doing it another way :)

@somequery
Copy link
Author

Thanks for the response!
The reason why I have it this way is that the array has a const number of element, so it is always well known parameter. Also it has no overhead like other more complex types, like a Vector. I found out a worked solution like this:

for (int i = 0; i < IV_LENGTH; i++) { items["IV_ValueHex"].push_back(iv[i]); }

or
for (int i = 0; i < IV_LENGTH; i++) { items["IV_ValueHex"][i] = iv[i]; }

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 24, 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
Projects
None yet
Development

No branches or pull requests

4 participants