As I’m working on finishing up the GetHuman app for Windows Phone 7 I am trying to make sure it loads fast and never has the user be forced to wait for data to get updated. To begin with I am working to make the main portion of the data update without any user interface slowdown. I am attempting to do this by downloading the data in the background and storing in locally. Also then if the user is in a situation without connectivity it will allow them to have at least one contact method for each company on their phone at any time and not break the basic functionality of the application.

The next time data is pulled is when a company is selected from the main list. At this point alternate contact methods, tips, and ratings are pulled for that individual company listing. To help ensure these options are loaded quickly I enlisted the use of Morten Nielsen’s GZipWebClient (NuGetblog postsource) and Json.NET.

On the blog Zvonko’s Playground I found this handy little excerpt on how to use the combo of Nielsen’s SharpGIS GZipWebClient and Json.NET to quickly grab the company details and store them for use in the app.

public void LoadJSON(Action callback, Action<Exception> error)
{
SharpGIS.GZipWebClient webClient = new SharpGIS.GZipWebClient();
webClient.DownloadStringCompleted += (p, q) =>
  {
    if (q.Error == null)
    {
      //do stuff
      callback();
    }
    else
    {
      //something went wrong
      error(q.Error);
    } 
  };
  webClient.DownloadStringAsync(new Uri(JsonUrl));
}

Note I’m using the Action delegates, as I needed a callback function to do the following – download a response, THEN navigate to next page and pass some parameters. Without the callback, the result fails, because the navigation executes before the string is downloaded and stored.

Now, what to do with the results? I guess I could use them as-is, but I figured I might need to use it later – the first idea was to store the results in .NET objects. For this, I’ve found a great online tool which creates .NET classes out of a JSON response – a real time saver (and works great!)

http://json2csharp.com/

Just paste the JSON URL or paste the response, and you’ll get a generated C# class to use with your application. Rename the RootObject to something else preferably, and work with the class.

Anyway, once you’ve done that, you can deserialize the received data pretty easily. Basically, in the code above, before the callback, do something like this:

var deserialized = JsonConvert.DeserializeObject<ClassToDeserializeTo>(q.Result);
_collection.Add(deserialized);

And that’s pretty much it. The data is deserialized to the ClassToDeserializeTo, and it can be accessed at will. If you need iterations, wrap it in a loop, the end result is the same. For storing your data, I would recommend using the ObservableCollection<T>, as it provides notifications if data stored in it is changed. It’s useful for working with WP7 – you can bind the collection to a ListBox, which will get automatically updated as items in the collection change.

via Handling JSON operations in .NET (Windows Phone 7) « Zvonko’s playground.

With GZip I hope to help reduce time it takes to download data both over WiFi and cellular connections. It appears that other’s have benefited from implementing this method. Jeff Wilcox shows a break down here of his performance increase he saw in his 4th & Mayor app from using GZip compression to handle his JSON requests: 

 

via Please ship your next Windows Phone app with GZip: speed requests 50-80% – Jeff Wilcox.

 

Combine that with these performance boosts that Json.NET is supposed to show over using the native deserialize function built into WP7:

 

via Json.NET – Codeplex.

 

 

Hopefully with the combination of these two packages I can get the speed boost of Json.Net and also GZip to make GetHuman for WP7 snappy fast.

Thanks Morten & James for providing these tools! You can follow Morten on Twitter (@dotMorten) or check out his blog (http://www.sharpgis.net/). You can follow James on Twittter (@JamesNK) or check out his blog (http://james.newtonking.com).