-
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.
BREAKING: Update MongoDB provider with support for object derivatives
Fixes #552
- Loading branch information
1 parent
985fd39
commit 45b0f84
Showing
3 changed files
with
52 additions
and
19 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
37 changes: 37 additions & 0 deletions
37
src/persistence/Elsa.Persistence.MongoDb/Serializers/ObjectSerializer.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,37 @@ | ||
using System; | ||
using MongoDB.Bson.Serialization; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using NodaTime; | ||
using NodaTime.Serialization.JsonNet; | ||
|
||
namespace Elsa.Persistence.MongoDb.Serializers | ||
{ | ||
public class ObjectSerializer : IBsonSerializer<object> | ||
{ | ||
public static ObjectSerializer Instance { get; } = new(); | ||
|
||
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings | ||
{ | ||
NullValueHandling = NullValueHandling.Ignore, | ||
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple, | ||
TypeNameHandling = TypeNameHandling.Auto, | ||
}.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); | ||
|
||
public Type ValueType => typeof(object); | ||
|
||
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) | ||
{ | ||
var json = JsonConvert.SerializeObject(value, _serializerSettings); | ||
context.Writer.WriteString(json); | ||
} | ||
|
||
object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) => Deserialize(context, args); | ||
|
||
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) | ||
{ | ||
var text = context.Reader.ReadString(); | ||
return JsonConvert.DeserializeObject<object>(text, _serializerSettings)!; | ||
} | ||
} | ||
} |
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