-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #728 - JSON string-to-enum converters improvements:
- Added internal helper class for creating/storing string name (from attribute) to value mapping for enums. - Added internal helper extension method for converting a string into an enum value with fallback handling. - The new helper method is used for all custom JSON-to-enum converters now. (cherry picked from commit 00d8a6eefe6045997df8e199b9ba46770bf875f4)
- Loading branch information
Showing
6 changed files
with
47 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,42 @@ | ||
namespace Alpaca.Markets; | ||
using System.Reflection; | ||
|
||
namespace Alpaca.Markets; | ||
|
||
internal static class EnumExtensions | ||
{ | ||
private static class NamesHelper< | ||
#if NET6_0_OR_GREATER | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] | ||
#endif | ||
T> where T : struct, Enum | ||
{ | ||
public static readonly IReadOnlyDictionary<String, T> ValuesByNames = | ||
Enum.GetValues(typeof(T)).OfType<T>() | ||
.ToDictionary(getJsonName, value => value); | ||
|
||
private static string getJsonName( | ||
T value) => | ||
typeof(T).GetField(Enum.GetName(typeof(T), value) ?? value.ToString())? | ||
.GetCustomAttribute<EnumMemberAttribute>()?.Value ?? value.ToString(); | ||
} | ||
|
||
private static readonly Char[] _doubleQuotes = [ '"' ]; | ||
|
||
public static String ToEnumString<T>( | ||
this T enumValue) | ||
where T : struct, Enum => | ||
JsonConvert.SerializeObject(enumValue).Trim(_doubleQuotes); | ||
|
||
public static T FromEnumString< | ||
#if NET6_0_OR_GREATER | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] | ||
#endif | ||
T>( | ||
this T fallbackEnumValue, | ||
JsonReader reader) | ||
where T : struct, Enum => | ||
reader.TokenType == JsonToken.String | ||
? NamesHelper<T>.ValuesByNames.GetValueOrDefault( | ||
reader.Value as String ?? String.Empty, fallbackEnumValue) | ||
: fallbackEnumValue; | ||
} |
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