-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Docker.DotNet; | ||
|
||
// Adapted from https://github.com/dotnet/runtime/issues/74385#issuecomment-1705083109 | ||
internal sealed class JsonEnumMemberConverter<TEnum> : JsonStringEnumConverter<TEnum> where TEnum : struct, Enum | ||
{ | ||
public JsonEnumMemberConverter() : base(namingPolicy: ResolveNamingPolicy()) | ||
{ | ||
} | ||
|
||
private static JsonNamingPolicy ResolveNamingPolicy() | ||
{ | ||
var map = typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static) | ||
.Select(f => (f.Name, AttributeName: f.GetCustomAttribute<EnumMemberAttribute>()?.Value)) | ||
.Where(pair => pair.AttributeName != null) | ||
.ToDictionary(e => e.Name, e => e.AttributeName); | ||
|
||
return map.Count > 0 ? new EnumMemberNamingPolicy(map) : null; | ||
} | ||
|
||
private sealed class EnumMemberNamingPolicy : JsonNamingPolicy | ||
{ | ||
private readonly IReadOnlyDictionary<string, string> _map; | ||
|
||
public EnumMemberNamingPolicy(IReadOnlyDictionary<string, string> map) => _map = map; | ||
|
||
public override string ConvertName(string name) => _map.TryGetValue(name, out var newName) ? newName : name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters