Skip to content

Commit

Permalink
fix serialization issue with MongoDB provider
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgerlag committed May 7, 2018
1 parent 7760f1e commit b110382
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DataObjectSerializer : SerializerBase<object>
{
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All
TypeNameHandling = TypeNameHandling.Objects,
};

public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
Expand All @@ -32,15 +32,45 @@ public override object Deserialize(BsonDeserializationContext context, BsonDeser

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
string str = JsonConvert.SerializeObject(value, SerializerSettings);
var str = JsonConvert.SerializeObject(value, SerializerSettings);
var doc = BsonDocument.Parse(str);
var typeElem = doc.GetElement("$type");
doc.RemoveElement(typeElem);

if (doc.Elements.All(x => x.Name != "_t"))
doc.InsertAt(0, new BsonElement("_t", typeElem.Value));
ConvertMetaFormat(doc);

BsonSerializer.Serialize(context.Writer, doc);
}

private static void ConvertMetaFormat(BsonDocument root)
{
var stack = new Stack<BsonDocument>();
stack.Push(root);

while (stack.Count > 0)
{
var doc = stack.Pop();

if (doc.TryGetElement("$type", out var typeElem))
{
doc.RemoveElement(typeElem);

if (doc.Elements.All(x => x.Name != "_t"))
doc.InsertAt(0, new BsonElement("_t", typeElem.Value));
}

foreach (var subDoc in doc.Elements)
{
if (subDoc.Value.IsBsonDocument)
stack.Push(subDoc.Value.ToBsonDocument());

if (subDoc.Value.IsBsonArray)
{
foreach (var element in subDoc.Value.AsBsonArray)
{
if (element.IsBsonDocument)
stack.Push(element.ToBsonDocument());
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>1.6.0</Version>
<Version>1.6.2</Version>
<Description>Provides support to persist workflows running on Workflow Core to a MongoDB database.</Description>
<AssemblyVersion>1.6.0.0</AssemblyVersion>
<FileVersion>1.6.0.0</FileVersion>
<AssemblyVersion>1.6.2.0</AssemblyVersion>
<FileVersion>1.6.2.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit b110382

Please sign in to comment.