JsonConvert.Import() on a JsonTextReader over a NetworkStream

172 views
Skip to first unread message

drewpvogel

unread,
Aug 17, 2010, 4:39:20 PM8/17/10
to Jayrock
I have a client/server application that uses the .Net TcpListener and
TcpClient classes. It currently uses a line based protocol. I would
like to convert it to use JSON objects as the basis of the protocol.
To this end I tried to do:

Stream stream = client.GetStream();
StreamReader streamReader = new StreamReader(stream);
JsonTextReader jsonReader = new JsonTextReader(streamReader);

The first call to JsonConvert.Import(jsonReader) succeeds. The
underlying stream is then at EOF. Then another message comes in, moves
the EOF for the stream. However JsonConvert.Import throws an exception
with the message "Unexpected EOF". So it seems as if JsonTextReader
caches the EOF value of the underlying stream. Can anyone confirm this
for me?

Atif Aziz

unread,
Aug 18, 2010, 1:56:16 AM8/18/10
to jay...@googlegroups.com
So it seems as if JsonTextReader
caches the EOF value of the underlying stream. Can anyone confirm this
for me?
 
It doesn't do that. Once the JsonTextReader enter the EOF state, it remains there. If you want to continue to read another, disjoint, JSON data from the underlying stream then you need to create a new JsonTextReader instance over the same stream. You cannot rewind an existing JsonTextReader into the BOF state.

- Atif
 

--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To post to this group, send email to jay...@googlegroups.com.
To unsubscribe from this group, send email to jayrock+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jayrock?hl=en.


drewpvogel

unread,
Aug 19, 2010, 12:39:49 PM8/19/10
to Jayrock
Thanks for clearing this up for me.


On Aug 18, 12:56 am, Atif Aziz <aziza...@gmail.com> wrote:
> > So it seems as if JsonTextReader
> > caches the EOF value of the underlying stream. Can anyone confirm this
> > for me?
>
> It doesn't do that. Once the JsonTextReader enter the EOF state, it remains
> there. If you want to continue to read another, disjoint, JSON data from the
> underlying stream then you need to create a new JsonTextReader instance over
> the same stream. You cannot rewind an existing JsonTextReader into the BOF
> state.
>
> - Atif
>
> On Tue, Aug 17, 2010 at 10:39 PM, drewpvogel <drewpvo...@gmail.com> wrote:
> > I have a client/server application that uses the .Net TcpListener and
> > TcpClient classes. It currently uses a line based protocol. I would
> > like to convert it to use JSON objects as the basis of the protocol.
> > To this end I tried to do:
>
> > Stream stream = client.GetStream();
> > StreamReader streamReader = new StreamReader(stream);
> > JsonTextReader jsonReader = new JsonTextReader(streamReader);
>
> > The first call to JsonConvert.Import(jsonReader) succeeds. The
> > underlying stream is then at EOF. Then another message comes in, moves
> > the EOF for the stream. However JsonConvert.Import throws an exception
> > with the message "Unexpected EOF". So it seems as if JsonTextReader
> > caches the EOF value of the underlying stream. Can anyone confirm this
> > for me?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Jayrock" group.
> > To post to this group, send email to jay...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > jayrock+u...@googlegroups.com<jayrock%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages