1
1
using MongoDB . Bson . Serialization ;
2
2
using MongoDB . Bson . Serialization . Serializers ;
3
+ using MongoDB . Bson ;
3
4
using System ;
4
5
using System . Collections . Generic ;
5
6
using System . Linq ;
@@ -14,20 +15,26 @@ namespace WorkflowCore.Persistence.MongoDB.Services
14
15
public class DataObjectSerializer : SerializerBase < object >
15
16
{
16
17
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings ( ) { TypeNameHandling = TypeNameHandling . All } ;
17
-
18
+
18
19
public override object Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
19
- {
20
- var raw = BsonSerializer . Deserialize < string > ( context . Reader ) ;
21
- var result = JsonConvert . DeserializeObject ( raw , SerializerSettings ) ;
22
- return result ;
20
+ {
21
+ if ( context . Reader . CurrentBsonType == BsonType . String )
22
+ {
23
+ var raw = BsonSerializer . Deserialize < string > ( context . Reader ) ;
24
+ return JsonConvert . DeserializeObject ( raw , SerializerSettings ) ;
25
+ }
26
+
27
+ return BsonSerializer . Deserialize ( context . Reader , typeof ( object ) ) ;
23
28
}
24
29
25
30
public override void Serialize ( BsonSerializationContext context , BsonSerializationArgs args , object value )
26
31
{
27
32
string str = JsonConvert . SerializeObject ( value , SerializerSettings ) ;
28
- BsonSerializer . Serialize ( context . Writer , str ) ;
33
+ var doc = BsonDocument . Parse ( str ) ;
34
+ var typeElem = doc . GetElement ( "$type" ) ;
35
+ doc . InsertAt ( 0 , new BsonElement ( "_t" , typeElem . Value ) ) ;
36
+ doc . RemoveElement ( typeElem ) ;
37
+ BsonSerializer . Serialize ( context . Writer , doc ) ;
29
38
}
30
-
31
-
32
39
}
33
40
}
0 commit comments