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.