Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Workaround for JSON serializer configuration #79

@RichardD2

Description

@RichardD2

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. 😊

Metadata

Metadata

Assignees

Labels

bugDecribes a bug or an invalid behavior from the library

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions