Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ streaming based JSON parser for TCP sockets [closed]

I've been looking for some time for a way to send JSON over a tcp socket and to de-serialize it into an object. The problem that I'm having is that when my message is too big, TCP chunks it. By doing this, my clients are receiving only a piece of the JSON and the parser sends that part and the following as malformed JSONs.

I'm trying to look for a tool that will allow me to feed it with this partial views of my JSON. The first time, my buffer contains only a partial JSON, I send it to the parser and it returns something that indicates that the JSON is not finished. Next time, when I append the new information to the buffer, the parser continues from the last point of where it was and finds the end of the JSON message (Or waits in case is needed and the JSON is coming in more than two chunks) and return something with the information of that JSON content, and removes it from the buffer, where the next information coming form the stream will be appended.

So far, I found that some other ideas but for other languages (Java in this one: Is there a streaming API for JSON?)

like image 209
Flashito Avatar asked May 23 '18 10:05

Flashito


Video Answer


2 Answers

I once had a similar problem.

To fix it, I determined the maximum size of one chunk and split my message manually to a lower size before sending it. Also attached an index and max index to the "manual chunk" to make an assembly on the receiver side.

Not sure if this is the best approach but it did the job

like image 110
MauriceRandomNumber Avatar answered Oct 10 '22 01:10

MauriceRandomNumber


You have some alternatives... One is to read data until some special sequence of characters (for example https uses \r\n\r\n). If you use boost C++ library you can look at boost::asio::async_read_until or boost::asio::read_until. Or you can implement you read routine that stops with a sentinel value.

The second alternative is to have a header file of your packet... in the header, you can specify for example the length of the message. So you must first read the header and then the whole body.

like image 24
Elvis Dukaj Avatar answered Oct 09 '22 23:10

Elvis Dukaj