-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
56 additions
and
42 deletions.
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
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
40 changes: 40 additions & 0 deletions
40
Mindbox.YandexTracker/JsonConverters/EnumWithEnumMemberAttributeJsonConverter.cs
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,40 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Mindbox.YandexTracker.JsonConverters; | ||
|
||
/// <summary> | ||
/// Кастомный конвертер для сериализации/десериализации enum'ов с атрибутом EnumMemberAttribute. | ||
/// </summary> | ||
/// <remarks> | ||
/// При конвертации в json значение берется из атрибута. | ||
/// </remarks> | ||
internal class EnumWithEnumMemberAttributeJsonConverter<T> : JsonConverter<T> where T: Enum | ||
{ | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
string? enumString = reader.GetString(); | ||
for (var i = 0; i < typeToConvert.GetFields().Length; i++) | ||
{ | ||
var field = typeToConvert.GetFields()[i]; | ||
|
||
if (field.GetCustomAttribute<EnumMemberAttribute>()?.Value == enumString) | ||
{ | ||
return (T)field.GetValue(null)!; | ||
} | ||
} | ||
|
||
if (enumString == null) throw new JsonException($"Unknown value for {nameof(QueueLocalFieldType)} ({enumString})"); | ||
|
||
return (T)Enum.Parse(typeToConvert, enumString, true); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
var enumMemberAttribute = value.GetType().GetField(value.ToString())?.GetCustomAttribute<EnumMemberAttribute>(); | ||
writer.WriteStringValue(enumMemberAttribute != null ? enumMemberAttribute.Value : value.ToString()); | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
Mindbox.YandexTracker/JsonConverters/QueueLocalFieldTypeConverter.cs
This file was deleted.
Oops, something went wrong.
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
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
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