-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Type serialization for MongoDB provider
- Loading branch information
1 parent
cbfb21c
commit abc4e53
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/persistence/Elsa.Persistence.MongoDb/Serializers/TypeSerializer.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,28 @@ | ||
using System; | ||
using Elsa.Models; | ||
using MongoDB.Bson.Serialization; | ||
using Rebus.Extensions; | ||
|
||
namespace Elsa.Persistence.MongoDb.Serializers | ||
{ | ||
public class TypeSerializer : IBsonSerializer<Type> | ||
{ | ||
public static TypeSerializer Instance { get; } = new(); | ||
public Type ValueType => typeof(Variables); | ||
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) => Serialize(context, args, (Type) value); | ||
|
||
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Type value) | ||
{ | ||
var typeName = value.GetSimpleAssemblyQualifiedName(); | ||
context.Writer.WriteString(typeName); | ||
} | ||
|
||
object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => Deserialize(context, args); | ||
|
||
public Type Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) | ||
{ | ||
var typeName = context.Reader.ReadString(); | ||
return typeName is not null and not "" ? Type.GetType(typeName)! : default!; | ||
} | ||
} | ||
} |
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