File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Reflection ;
5
+ using System . Runtime . Serialization ;
6
+ using System . Text . Json ;
7
+ using System . Text . Json . Serialization ;
8
+
9
+ namespace Docker . DotNet ;
10
+
11
+ // Adapted from https://github.com/dotnet/runtime/issues/74385#issuecomment-1705083109
12
+ internal sealed class JsonEnumMemberConverter < TEnum > : JsonStringEnumConverter < TEnum > where TEnum : struct , Enum
13
+ {
14
+ public JsonEnumMemberConverter ( ) : base ( namingPolicy : ResolveNamingPolicy ( ) )
15
+ {
16
+ }
17
+
18
+ private static JsonNamingPolicy ResolveNamingPolicy ( )
19
+ {
20
+ var map = typeof ( TEnum ) . GetFields ( BindingFlags . Public | BindingFlags . Static )
21
+ . Select ( f => ( f . Name , AttributeName : f . GetCustomAttribute < EnumMemberAttribute > ( ) ? . Value ) )
22
+ . Where ( pair => pair . AttributeName != null )
23
+ . ToDictionary ( e => e . Name , e => e . AttributeName ) ;
24
+
25
+ return map . Count > 0 ? new EnumMemberNamingPolicy ( map ) : null ;
26
+ }
27
+
28
+ private sealed class EnumMemberNamingPolicy : JsonNamingPolicy
29
+ {
30
+ private readonly IReadOnlyDictionary < string , string > _map ;
31
+
32
+ public EnumMemberNamingPolicy ( IReadOnlyDictionary < string , string > map ) => _map = map ;
33
+
34
+ public override string ConvertName ( string name ) => _map . TryGetValue ( name , out var newName ) ? newName : name ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change 6
6
using System . Net . Http . Json ;
7
7
using System . Runtime . CompilerServices ;
8
8
using System . Text . Json ;
9
- using System . Text . Json . Serialization ;
10
9
using System . Threading ;
11
10
using System . Threading . Tasks ;
11
+ using Docker . DotNet . Models ;
12
12
13
13
namespace Docker . DotNet
14
14
{
@@ -21,9 +21,10 @@ internal class JsonSerializer
21
21
{
22
22
Converters =
23
23
{
24
+ new JsonEnumMemberConverter < TaskState > ( ) ,
25
+ new JsonEnumMemberConverter < RestartPolicyKind > ( ) ,
24
26
new JsonDateTimeConverter ( ) ,
25
27
new JsonNullableDateTimeConverter ( ) ,
26
- new JsonStringEnumConverter ( ) ,
27
28
new JsonBase64Converter ( ) ,
28
29
} ,
29
30
} ;
You can’t perform that action at this time.
0 commit comments