Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Parse more than one valid json obj ?

Anonymous
2010-12-17
2013-06-12

  • Anonymous
    2010-12-17

    Hi all,

    I need to integrate libjson in a existing client-server application. I have to read "messages" from a socket and parse them. the problem is that I may read more than one valid message at time.
    For example I could receive:

    {
        "RootA":"Value in parent node",
        "ChildNode":
        {
            "ChildA":"String Value",
            "ChildB":42
        }
    }
    {
        "Asd":"Value"
    }

    or even:

    {
        "RootA":"Value in parent node",
        "ChildNode":
        {
            "ChildA":"String Value",
            "ChildB":42
        }
    }
    {
        "As

    Now… I need to decode the first valid obj and save the remaining chars for the future ( in the next read() I'll read the remaining chars).

    Is it possibile? Now I have a different protocol (not json) and for each "message" I have and header describing the size and the content of the coming message.
    Any helps?
    Tnx
    Mario

     
  • Currently that is not possible, however.  I will be releasing a new version of libjson in a few days if you're able to wait.  I do have plans for a JSONStream object that will fit your purposes exactly.  You are not the first person to require something like this and as far as I know, no JSON library has any support for this even though JSON is usually used for talking across servers.

    If you'd like to start doing your porting now, I can tell you how the JSONStream object will work. 
    You'll create one of these string objects and register a callback with it.  You will then stream text into it.  Whenever the stream completes a JSON node, it will hit the callback, supplying the new json node.

     
  • I have finished the streaming abilities.  The new libjson will be out in a day or two, after it runs through all of it's unit tests.  You will use it like this:

    void callback(JSONNODE * node){
        // work on this node, or save a copy of it somewhere
    }

    JSONSTREAM *stream = json_new_stream(callback);
    json_stream_push(stream, "partial json");
    json_delete_stream(stream);