views:

94

answers:

1

Hi

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?

Let me know Regards

Albert

+1  A: 

If you implementing some custom protocol you could send a marker to indicate the current object end so that the client can deserialize it and add it to a resulting list. Another option is to read the whole stream and deserialize the entire array at one go which could of course consume more memory.

Darin Dimitrov
I meant without sending any special markers, just the JSON objects.
aattia
JSON deserializers need the whole JSON string in order to successfully perform deserialization, so if you want to achieve this you either need a marker or wait for the whole stream to be read. You could try implementing some heuristics such as counting the number of opening `{` and match the corresponding closing `}` in order to determine where the current object ends but this might not be an easy task that works for any JSON string.
Darin Dimitrov
JSON.NET needs to do something similar when reading from a TextStream, If I parse a JSON Document from a TextStream there is no marker telling JSON.NET when the document ends.So, just like JSON.NET parses a document from a textstream is there a way to do the same for a networkstream, since both are streams?
aattia