RICHARD FAWCETT'S WEBSITE

ABOUT     CONTACT     RSS


2 August 2013
ServiceStack JSON Deserialization vs DataContractJsonSerializer

I used to have this horrid piece of code to deserialize a license stored as JSON:

var deserializer = new DataContractJsonSerializer(typeof(License));
using (var stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(licenseJSON)))
{
    theLicense = (License)deserializer.ReadObject(stream);
    stream.Close();
}

Along came ServiceStack.Text, and now I have this beautiful one-liner:

theLicense = JsonSerializer.DeserializeFromString<License>(licenseJSON);

Or using an extension method ServiceStack provides results in even clearer code:

theLicense = licenseJSON.FromJson<License>();

The simplicity and elegance of this just blows me away. What were Microsoft thinking when they came up with the DataContractJsonSerializer?