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

create json instance from raw json (unsigned char*) #431

Closed
CheyenneForbes opened this issue Jan 20, 2017 · 12 comments
Closed

create json instance from raw json (unsigned char*) #431

CheyenneForbes opened this issue Jan 20, 2017 · 12 comments

Comments

@CheyenneForbes
Copy link

CheyenneForbes commented Jan 20, 2017

I cant get nor set values if I try :

basic_ostream BOS = get_json_from_server(); // the json response from a server
json BOS_json = BOS;

If I am doing something wrong, whats the right way to create a json instance from raw json received from a webserver as basic_ostream?

@CheyenneForbes
Copy link
Author

Can anyone help me?

@gregmarr
Copy link
Contributor

You need to have an istream, not an ostream.

@CheyenneForbes
Copy link
Author

What can I do if the http client I am using returns the response body as a "basic_ostream"?

@gregmarr
Copy link
Contributor

That seems to be an odd thing for it to return. What client are you using?

@CheyenneForbes
Copy link
Author

CheyenneForbes commented Jan 21, 2017

it was made by a friend, I was reading wrong, it actually returns "unsigned char*" and not "basic_ostream", my apologies.

but it doesnt work still, how can I create a nlohmann::json instance from json (not Serialized) returned from a server?

@CheyenneForbes CheyenneForbes changed the title create json instance from basic_ostream create json instance from raw json (unsigned char*) Jan 21, 2017
@gregmarr
Copy link
Contributor

json::parse()

@CheyenneForbes
Copy link
Author

that gives "Segmentation fault"

@gregmarr
Copy link
Contributor

Where is the segmentation fault (stack trace)? It could be a problem with the string itself. Do you get the same fault if you use strlen() on the string?

@nlohmann
Copy link
Owner

@CheyenneForbes As @gregmarr said, the parse function can handle char *. Can you please add more precise description of your errors?

@nlohmann
Copy link
Owner

@CheyenneForbes Any update?

@CheyenneForbes
Copy link
Author

Hey, sorry for the delayed response, the client was actually sending a POST request instead of a GET request so the json instance was actually "empty bytes" when I was trying to add/get data, I thought the issue was with the json instance because when I opened the link in the browser it worked. I recommend that anyone using the json header file and plan to receive the json data from "outside" should do so in a try{}catch(){} block when application crashes due to "empty" data arent "acceptable", for example when being used in a web server

@nlohmann
Copy link
Owner

Thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants