Skip to content

Commit 535eee9

Browse files
authored
feat(Converter): add JsonEnumConverter class (#4209)
* feat: 增加 JsonEnumConverter 转换器 * test: 增加单元测试 * refactor: 移除代码 * chore: bump version 8.0.2 * feat: 增加枚举转换器标签 * revert: 恢复扩展方法 * chore: bump version 8.9.1-beta02 * doc: 更新注释文档 * refactor: 移除转化器使用 BB 内置的 * chore: bump version 8.1.5-beta01 * doc: 更新依赖包 * chore: bump version 8.1.4-beta01 * chore: 更新依赖包 * chore: 更新 Core 工程位置
1 parent 0d535d2 commit 535eee9

File tree

16 files changed

+125
-110
lines changed

16 files changed

+125
-110
lines changed

src/BootstrapBlazor.Core/BootstrapBlazor.Core.csproj

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/BootstrapBlazor.Core/Extensions/EnumExtensions.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<PackageReference Include="BootstrapBlazor.CherryMarkdown" Version="8.0.0" />
3737
<PackageReference Include="BootstrapBlazor.CodeEditor" Version="8.0.2" />
3838
<PackageReference Include="BootstrapBlazor.Dock" Version="8.1.7" />
39-
<PackageReference Include="BootstrapBlazor.DockView" Version="8.1.3" />
39+
<PackageReference Include="BootstrapBlazor.DockView" Version="8.1.4-beta01" />
4040
<PackageReference Include="BootstrapBlazor.DriverJs" Version="8.0.1" />
4141
<PackageReference Include="BootstrapBlazor.ElementIcon" Version="8.0.0" />
4242
<PackageReference Include="BootstrapBlazor.FileViewer" Version="8.0.3" />

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>8.9.1-beta01</Version>
4+
<Version>8.9.1-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
@@ -63,10 +63,6 @@
6363
<None Include="wwwroot\src\**\*.*" />
6464
</ItemGroup>
6565

66-
<ItemGroup>
67-
<ProjectReference Include="..\BootstrapBlazor.Core\BootstrapBlazor.Core.csproj" />
68-
</ItemGroup>
69-
7066
<ItemGroup>
7167
<Using Include="BootstrapBlazor.Components" />
7268
<Using Include="Microsoft.AspNetCore.Components" />

src/BootstrapBlazor.Core/Converter/JsonDescriptionEnumConverter.cs renamed to src/BootstrapBlazor/Converter/JsonDescriptionEnumConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
namespace BootstrapBlazor.Core.Converter;
99

1010
/// <summary>
11-
/// 枚举类型转换器
11+
/// 枚举类型转换器 序列化时把枚举类型的 [Description] 标签内容序列化成字符串 推荐使用 <see cref="JsonEnumConverter"/> 转换器
1212
/// </summary>
13-
/// <remarks>序列化时把枚举类型序列化成字符串</remarks>
1413
public class JsonDescriptionEnumConverter<T> : JsonConverter<T> where T : struct, Enum
1514
{
1615
/// <summary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace BootstrapBlazor.Core.Converter;
9+
10+
/// <summary>
11+
/// JsonEnumConverter 枚举转换器
12+
/// </summary>
13+
public class JsonEnumConverter : JsonConverterAttribute
14+
{
15+
/// <summary>
16+
/// 构造函数
17+
/// </summary>
18+
public JsonEnumConverter() : base()
19+
{
20+
21+
}
22+
23+
/// <summary>
24+
/// 构造函数
25+
/// </summary>
26+
/// <param name="camelCase">Optional naming policy for writing enum values.</param>
27+
/// <param name="allowIntegerValues">True to allow undefined enum values. When true, if an enum value isn't defined it will output as a number rather than a string.</param>
28+
public JsonEnumConverter(bool camelCase, bool allowIntegerValues = true) : this()
29+
{
30+
CamelCase = camelCase;
31+
AllowIntegerValues = allowIntegerValues;
32+
}
33+
34+
/// <summary>
35+
/// naming policy for writing enum values
36+
/// </summary>
37+
public bool CamelCase { get; private set; }
38+
39+
/// <summary>
40+
/// True to allow undefined enum values. When true, if an enum value isn't defined it will output as a number rather than a string
41+
/// </summary>
42+
public bool AllowIntegerValues { get; private set; } = true;
43+
44+
/// <summary>
45+
/// <inheritdoc/>
46+
/// </summary>
47+
/// <param name="typeToConvert"></param>
48+
/// <returns></returns>
49+
public override JsonConverter? CreateConverter(Type typeToConvert)
50+
{
51+
var converter = CamelCase
52+
? new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, AllowIntegerValues)
53+
: new JsonStringEnumConverter(null, AllowIntegerValues);
54+
return converter;
55+
}
56+
}

src/BootstrapBlazor/Extensions/EnumExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ namespace BootstrapBlazor.Components;
1212
/// </summary>
1313
public static class EnumExtensions
1414
{
15+
/// <summary>
16+
/// 获取 DescriptionAttribute 标签方法
17+
/// </summary>
18+
/// <param name="val"></param>
19+
/// <returns></returns>
20+
public static string ToDescriptionString<TEnum>(this TEnum val) where TEnum : Enum => typeof(TEnum).ToDescriptionString(val.ToString());
21+
22+
/// <summary>
23+
/// 通过字段名称获取 DescriptionAttribute 标签值
24+
/// </summary>
25+
/// <param name="type"></param>
26+
/// <param name="fieldName"></param>
27+
/// <returns></returns>
28+
public static string ToDescriptionString(this Type? type, string? fieldName)
29+
{
30+
var ret = string.Empty;
31+
if (type != null && !string.IsNullOrEmpty(fieldName))
32+
{
33+
var t = Nullable.GetUnderlyingType(type) ?? type;
34+
var attributes = t.GetField(fieldName)?.GetCustomAttribute<DescriptionAttribute>(true);
35+
ret = attributes?.Description ?? fieldName;
36+
}
37+
return ret;
38+
}
39+
1540
/// <summary>
1641
/// 通过字段名称获取 DisplayAttribute/DescriptionAttribute 标签值
1742
/// </summary>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Version>8.0.2</Version>
5+
</PropertyGroup>
6+
7+
</Project>

0 commit comments

Comments
 (0)