Skip to content

Commit 94edcbf

Browse files
authored
Avoid throwing if serializer settings is null (#12207)
Also adds some docs that suggest what needs to be passed as an argument instance
1 parent 7b56439 commit 94edcbf

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/Mvc/Mvc.Core/src/JsonResult.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Text.Json;
56
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Mvc.Core;
77
using Microsoft.AspNetCore.Mvc.Infrastructure;
88
using Microsoft.Extensions.DependencyInjection;
99

@@ -27,14 +27,17 @@ public JsonResult(object value)
2727
/// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
2828
/// </summary>
2929
/// <param name="value">The value to format as JSON.</param>
30-
/// <param name="serializerSettings">The serializer settings to be used by the formatter.</param>
30+
/// <param name="serializerSettings">
31+
/// The serializer settings to be used by the formatter.
32+
/// <para>
33+
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />.
34+
/// </para>
35+
/// <para>
36+
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
37+
/// </para>
38+
/// </param>
3139
public JsonResult(object value, object serializerSettings)
3240
{
33-
if (serializerSettings == null)
34-
{
35-
throw new ArgumentNullException(nameof(serializerSettings));
36-
}
37-
3841
Value = value;
3942
SerializerSettings = serializerSettings;
4043
}
@@ -46,6 +49,12 @@ public JsonResult(object value, object serializerSettings)
4649

4750
/// <summary>
4851
/// Gets or sets the serializer settings.
52+
/// <para>
53+
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />
54+
/// </para>
55+
/// <para>
56+
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
57+
/// </para>
4958
/// </summary>
5059
public object SerializerSettings { get; set; }
5160

src/Mvc/Mvc.ViewFeatures/src/Controller.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Text.Json;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Mvc.Filters;
78
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -298,19 +299,21 @@ public virtual JsonResult Json(object data)
298299
/// to JSON.
299300
/// </summary>
300301
/// <param name="data">The object to serialize.</param>
301-
/// <param name="serializerSettings">The serializer settings to be used by the formatter.</param>
302+
/// <param name="serializerSettings">The serializer settings to be used by the formatter.
303+
/// <para>
304+
/// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />.
305+
/// </para>
306+
/// <para>
307+
/// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
308+
/// </para>
309+
/// </param>
302310
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
303311
/// as JSON format for the response.</returns>
304312
/// <remarks>Callers should cache an instance of serializer settings to avoid
305313
/// recreating cached data with each call.</remarks>
306314
[NonAction]
307315
public virtual JsonResult Json(object data, object serializerSettings)
308316
{
309-
if (serializerSettings == null)
310-
{
311-
throw new ArgumentNullException(nameof(serializerSettings));
312-
}
313-
314317
return new JsonResult(data, serializerSettings);
315318
}
316319

0 commit comments

Comments
 (0)