JsonTextReader gets stuck after reading a certain amount of lines

205 Views Asked by At

I'm trying to deserialize a large stream of Twitter Tweets.

Everything is going well at around the first 200 deserialized tweets, but after that, the Deserialize method gets stuck and never proceeds. If I leave it to work for a prolonged time, it eventually throws a System.Net.WebException: The operation has timed out on HttpWebResponse after a long time.

        var tweets = new List<Tweet>();
        JsonTextReader reader;
        using (var client = new WebClient())
        using (var stream = client.OpenRead(url))
        using (var streamReader = new StreamReader(stream))
        using (reader = new JsonTextReader(streamReader))
        {
            reader.SupportMultipleContent = true;
            var serializer = new JsonSerializer();

            while (reader.Read() && tweets.Count < 500)
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    tweets.Add(serializer.Deserialize<Tweet>(reader));
                }
            }
        }

Any ideas why would the JsonTextReader get stuck? The stream I am reading seems to continue returning data when consumed through the browser.

0

There are 0 best solutions below