Skip to content

Commit c9db22c

Browse files
committed
Make MVC's JsonOptions inherit settings from Http's JsonOptions
1 parent 4ff2671 commit c9db22c

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ internal static void AddMvcCoreServices(IServiceCollection services)
141141
//
142142
// Options
143143
//
144+
services.TryAddEnumerable(
145+
ServiceDescriptor.Transient<IOptionsFactory<JsonOptions>, JsonOptionsFactory>());
144146
services.TryAddEnumerable(
145147
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcCoreMvcOptionsSetup>());
146148
services.TryAddEnumerable(

src/Mvc/Mvc.Core/src/JsonOptions.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,32 @@ public class JsonOptions
1212
/// Gets the <see cref="System.Text.Json.JsonSerializerOptions"/> used by <see cref="SystemTextJsonInputFormatter"/> and
1313
/// <see cref="SystemTextJsonOutputFormatter"/>.
1414
/// </summary>
15-
public JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions
15+
public JsonSerializerOptions JsonSerializerOptions { get; }
16+
17+
public JsonOptions()
1618
{
17-
// Limit the object graph we'll consume to a fixed depth. This prevents stackoverflow exceptions
18-
// from deserialization errors that might occur from deeply nested objects.
19-
// This value is the same for model binding and Json.Net's serialization.
20-
MaxDepth = MvcOptions.DefaultMaxModelBindingRecursionDepth,
19+
JsonSerializerOptions = new JsonSerializerOptions
20+
{
21+
// Limit the object graph we'll consume to a fixed depth. This prevents stackoverflow exceptions
22+
// from deserialization errors that might occur from deeply nested objects.
23+
// This value is the same for model binding and Json.Net's serialization.
24+
MaxDepth = MvcOptions.DefaultMaxModelBindingRecursionDepth,
25+
26+
// We're using case-insensitive because there's a TON of code that there that does uses JSON.NET's default
27+
// settings (preserve case) - including the WebAPIClient. This worked when we were using JSON.NET + camel casing
28+
// because JSON.NET is case-insensitive by default.
29+
PropertyNameCaseInsensitive = true,
2130

22-
// We're using case-insensitive because there's a TON of code that there that does uses JSON.NET's default
23-
// settings (preserve case) - including the WebAPIClient. This worked when we were using JSON.NET + camel casing
24-
// because JSON.NET is case-insensitive by default.
25-
PropertyNameCaseInsensitive = true,
31+
// Use camel casing for properties
32+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
33+
};
34+
}
2635

27-
// Use camel casing for properties
28-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
29-
};
36+
// Initialize JsonOptions with already configured serializer options.
37+
// Note that these options will be publically accessible.
38+
internal JsonOptions(JsonSerializerOptions serializerOptions)
39+
{
40+
JsonSerializerOptions = serializerOptions;
41+
}
3042
}
3143
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Text.Json;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace Microsoft.AspNetCore.Mvc
9+
{
10+
/// <summary>
11+
/// This factory copies settings from Microsoft.AspNetCore.Http.Json.JsonOptions,
12+
/// and they are used as the base for any additional MVC serialization configuration.
13+
/// Changes applies to Microsoft.AspNetCore.Mvc.JsonOptions are isolated to MVC.
14+
/// </summary>
15+
internal class JsonOptionsFactory : OptionsFactory<JsonOptions>
16+
{
17+
private JsonSerializerOptions _serializerOptions;
18+
19+
public JsonOptionsFactory(
20+
IOptions<Http.Json.JsonOptions> options,
21+
IEnumerable<IConfigureOptions<JsonOptions>> setups,
22+
IEnumerable<IPostConfigureOptions<JsonOptions>> postConfigures,
23+
IEnumerable<IValidateOptions<JsonOptions>> validations) : base(setups, postConfigures, validations)
24+
{
25+
_serializerOptions = options.Value.SerializerOptions;
26+
}
27+
28+
protected override JsonOptions CreateInstance(string name)
29+
{
30+
// Copy values from Microsoft.AspNetCore.Http.Json.JsonOptions serializer options.
31+
return new JsonOptions(new JsonSerializerOptions(_serializerOptions));
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)