Skip to content

Response Deserialization

Don Kackman edited this page Feb 7, 2015 · 6 revisions

Deserialization Conventions

Dynamically Typed Responses

By default any of the supported http verb methods (get, post, put, patch, delete) expects the response, if any, to be a JSON string and will return it deserialized as a dynamic object (ExpandoObject).

using (dynamic google = new DynamicRestClient("https://www.googleapis.com/"))
{
    dynamic bucket = await google.storage.v1.b("uspto-pair").get();
    Assert.IsNotNull(bucket);
    Assert.AreEqual(bucket.name, "uspto-pair");
}    

It is possible to retrieve the response in a few other formats.

Statically Typed Responses

If it is desired that a JSON response be deserialized as a static type, that type can be passed to a verb invocation as an unnamed argument.

public class Bucket
{
    public string kind { get; set; }
    public string id { get; set; }
    public string selfLink { get; set; }
    public string name { get; set; }
    public DateTime timeCreated { get; set; }
    public int metageneration { get; set; }
    public string location { get; set; }
    public string storageClass { get; set; }
    public string etag { get; set; }
}

public async Task DeserializeToStaticType()
{
    using (dynamic google = new DynamicRestClient("https://www.googleapis.com/"))
    {
        Bucket bucket= await google.storage.v1.b("uspto-pair").get(typeof(Bucket));
        Assert.IsNotNull(bucket);
        Assert.AreEqual(bucket.name, "uspto-pair");
    }
}

For both dynamic and static deserialization Json.Net is used. An instance of its JsonSerializationSettings can also be passed to control the deserialization.

Getting the Response Stream, string or bytes

It is possible to retrieve the content as a raw string, a byte array or the underlying response stream, bypassing deserialization altogether. This is done by passing the typeof ``Stream, string, or byte[], respectively, to the invocation. If the response Stream is returned, it is the responsibility of the caller to dispose it.

using (dynamic google = new DynamicRestClient("https://www.google.com/"))
{
    byte[] content = await google.get(typeof(byte[]));

    Assert.IsNotNull(content);
    Assert.IsTrue(content.Length > 0);

    var s = Encoding.UTF8.GetString(content);
    Assert.IsFalse(string.IsNullOrEmpty(s));
    Assert.IsTrue(s.StartsWith("<!doctype html>"));
}

Getting the HttpResponseMessage

If it is necessary to inspect the HttpResponseMessage in more detail than just the content it is also possible to retrieve it directly by passing that type to an invocation. In all other cases the DynamicRestClient ensures that the response message is successful before returning the response content. If the response message is returned, success is not checked before returning. It is therefore the responsibility of the caller to check for success if appropriate as well as disposing the response object.

using (dynamic google = new DynamicRestClient("https://www.google.com/"))
using (HttpResponseMessage response = await google.get(typeof(HttpResponseMessage)))
{
    Assert.IsNotNull(response);
    // this makes it possible to inspect the response for more information than just the content
    Assert.AreEqual("OK", response.ReasonPhrase);

    // when asking for the complete response object the client does not check status
    Assert.IsTrue(response.IsSuccessStatusCode);

    var content = await response.Content.ReadAsStringAsync();

    Assert.IsNotNull(content);
    Assert.IsTrue(content.StartsWith("<!doctype html>"));
}

Clone this wiki locally