2

Tweetsharp and AgFx – Windows Phone 7

Being a big fan of AgFx for any online data retrieval, I was exited to see Bjorn Kuiper his blog post about using Tweetsharp together with AgFx in a WP7 app. You can read all about it here… it shows you how to display tweets from a specific users’ timeline.

But like many blog posts, most of the time you’ll have to tweak them to work in your own scenario. So also with this one, I needed to manipulate some code to get my case working. Best to start off, is downloading Bjorn’s example here.

What I wanted to achieve, was to show a list of tweets done by a Search, also through the use of Tweetsharp and AgFx of course. But what I soon found out was that the code of Bjorn uses the class TwitterStatus that contains a tweet, in my case the returning items are of type TwitterSearchStatus. They are actually not that different, but I ran into some serialization/deserializations issues.

So what was my problem… when you get your search results back from Tweetsharp you want to serialize them so that you can use the AgFx feature. In Bjorn’s example, he strips the Entities from the TwitterStatus before serializing. Found in the Execute method of the TwitterLoadRequest class.

 

But like I stated before I’m not working with TwitterStatus but with TwitterSearchStatus and it seems that performing the same action has no effect and I was still getting problems with the Entities property when Deserializing.

So how did I solve this, well I used Json.net from James Newton-King, also available as a NuGet here. So, instead of using the .Net DataContractJsonSerializer like Bjorn, we use the JsonConvert.SerializeObject. This gives us following code that replaces the one show above:

What you will notice is that I’m doing a special kind of Serialization… using a DynamicContractResolver! This is needed to manipulate the Json serialization, because if we don’t do anything, we’ll still have an output Json string containing the Entities properties.

So to fix this, you’ll need to interact when Json.Net tries to convert your class to Json, how to do this is is by creating your own class that inherits from DefaultContractResolver.

 

As you can see, Json.net finds properties to serialize, so he will pass the CreateProperties method, what we do here is use Linq to filter out the Entities, resulting in a total Json outpot string containing serialized TwitterSearchStatus objects without any Entities property.

The only thing left to do is Deserialize everything back in the Deserialize method of your AgFx DataLoader class.

 

So now you’ll be getting TwitterSearchStatus objects back from AgFx that you can use in your WP7 Views! Happy coding…

Depechie

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *