Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing TimeSpan using JSON.net and .net WebApi

I have an ASP.NET WebApi project that utilizes Entity Framework 4.1. I am attempting to return an entity and leveraging JSON.net for deserialization. All is working well however when attempting to deserialize entities with TimeSpan properties I run accross the following exception:

Could not cast or convert from System.String to System.TimeSpan.

Looking at the stack trace reveals:

JsonSerializationException: Error converting value "PT17H9M43.1671969S" to type 'System.TimeSpan'. Path 'TimeIn', line 1, position 890.

The output from response.Content.ReadAsStringAsync().Result is:

{"CreatedDate":"/Date(1329851383157-0500)/","ServicerUserId":6,"TimeIn":"PT17H9M43.1671969S","TimeOut":"PT17H28M43.1671969S"}

Before using JSON.net I have successfully deserialized using System.Runtime.Serialization.JsonDataContractJsonSerializer however would prefer to not use this approach.

Code used to get entity from WebApi

public T Get<T>(object id)
{
    using (var httpClient = NewHttpClient())
    {
        var response = httpClient.GetAsync(_endpoint + id.ToString()).Result;
        return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result);
    }
}

Code used to in WebApi controller to return entity:

// GET /api/services/5
public Service Get(Guid id)
{
    var entry = db.Services.Find(id);
    if (entry == null)
        throw new HttpResponseException(HttpStatusCode.NotFound);
    return entry;
}

Any ideas on how to deserialize TimeSpan's would be greatly appreciated.

like image 950
Jakkwylde Avatar asked May 11 '12 14:05

Jakkwylde


1 Answers

The solution was in configuring the WebApi project to leverage JSON.net for serialization by default in the global.asax Application_Start event as referenced at http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx

 // Create Json.Net formatter serializing DateTime using the ISO 8601 format
            JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
            serializerSettings.Converters.Add(new IsoDateTimeConverter());
            GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);
like image 50
Jakkwylde Avatar answered Oct 12 '22 10:10

Jakkwylde