-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
MvcNewtonsoftJsonOptionsExtensions.cs
83 lines (73 loc) · 3.49 KB
/
MvcNewtonsoftJsonOptionsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for Mvc.Newtonsoft.Json options.
/// </summary>
public static class MvcNewtonsoftJsonOptionsExtensions
{
/// <summary>
/// Configures the casing behavior of JSON serialization to use camel case for property names,
/// and optionally for dynamic types and dictionary keys.
/// </summary>
/// <remarks>
/// This method modifies <see cref="JsonSerializerSettings.ContractResolver"/>.
/// </remarks>
/// <param name="options"><see cref="MvcNewtonsoftJsonOptions"/></param>
/// <param name="processDictionaryKeys">If true will camel case dictionary keys and properties of dynamic objects.</param>
/// <returns><see cref="MvcNewtonsoftJsonOptions"/> with camel case settings.</returns>
public static MvcNewtonsoftJsonOptions UseCamelCasing(this MvcNewtonsoftJsonOptions options, bool processDictionaryKeys)
{
ArgumentNullException.ThrowIfNull(options);
if (options.SerializerSettings.ContractResolver is DefaultContractResolver resolver)
{
resolver.NamingStrategy = new CamelCaseNamingStrategy
{
ProcessDictionaryKeys = processDictionaryKeys
};
}
else
{
if (options.SerializerSettings.ContractResolver == null)
{
throw new InvalidOperationException(Resources.FormatContractResolverCannotBeNull(nameof(JsonSerializerSettings.ContractResolver)));
}
var contractResolverName = options.SerializerSettings.ContractResolver.GetType().Name;
throw new InvalidOperationException(
Resources.FormatInvalidContractResolverForJsonCasingConfiguration(contractResolverName, nameof(DefaultContractResolver)));
}
return options;
}
/// <summary>
/// Configures the casing behavior of JSON serialization to use the member's casing for property names,
/// properties of dynamic types, and dictionary keys.
/// </summary>
/// <remarks>
/// This method modifies <see cref="JsonSerializerSettings.ContractResolver"/>.
/// </remarks>
/// <param name="options"><see cref="MvcNewtonsoftJsonOptions"/></param>
/// <returns><see cref="MvcNewtonsoftJsonOptions"/> with member casing settings.</returns>
public static MvcNewtonsoftJsonOptions UseMemberCasing(this MvcNewtonsoftJsonOptions options)
{
ArgumentNullException.ThrowIfNull(options);
if (options.SerializerSettings.ContractResolver is DefaultContractResolver resolver)
{
resolver.NamingStrategy = new DefaultNamingStrategy();
}
else
{
if (options.SerializerSettings.ContractResolver == null)
{
throw new InvalidOperationException(Resources.FormatContractResolverCannotBeNull(nameof(JsonSerializerSettings.ContractResolver)));
}
var contractResolverName = options.SerializerSettings.ContractResolver.GetType().Name;
throw new InvalidOperationException(
Resources.FormatInvalidContractResolverForJsonCasingConfiguration(contractResolverName, nameof(DefaultContractResolver)));
}
return options;
}
}