[Fixes #2715] Using default SerializerSettings from MvcJsonOptions
This commit is contained in:
parent
15cb2ce709
commit
d2d4d1d1d3
|
|
@ -3,8 +3,9 @@
|
||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
using Microsoft.Framework.OptionsModel;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
|
@ -27,8 +28,8 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to format as JSON.</param>
|
/// <param name="value">The value to format as JSON.</param>
|
||||||
public JsonResult(object value)
|
public JsonResult(object value)
|
||||||
: this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
|
|
||||||
{
|
{
|
||||||
|
Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -86,12 +87,23 @@ namespace Microsoft.AspNet.Mvc
|
||||||
response.StatusCode = StatusCode.Value;
|
response.StatusCode = StatusCode.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var serializerSettings = _serializerSettings;
|
||||||
|
if (serializerSettings == null)
|
||||||
|
{
|
||||||
|
serializerSettings = context
|
||||||
|
.HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetRequiredService<IOptions<MvcJsonOptions>>()
|
||||||
|
.Options
|
||||||
|
.SerializerSettings;
|
||||||
|
}
|
||||||
|
|
||||||
using (var writer = new HttpResponseStreamWriter(response.Body, contentTypeHeader.Encoding))
|
using (var writer = new HttpResponseStreamWriter(response.Body, contentTypeHeader.Encoding))
|
||||||
{
|
{
|
||||||
using (var jsonWriter = new JsonTextWriter(writer))
|
using (var jsonWriter = new JsonTextWriter(writer))
|
||||||
{
|
{
|
||||||
jsonWriter.CloseOutput = false;
|
jsonWriter.CloseOutput = false;
|
||||||
var jsonSerializer = JsonSerializer.Create(_serializerSettings);
|
var jsonSerializer = JsonSerializer.Create(serializerSettings);
|
||||||
jsonSerializer.Serialize(jsonWriter, Value);
|
jsonSerializer.Serialize(jsonWriter, Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
using Microsoft.Framework.OptionsModel;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
|
@ -20,8 +21,8 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to format as JSON text.</param>
|
/// <param name="value">The value to format as JSON text.</param>
|
||||||
public JsonViewComponentResult(object value)
|
public JsonViewComponentResult(object value)
|
||||||
: this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
|
|
||||||
{
|
{
|
||||||
|
Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -47,10 +48,22 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
|
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
|
||||||
public void Execute([NotNull] ViewComponentContext context)
|
public void Execute([NotNull] ViewComponentContext context)
|
||||||
{
|
{
|
||||||
|
var serializerSettings = _serializerSettings;
|
||||||
|
if (serializerSettings == null)
|
||||||
|
{
|
||||||
|
serializerSettings = context
|
||||||
|
.ViewContext
|
||||||
|
.HttpContext
|
||||||
|
.RequestServices
|
||||||
|
.GetRequiredService<IOptions<MvcJsonOptions>>()
|
||||||
|
.Options
|
||||||
|
.SerializerSettings;
|
||||||
|
}
|
||||||
|
|
||||||
using (var jsonWriter = new JsonTextWriter(context.Writer))
|
using (var jsonWriter = new JsonTextWriter(context.Writer))
|
||||||
{
|
{
|
||||||
jsonWriter.CloseOutput = false;
|
jsonWriter.CloseOutput = false;
|
||||||
var jsonSerializer = JsonSerializer.Create(_serializerSettings);
|
var jsonSerializer = JsonSerializer.Create(serializerSettings);
|
||||||
jsonSerializer.Serialize(jsonWriter, Value);
|
jsonSerializer.Serialize(jsonWriter, Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Internal;
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -109,15 +111,18 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
|
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpContext GetHttpContext()
|
private static HttpContext GetHttpContext()
|
||||||
{
|
{
|
||||||
var httpContext = new DefaultHttpContext();
|
var httpContext = new DefaultHttpContext();
|
||||||
httpContext.Response.Body = new MemoryStream();
|
httpContext.Response.Body = new MemoryStream();
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
services.AddOptions();
|
||||||
|
httpContext.RequestServices = services.BuildServiceProvider();
|
||||||
|
|
||||||
return httpContext;
|
return httpContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] GetWrittenBytes(HttpContext context)
|
private static byte[] GetWrittenBytes(HttpContext context)
|
||||||
{
|
{
|
||||||
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
||||||
return Assert.IsType<MemoryStream>(context.Response.Body).ToArray();
|
return Assert.IsType<MemoryStream>(context.Response.Body).ToArray();
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,13 @@
|
||||||
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Internal;
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Microsoft.AspNet.Mvc.ViewComponents;
|
using Microsoft.AspNet.Mvc.ViewComponents;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -17,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public class JsonViewComponentResultTest
|
public class JsonViewComponentResultTest
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
|
public void Execute_UsesSerializer_WithSpecifiedSerializerSettings()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var view = Mock.Of<IView>();
|
var view = Mock.Of<IView>();
|
||||||
|
|
@ -37,9 +39,27 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Assert.Equal("{\r\n \"foo\": \"abcd\"\r\n}", Encoding.UTF8.GetString(buffer.ToArray()));
|
Assert.Equal("{\r\n \"foo\": \"abcd\"\r\n}", Encoding.UTF8.GetString(buffer.ToArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Execute_UsesSerializerSettingsFromOptions_IfNotProvided()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var view = Mock.Of<IView>();
|
||||||
|
var buffer = new MemoryStream();
|
||||||
|
var viewComponentContext = GetViewComponentContext(view, buffer);
|
||||||
|
|
||||||
|
var result = new JsonViewComponentResult(new { foo = "abcd" });
|
||||||
|
viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
result.Execute(viewComponentContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("{\"foo\":\"abcd\"}", Encoding.UTF8.GetString(buffer.ToArray()));
|
||||||
|
}
|
||||||
|
|
||||||
private static ViewComponentContext GetViewComponentContext(IView view, Stream stream)
|
private static ViewComponentContext GetViewComponentContext(IView view, Stream stream)
|
||||||
{
|
{
|
||||||
var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
|
||||||
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
|
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
|
||||||
var viewContext = new ViewContext(
|
var viewContext = new ViewContext(
|
||||||
actionContext,
|
actionContext,
|
||||||
|
|
@ -59,5 +79,15 @@ namespace Microsoft.AspNet.Mvc
|
||||||
var viewComponentContext = new ViewComponentContext(viewComponentDescriptor, new object[0], viewContext, writer);
|
var viewComponentContext = new ViewComponentContext(viewComponentDescriptor, new object[0], viewContext, writer);
|
||||||
return viewComponentContext;
|
return viewComponentContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static HttpContext GetHttpContext()
|
||||||
|
{
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
services.AddOptions();
|
||||||
|
httpContext.RequestServices = services.BuildServiceProvider();
|
||||||
|
|
||||||
|
return httpContext;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue