Avoid throwing if serializer settings is null (#12207)
Also adds some docs that suggest what needs to be passed as an argument instance
This commit is contained in:
parent
7b56439ca1
commit
94edcbf65b
|
|
@ -2,8 +2,8 @@
|
||||||
// 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;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc.Core;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
|
@ -27,14 +27,17 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
|
/// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to format as JSON.</param>
|
/// <param name="value">The value to format as JSON.</param>
|
||||||
/// <param name="serializerSettings">The serializer settings to be used by the formatter.</param>
|
/// <param name="serializerSettings">
|
||||||
|
/// The serializer settings to be used by the formatter.
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
|
||||||
|
/// </para>
|
||||||
|
/// </param>
|
||||||
public JsonResult(object value, object serializerSettings)
|
public JsonResult(object value, object serializerSettings)
|
||||||
{
|
{
|
||||||
if (serializerSettings == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(serializerSettings));
|
|
||||||
}
|
|
||||||
|
|
||||||
Value = value;
|
Value = value;
|
||||||
SerializerSettings = serializerSettings;
|
SerializerSettings = serializerSettings;
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +49,12 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the serializer settings.
|
/// Gets or sets the serializer settings.
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
|
||||||
|
/// </para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public object SerializerSettings { get; set; }
|
public object SerializerSettings { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// 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;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
@ -298,7 +299,14 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// to JSON.
|
/// to JSON.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">The object to serialize.</param>
|
/// <param name="data">The object to serialize.</param>
|
||||||
/// <param name="serializerSettings">The serializer settings to be used by the formatter.</param>
|
/// <param name="serializerSettings">The serializer settings to be used by the formatter.
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
|
||||||
|
/// </para>
|
||||||
|
/// </param>
|
||||||
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
|
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
|
||||||
/// as JSON format for the response.</returns>
|
/// as JSON format for the response.</returns>
|
||||||
/// <remarks>Callers should cache an instance of serializer settings to avoid
|
/// <remarks>Callers should cache an instance of serializer settings to avoid
|
||||||
|
|
@ -306,11 +314,6 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
[NonAction]
|
[NonAction]
|
||||||
public virtual JsonResult Json(object data, object serializerSettings)
|
public virtual JsonResult Json(object data, object serializerSettings)
|
||||||
{
|
{
|
||||||
if (serializerSettings == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(serializerSettings));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new JsonResult(data, serializerSettings);
|
return new JsonResult(data, serializerSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue