Parse JSON from a NetworkStream using JSON.Net

Jul 14, 2011 at 12:15 PM
Edited Jul 18, 2011 at 6:49 AM

Hi james,

I am sending serialized objects via a NetWorkStream to another computer, on the receiving end I would like to deserialize these objects.

I will be sending many consecutive objects, when I am receiving data via the NetworkStream, how do I know when the first JSON Document ended in order to have JSON.NET Parse the document from the received string?

Or better yet, is there any way to have Json.NET read directly from the NetworkStream and Deserialize/Parse the resulting JSON document?

            var bufferSize = 512;
            var ms = new MemoryStream();
            var inbuffer = new byte[bufferSize];
            while (true)
            {
                var read = this.reader.Read(inbuffer, 0, inbuffer.Length);
              
                    if (read > 1)
                    {
                        if (inbuffer[inbuffer - 1] == '}' )
                        {
                            ms.Write(inbuffer, 0, read);
                            this.ParseData(ms.ToArray());
                            break;
                        }
                    }               
                 ms.Write(inbuffer, 0, read);
                }

above code is correct to find the end of the json object , is there any way to do it?

Thanks & Regards

Reddeppa