-
Notifications
You must be signed in to change notification settings - Fork 135
Workaround for JSON serializer configuration #79
Description
Problem
This library currently provides no way to configure the JSON serializer used to serialize the data. This leads to issues such as #48 / #73, and can also cause problems if you need to use a global JsonConverter rather than decorating a specific property.
For example, trying to return data from an API which includes an IPAddress property would consistently result in:
Error getting value from 'ScopeId' on 'System.Net.IPAddress'.
...
at DataTables.AspNet.Mvc5.DataTablesResponse.SerializeData(Object data)
Workaround
To resolve the problem, I created and registered a custom JsonConverter for the IDataTablesResponse object. In my base controller class, I override the Json method to use JSON.NET with my global custom converters.
DataTables.AspNet Json Configuration gist
protected override JsonResult Json(
object data,
string contentType,
Encoding contentEncoding,
JsonRequestBehavior behavior)
=> new JsonNetResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
JsonSettings =
{
Converters =
{
DataTablesResponseConverter.Instance,
IPAddressConverter.Instance,
}
}
};It was then simply a case of replacing:
return new DataTablesJsonResult(response, JsonRequestBehavior.AllowGet);with:
return Json(response, JsonRequestBehavior.AllowGet);Hopefully this might help someone else until the issue is fixed. 😊