-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
reven
committed
May 18, 2023
1 parent
d48846a
commit 163ee33
Showing
10 changed files
with
151 additions
and
33 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
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,39 @@ | ||
using LiteDB; | ||
|
||
namespace Fenrus.Converters; | ||
|
||
/// <summary> | ||
/// Provides serialization and deserialization methods for enum values to be stored as integers. | ||
/// </summary> | ||
public static class EnumSerializer | ||
{ | ||
/// <summary> | ||
/// Registers custom serializers and deserializers for all enum types in the assembly. | ||
/// </summary> | ||
/// <param name="mapper">The BsonMapper instance to register the serializers and deserializers.</param> | ||
public static void RegisterEnums(BsonMapper mapper) | ||
{ | ||
BsonMapper.Global.ResolveMember = (type, memberInfo, memberMapper) => | ||
{ | ||
if (memberMapper.DataType.IsEnum) | ||
{ | ||
memberMapper.Serialize = (obj, mapper) => new BsonValue((int)obj); | ||
memberMapper.Deserialize = (value, mapper) => | ||
{ | ||
if (value.IsString) | ||
{ | ||
var str = value.AsString; | ||
if (Enum.TryParse(memberMapper.DataType, str, out var enumValue)) | ||
return enumValue!; | ||
|
||
return 0; | ||
} | ||
else | ||
{ | ||
return Enum.ToObject(memberMapper.DataType, value); | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
} |
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
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