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

Open
RichardD2 opened this issue Mar 4, 2022 · 0 comments
Open

Workaround for JSON serializer configuration #79

RichardD2 opened this issue Mar 4, 2022 · 0 comments
Assignees
Labels
bug Decribes a bug or an invalid behavior from the library
Milestone

Comments

@RichardD2
Copy link

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

@ALMMa ALMMa self-assigned this Apr 14, 2022
@ALMMa ALMMa added bug Decribes a bug or an invalid behavior from the library enhancement Describes an enhancement or an existing feature and removed enhancement Describes an enhancement or an existing feature labels Apr 14, 2022
@ALMMa ALMMa added this to the 3.0.0 milestone Apr 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Decribes a bug or an invalid behavior from the library
Projects
None yet
Development

No branches or pull requests

2 participants