From 39642761b17030f284d3d5eb5b79ac6a34cad31f Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 13 May 2015 15:06:32 -0700 Subject: [PATCH] Added overload to ViewComponent.Json to accept JsonSerializerSettings --- .../ViewComponents/JsonViewComponentResult.cs | 14 +++++++++- .../ViewComponents/ViewComponent.cs | 17 +++++++++++- .../ControllerTests.cs | 27 +++++++++++++++++++ .../JsonViewComponentResultTest.cs | 21 +++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs index 895e85e695..688a36b917 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs @@ -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 /// /// The value to format as JSON text. public JsonViewComponentResult(object value) + : this(value, formatter: null) + { + } + + /// + /// Initializes a new . + /// + /// The value to format as JSON text. + /// The to be used by + /// the formatter. + public JsonViewComponentResult(object value, [NotNull] JsonSerializerSettings serializerSettings) + : this(value, new JsonOutputFormatter(serializerSettings)) { - Value = value; } /// diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs index 0e1184ea0a..21731b2819 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs @@ -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 /// /// The value to output in JSON text. /// A . - public JsonViewComponentResult Json([NotNull] object value) + public JsonViewComponentResult Json(object value) { return new JsonViewComponentResult(value); } + /// + /// Returns a result which will render JSON text. + /// + /// The value to output in JSON text. + /// The to be used by + /// the formatter. + /// A . + /// Callers should cache an instance of to avoid + /// recreating cached data with each call. + public JsonViewComponentResult Json(object value, [NotNull] JsonSerializerSettings serializerSettings) + { + return new JsonViewComponentResult(value, serializerSettings); + } + /// /// Returns a result which will render the partial view with name "Default". /// diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs index e60daa5f6d..813647ac56 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ControllerTests.cs @@ -1074,6 +1074,33 @@ namespace Microsoft.AspNet.Mvc.Test Times.Once()); } + [Fact] + public void Controller_JsonWithParameterValueAndSerializerSettings_IDisposableObject_RegistersForDispose() + { + // Arrange + var mockHttpContext = new Mock(); + mockHttpContext.Setup(x => x.Response.OnResponseCompleted(It.IsAny>(), It.IsAny())); + + 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(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>(), It.IsAny()), + Times.Once()); + } + public static IEnumerable RedirectTestData { get diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/JsonViewComponentResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/JsonViewComponentResultTest.cs index 817b7918f6..0fb37c79d4 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/JsonViewComponentResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/JsonViewComponentResultTest.cs @@ -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(); + 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() {