zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
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
#if !NETSTANDARD2_0
using System.Text.Json;
using System.Text.Json.Serialization;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// System.Text.Json 扩展操作类
    /// </summary>
    public static class JsonExtensions
    {
        /// <summary>
        /// 提供默认配置 支持驼峰命名方法(Pascal)
        /// </summary>
        /// <param name="options">JsonSerializerOptions 实例</param>
        /// <remarks>内部 Converters 增加了时间转化器</remarks>
        /// <returns></returns>
        public static JsonSerializerOptions AddDefaultConverters(this JsonSerializerOptions options)
        {
            options.PropertyNamingPolicy = null;
            options.PropertyNameCaseInsensitive = true;
            options.Converters.Add(new JsonStringEnumConverter());
            options.Converters.Add(new StringToNumberConverter());
            options.Converters.Add(new StringToTimeSpanConverter());
            options.Converters.Add(new CustomerDateTimeConverter());
            options.Converters.Add(new CustomerDateTimeOffsetConverter());
            return options;
        }
    }
}
#endif