Skip to content

Avoid throwing if serializer settings is null #12207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Mvc/Mvc.Core/src/JsonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -27,14 +27,17 @@ public JsonResult(object value)
/// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
/// </summary>
/// <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)
{
if (serializerSettings == null)
{
throw new ArgumentNullException(nameof(serializerSettings));
}

Value = value;
SerializerSettings = serializerSettings;
}
Expand All @@ -46,6 +49,12 @@ public JsonResult(object value, object serializerSettings)

/// <summary>
/// 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>
public object SerializerSettings { get; set; }

Expand Down
15 changes: 9 additions & 6 deletions src/Mvc/Mvc.ViewFeatures/src/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
Expand Down Expand Up @@ -298,19 +299,21 @@ public virtual JsonResult Json(object data)
/// to JSON.
/// </summary>
/// <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"/>
/// as JSON format for the response.</returns>
/// <remarks>Callers should cache an instance of serializer settings to avoid
/// recreating cached data with each call.</remarks>
[NonAction]
public virtual JsonResult Json(object data, object serializerSettings)
{
if (serializerSettings == null)
{
throw new ArgumentNullException(nameof(serializerSettings));
}

return new JsonResult(data, serializerSettings);
}

Expand Down