Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Add a Controller.Json(object, JsonSerializerSettings) overload #2506

Closed
JamesNK opened this issue May 6, 2015 · 9 comments
Closed

Add a Controller.Json(object, JsonSerializerSettings) overload #2506

JamesNK opened this issue May 6, 2015 · 9 comments

Comments

@JamesNK
Copy link
Member

JamesNK commented May 6, 2015

Right now JSON settings can only be defined once at an application level on the JsonOutputFormatter. It would be useful to be able to pass a JsonSerializerSettings argument to the Controller.Json method to override the JSON settings on a per result basis, e.g. a developer might want indented JSON, or to use a specific JsonConverter, for only one action in their application.

Current method:

/// <summary>
/// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object
/// to JSON.
/// </summary>
/// <param name="data">The object to serialize.</param>
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
/// to JSON format for the response.</returns>
[NonAction]
public virtual JsonResult Json(object data)
{
var disposableValue = data as IDisposable;
if (disposableValue != null)
{
Response.OnResponseCompleted(_ => disposableValue.Dispose(), state: null);
}
return new JsonResult(data);
}

@davidfowl
Copy link
Member

That migh actually be pretty nice

@rynowak
Copy link
Member

rynowak commented May 6, 2015

Would be pretty easy for us to add. Should probably do because WebAPI had something similar:

protected internal JsonResult<T> Json<T>(T content, JsonSerializerSettings serializerSettings)

@henkmollema
Copy link
Contributor

Using JsonSerializerSettings will create a hard dependency on Json.NET, is that desired?

@JamesNK
Copy link
Member Author

JamesNK commented May 6, 2015

@henkmollema That overload doesn't stop the underlying JSON formatter being something other than Json.NET - another serializer can ignore that class or read settings from JsonSerializerSettings and map them to its own functionality. To use WebAPI as a similar example, just because WebAPI knew about Json.NET didn't mean you couldn't replace the serializer with SimpleJson. Many people wrote their own WebAPI JSON formatters.

@danroth27 danroth27 added this to the 6.0.0-beta5 milestone May 6, 2015
@henkmollema
Copy link
Contributor

@JamesNK good point. Was just curious because ASP.NET 5 is all about abstractions and loose coupling. Figured a using Newtonsoft.Json in the Controller class might be a bit odd.

@rynowak
Copy link
Member

rynowak commented May 7, 2015

@henkmollema - we're pretty fully-bought into Json.Net at this point. You could use another serializer with MVC/WebAPI if you wanted to, but we don't plan to show an experience where Json.Net isn't a required dependency.

@henkmollema
Copy link
Contributor

@rynowak I see, thanks! 😄

@nicholasyin
Copy link

Was this only added to Core, and not to MVC 5?

@ajaybhargavb
Copy link
Contributor

@nicholasyin yes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants