Alright, next round in this mini-series.
WCF in .NET 3.5 contains a new serializer for handling with JSON-based data. With the so called DataContractJsonSerializer we can serialize and deserialize JSON packets based on DataContract descriptions. The new serializer can work with the DataContract idea and concepts but obviously takes into account that XML (the infoset) and JSON are two totally different beasts - not just syntax-wise but especially semantically, in terms of underlying and supported concepts.
For the sake of demonstrating how it work and how to use it, I have come up with a simple sample. This is a DataContract of Google's SearchMash R&D site JSON format. SearchMash is a nice example of a simple-to-use REST-style API which leverages JSON as its data representation format. I hand to hand-craft these DataContracts based on a sample query issued to the SearchMash endpoint.
[DataContract]
public class GoogleSearchResult
{
[DataMember(Name="estimatedCount")]
public string EstimatedCount { get; set; }
[DataMember(Name = "moreResults")]
public bool MoreResults { get; set; }
[DataMember(Name = "event")]
public string Event { get; set; }
[DataMember(Name = "query")]
public GoogleSearchQuery Query { get; set; }
[DataMember(Name = "chat")]
public string Chat { get; set; }
[DataMember(Name = "results")]
public List<GoogleSearchResultEntry> Results { get; set; }
}
[DataContract]
public class GoogleSearchQuery
{
[DataMember(Name = "prefix")]
public string Prefix { get; set; }
[DataMember(Name = "terms")]
public string Terms { get; set; }
}
[DataContract]
public class GoogleSearchResultEntry
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "snippet")]
public string Snippet { get; set; }
[DataMember(Name = "site")]
public string Site { get; set; }
[DataMember(Name = "rawUrl")]
public string RawUrl { get; set; }
[DataMember(Name = "url")]
public string Url { get; set; }
[DataMember(Name = "cacheUrl")]
public string CacheUrl { get; set; }
[DataMember(Name = "displayUrl")]
public string DisplayUrl { get; set; }
}
Following is the code snippet crafting the URI by using a URI template (also new in .NET 3.5) and finally issuing the query by simply using plain WebRequest/WebResponse.
The search result is passed into the new serializer and then (for the sake of demo :)) bound to a Windows Forms control.
...
GoogleSearch search = new GoogleSearch(textBox1.Text);
Uri uri = search.SearchUri;
WebRequest wrq = WebRequest.Create(uri);
WebResponse wrs = wrq.GetResponse();
DataContractJsonSerializer djs =
new DataContractJsonSerializer(
typeof(GoogleSearchResult));
GoogleSearchResult data =
(GoogleSearchResult)djs.ReadObject(
wrs.GetResponseStream());
resultsBindingSource.DataSource = data.Results;
...
class GoogleSearch
{
private string q = String.Empty;
public Uri SearchUri { get; set; }
public GoogleSearch(string query)
{
q = query;
Uri baseSearchUri =
new Uri("http://www.searchmash.com/results");
UriTemplate template = new UriTemplate("{query}");
SearchUri = template.BindByPosition(
baseSearchUri, q);
}
}
That's it: JSON support based on WCF's DataContract concept in .NET 3.5.
Comments