Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.net and incremental processing of the twitter streaming API

I'm trying to make use of the Twitter streaming API in an incremental fashion to process root objects one by one and return each one via a callback asynchronously. I've seen implementations counting the number of { vs } and waiting for the numbers to balance at 0 then processing that chunk of data although to me this really just didn't feel safe.

My plan was to make use of a json.net JsonTextReader and use TokenType and Depth to do this job. This is working well in that it is a very reliable way of splitting the read at the end of each object, I just cannot figure out how to get that data into the Deserialize method. The Deserialize method does accept a JsonTextReader as an argument but that processes from that point onwards (it's a forward only stream after all) so I'm stuck. There is also a DeserializeObjectAsync method available but that only accepts a string.

var jsonSerializer = new JsonSerializer();
using (StreamReader reader = new StreamReader(_response.GetResponseStream()))
{
    using (var jsonReader = new JsonTextReader(reader))
    {
        while (jsonReader.Read())
        {
            if (jsonReader.Depth == 0 && jsonReader.TokenType == JsonToken.EndObject)
            {

                //newTweetCallback(jsonSerializer.Deserialize<Tweet>([Some magic here. Help!]));
            }
        }
    }
}

I'm hoping I'm just missing something obvious as a result of staring at the particular application for too long.

like image 557
Hawxby Avatar asked Nov 26 '25 14:11

Hawxby


1 Answers

Actually, instead of handling the depth yourself, you can pass the partially used JsonTextReader to Deserialize, which will not read further than to the end token:

while (jsonReader.Read())
{
    if (jsonReader.TokenType == JsonToken.StartObject)
    {
        newTweetCallback(jsonSerializer.Deserialize<Tweet>(jsonReader));
        // Debug.Assert(jsonReader.TokenType == JsonToken.EndObject);
    }
}

So there's no need to read ahead yourself.

like image 165
sebug Avatar answered Nov 29 '25 04:11

sebug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!