Posts Tagged ‘JSON’

DynamicJson and unit-testing JSON services

January 1, 2011

I’m currently messing with setting up unittests for a JSON/REST’ish service. My goal is having (or at least have a looke at having :)) a test suite that executes over HTTP in exatcly the same manner as an actual client. One advantages of this I think, will be the ability to execute the tests against a remote system, once the system is deployed.

Trying to figure out the best way to inspect the returned JSON data, I came across DynamicJson (http://dynamicjson.codeplex.com/). The library combines the new C# 4.0 dynamic features with a JSON parser to allow for what you see below:

[TestMethod]
public void GetList()
{
    var webClient = new WebClient();
    webClient.Encoding = UTF8Encoding.UTF8;
    webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

    var result = DynamicJson.Parse(webClient.DownloadString("http://localhost/WebSite/List/BE33A625-63F3-4014-85E7-D306F00A5A7E"));

    Assert.AreEqual("TestList", result.Title);
}

The library does contain a serializer as well, but it only support basic types (so GUID’s for instance are not supported). I find the JavaScriptSerializer much more usefull..