aspnet / AspNetWebStack

ASP.NET MVC 5.x, Web API 2.x, and Web Pages 3.x (not ASP.NET Core)
Other
858 stars 354 forks source link

Inject custom json serializer in JsonResult #216

Closed gelambert closed 5 years ago

gelambert commented 5 years ago

It would be nice to give possibility to inject a custom json serializer in JsonResult.

I find the current one (JavaScriptSerializer) sometimes limited (for example when i want to camel case the properties of my object).

It would result in something like below :

if (Data != null)
{
    if (CustomSerializer != null) {
        response.Write(CustomSerializer(Data));
    } else { // Current code untouched
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        if (MaxJsonLength.HasValue)
        {
            serializer.MaxJsonLength = MaxJsonLength.Value;
        }
        if (RecursionLimit.HasValue)
        {
            serializer.RecursionLimit = RecursionLimit.Value;
        }
        response.Write(serializer.Serialize(Data));
    }
}

Could this feature be considered ? Currently, I am using a class which inherits from JsonResult to achieve what I want.

dougbu commented 5 years ago

Thank you for your feedback. We do not plan to add additional extensibility to JsonResult.

Creating your own ActionResult is exactly what we recommend. (Not sure if deriving from JsonResult helps but that's up to you.) We're closing this issue as the behaviour discussed is by design.