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

Problem in converting string into JSON; Can't parse successfully. #1854

Closed
nhule11 opened this issue Nov 25, 2019 · 6 comments
Closed

Problem in converting string into JSON; Can't parse successfully. #1854

nhule11 opened this issue Nov 25, 2019 · 6 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@nhule11
Copy link

nhule11 commented Nov 25, 2019

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@pboettch
Copy link
Contributor

You should put your description into the comment/description field, not in the subject, it is unreadable.

@nhule11 nhule11 changed the title I'm receiving Json string from TCP protocol. I get some packets perfect but then I get an error " terminate called after throwing an instance of 'std::invalid_argument' what(): parse error - unexpected '"'; expected string literal Aborted " this is how I convert my char into string then into Json " std::string s = (string(buf, 0, bytesReceived)); json j = json::parse(s); cout << j.dump(4) << endl; " I'm a newbie to c++ and raspberrypi. I really appreciate if anyone can help. Problem in converting string into JSON; Can't parse successfully. Nov 26, 2019
@nhule11
Copy link
Author

nhule11 commented Nov 26, 2019

I'm receiving Json string from TCP protocol. I get some packets perfect but then I get an error
This is the error I get,
"terminate called after throwing an instance of 'std::invalid_argument' what(): parse error - unexpected '"'; expected string literal Aborted"

The string I want is here in the char "buf"
The total number of bytes are here "bytesReceived" :
std::string s = (string(buf, 0, bytesReceived));
Then I try to parse with this:));
json j = json::parse(s); ------> On this step I get the error.
I'm receiving the data continuously through tcpip.
This is how i output the the data:
cout << j.dump(4) << endl;

I receive few Json perfect. Then I get the error I mentioned above.

I'm a newbie to c++ and to raspberry pi. Any help will be really appreciated.

@nlohmann
Copy link
Owner

What is the exception saying?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 26, 2019
@nhule11
Copy link
Author

nhule11 commented Nov 26, 2019

I tried catching the exception, but it gives me error
" error: ‘exception’ in ‘using json = using json = class nlohmann::basic_json<>’ {aka ‘class nlohmann::basic_json<>’} does not name a type
catch (json::exception& e)
^~~~~~~~~
t.cpp:17:37: error: ‘e’ was not declared in this scope
std::cout << "message: " << e.what() << '\n'
"
This is the error I get,
terminate called after throwing an instance of 'std::invalid_argument'
what(): parse error - unexpected '"'; expected string literal
Aborted
I think i know what the problem is but don't know how to resolve it.

So I'm receiving 3 jason continuously one after the other, I receive few but it gives me this error in the middle of the 5th or 6th packet I don't know why it doesn't wait till it finds the close bracket '}'.

I don't know if it has to do something with the size of the buffer where I receive the tcpip,

char buf[4096];
int bytesReceived = recv(clientSocket, buf, 4096, 0);

Sorry if I'm not clear explaining.

Thank you for the quick response.

@nhule11
Copy link
Author

nhule11 commented Nov 26, 2019

It has nothing to do with the size of the buffer. The longest json I have is less than 4096.

Also I didn't mention is I'm getting ton of warnings,

This is the first one,

/usr/include/c++/8/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, bool, long long int, long long unsigned int, double, std::allocator, nlohmann::adl_serializer>}; _Tp = nlohmann::basic_jso n<>; _Alloc = std::allocator<nlohmann::basic_json<> >]’:
/usr/include/c++/8/bits/vector.tcc:109:4: note: parameter passing for argument of type ‘__gnu_cxx::__normal_iterator<nlohmann ::basic_json<>*, std::vector<nlohmann::basic_json<>, std::allocator<nlohmann::basic_json<> > > >’ changed in GCC 7.1
_M_realloc_insert(end(), std::forward<_Args>(__args)...);

@nhule11
Copy link
Author

nhule11 commented Nov 27, 2019

@nlohmann You can close this issue.

I think the problem is I'm not able to receive a complete string from my application through tcpip after few successful json strings.

If I ignore that one incomplete json string my program works fine. I don't think it has anything to do with your library. It works perfect ones I get a complete json string to process.

Thank you for your response.
:)

@nhule11 nhule11 closed this as completed Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants