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

pointer to json parse vector #711

Closed
kessero opened this issue Aug 25, 2017 · 11 comments
Closed

pointer to json parse vector #711

kessero opened this issue Aug 25, 2017 · 11 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@kessero
Copy link

kessero commented Aug 25, 2017

My main:

int main(int argc, char *argv[])
{
  // read a JSON file
  std::stringstream ss;
  std::ifstream i("filee.json");
  json j_complete = json::parse(i);
  std::vector < int > data_send_to_LED;
  for (int i =0; i<j_complete["tablica"].size(); i++){
    data_send_to_LED.push_back(j_complete["tablica"][i].get<int>());
  }
  for (int i =0; i<data_send_to_LED.size(); i++){
    cout <<"data send: "<< data_send_to_LED[i]<<endl;
  }
  json j_vec(data_send_to_LED);
  long int pv = j_vec.get_ptr<json::number_integer_t*>();
  cout << j_vec.size() << endl;
  cout << j_vec << endl;
  cout << pv << endl;
...
n = write(sockfd,pv, j_vec.size()+1);

In general I want to send the contents of the file.json file to the server which accepts data in json format.
J_vec shows - [192,0,0,1,0,0,1,0,84,76,126]
Pv.size shows 11 elements and this is OK
pv shows 0
and n returns bad address?
I can skip the vector in the program and send the data directly form file.

@nlohmann
Copy link
Owner

long int pv = j_vec.get_ptr<json::number_integer_t*>(); is weird: you store a pointer to json::number_integer_t inside a long int variable.

@theodelrieu
Copy link
Contributor

First, you could use j_complete.at("tablica").get<std::vector<int>>() to avoid a loop, but to fix your problem, try to use j_vec.get_ptr<json::array_t*>().

@kessero
Copy link
Author

kessero commented Aug 25, 2017

Now:

std::vector < int > data_send_to_LED;
  for (int i =0; i<j_complete["tablica"].size(); i++){
    data_send_to_LED.push_back(j_complete["tablica"][i].get<int>());
  }
  //j_complete.at("tablica").get<std::vector<int>>();
  for (int i =0; i<data_send_to_LED.size(); i++){
    cout <<"data send: "<< data_send_to_LED[i]<<endl;
  }
  json j_vec(data_send_to_LED);
  auto pv = j_vec.get_ptr<json::array_t*>();
  cout << j_vec.size() << endl;
  cout << pv << endl;

I don't have errors in this program but server shows mi error:

what(): [json.exception.parse_error.101] parse error at 1: syntax error - invalid literal; last read: 'p'
pBg�� Cg

@nlohmann
Copy link
Owner

Your input to the parse function seems corrupted. Can you make sure you pass proper JSON?

@kessero
Copy link
Author

kessero commented Aug 25, 2017

I'm not sure what you mean but my json file looks like this:

{
  "tablica": [192, 0, 0, 1, 0, 0, 1, 0,84, 76, 126]
}

@nlohmann
Copy link
Owner

Well the parser exception says the first read character is p.

@kessero
Copy link
Author

kessero commented Aug 25, 2017

On server side bafore json error:

bufor: p��{:

So some ^%&(( are sending.

@gregmarr
Copy link
Contributor

As Niels and Theo have pointed out, it is most likely that pv is not of the proper data type. What does the receiving side think is the format of the data being received?

@gregmarr
Copy link
Contributor

If it's expecting json, you probably want:

int main(int argc, char *argv[])
{
  // read a JSON file
  std::ifstream i("filee.json");
  json j_complete = json::parse(i);
  json j_vec = j_complete["tablica"];
  cout << j_vec.size() << endl;
  cout << j_vec << endl;
  std::string json = j_vec.dump();
  n = write(sockfd, json.data(), json.size());

@nlohmann
Copy link
Owner

@kessero Any news on this?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Aug 27, 2017
@kessero
Copy link
Author

kessero commented Aug 29, 2017

Thnx gregmarr solve my problem i have to read what dump do in doc but works. Sorry for late response

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: please discuss please discuss the issue or vote for your favorite option labels Aug 29, 2017
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

4 participants