Added overload to ViewComponent.Json to accept JsonSerializerSettings

This commit is contained in:
Ajay Bhargav Baaskaran 2015-05-13 15:06:32 -07:00
parent 7767251dad
commit 39642761b1
4 changed files with 77 additions and 2 deletions

View File

@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Newtonsoft.Json;
namespace Microsoft.AspNet.Mvc
{
@ -17,8 +18,19 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <param name="value">The value to format as JSON text.</param>
public JsonViewComponentResult(object value)
: this(value, formatter: null)
{
}
/// <summary>
/// Initializes a new <see cref="JsonViewComponentResult"/>.
/// </summary>
/// <param name="value">The value to format as JSON text.</param>
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by
/// the formatter.</param>
public JsonViewComponentResult(object value, [NotNull] JsonSerializerSettings serializerSettings)
: this(value, new JsonOutputFormatter(serializerSettings))
{
Value = value;
}
/// <summary>

View File

@ -8,6 +8,7 @@ using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
using Newtonsoft.Json;
namespace Microsoft.AspNet.Mvc
{
@ -167,11 +168,25 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <param name="value">The value to output in JSON text.</param>
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
public JsonViewComponentResult Json([NotNull] object value)
public JsonViewComponentResult Json(object value)
{
return new JsonViewComponentResult(value);
}
/// <summary>
/// Returns a result which will render JSON text.
/// </summary>
/// <param name="value">The value to output in JSON text.</param>
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by
/// the formatter.</param>
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
/// <remarks>Callers should cache an instance of <see cref="JsonSerializerSettings"/> to avoid
/// recreating cached data with each call.</remarks>
public JsonViewComponentResult Json(object value, [NotNull] JsonSerializerSettings serializerSettings)
{
return new JsonViewComponentResult(value, serializerSettings);
}
/// <summary>
/// Returns a result which will render the partial view with name <c>&quot;Default&quot;</c>.
/// </summary>

View File

@ -1074,6 +1074,33 @@ namespace Microsoft.AspNet.Mvc.Test
Times.Once());
}
[Fact]
public void Controller_JsonWithParameterValueAndSerializerSettings_IDisposableObject_RegistersForDispose()
{
// Arrange
var mockHttpContext = new Mock<DefaultHttpContext>();
mockHttpContext.Setup(x => x.Response.OnResponseCompleted(It.IsAny<Action<object>>(), It.IsAny<object>()));
var controller = new TestableController()
{
ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor())
};
var input = new DisposableObject();
var serializerSettings = new JsonSerializerSettings();
// Act
var result = controller.Json(input, serializerSettings);
// Assert
Assert.IsType<JsonResult>(result);
Assert.Same(input, result.Value);
var jsonFormatter = result.Formatter as JsonOutputFormatter;
Assert.Same(serializerSettings, jsonFormatter.SerializerSettings);
mockHttpContext.Verify(
x => x.Response.OnResponseCompleted(It.IsAny<Action<object>>(), It.IsAny<object>()),
Times.Once());
}
public static IEnumerable<object[]> RedirectTestData
{
get

View File

@ -10,6 +10,7 @@ using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNet.Mvc
@ -36,6 +37,26 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("1", new StreamReader(buffer).ReadToEnd());
}
[Fact]
public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
{
// Arrange
var view = Mock.Of<IView>();
var buffer = new MemoryStream();
var viewComponentContext = GetViewComponentContext(view, buffer);
var serializerSettings = new JsonSerializerSettings();
serializerSettings.Formatting = Formatting.Indented;
var result = new JsonViewComponentResult("abc", serializerSettings);
// Act
result.Execute(viewComponentContext);
// Assert
Assert.Same(serializerSettings, result.Formatter.SerializerSettings);
}
[Fact]
public void Execute_FallsbackToServices_WhenNoJsonFormatterIsProvided()
{