1

I am using POCO 1.6.0. I am trying to write a service that receives a JSON message on a raw socket and parses it.

However, the only way that POCO's parser seems to work is to take an entire string as input, and either return the parsed result, or throw a "Syntax error" exception.

So this means I have to re-parse the whole message each time a new byte arrives on the socket; and also there is no way of distinguishing between an actual syntax error versus it just being an incomplete message so far.

The parseChar function looks nice but it is private. Is there any way to have the parser parse some of a message and remain in that state so that I can resume parsing by passing more data?

Also, is there any way to distinguish actual syntax errors from incomplete messages (and preferably get feedback about the exact nature of the syntax error).

Pseudocode:

Poco::JSON::Parser parser;
std::string input_buffer;

for(;;)
{
    // (append byte(s) from socket into input_buffer)
    // (return failure if this read times out after 5 seconds)

    parser.reset();
    try
    {
        parser.parse(input_buffer);
        break;
    } 
    catch(Poco::Exception &e)
    {
        // (abort, but we don't know if data incomplete or data malformed
    }
}

Note: I realize that this problem could be mooted by having the client frame the entire message as described in this thread, however I was hoping to make things as simple as possible for the client by just having a correctly-formed packet be sufficient to define a frame (method 5 of that question).

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365

1 Answers1

3

There currently is no way to do either of the things you'd like to do. However, they are both reasonable requests and doable, so this was put on the TODO list for one of the upcoming releases.

Alex
  • 5,159
  • 4
  • 25
  • 33
  • Thanks. I heard you mention a different parser "Poco::Web::JSON" in a past answer on here, did that ever materialize? – M.M Mar 30 '15 at 20:24
  • yes [it did](https://github.com/pocoproject/poco/commit/87d955ecde8e2bc2843047b54ce33018dab1329b). The original implementatiojn was somewhat naive, based on Poco::StringTokenizer with very poor performance. – Alex Mar 30 '15 at 20:27