Dynamic Types with C# MongoDB Driver 2.2 - How to not serialize the _t type Discriminator?

2,946 views
Skip to first unread message

Joe Gardner

unread,
May 11, 2016, 12:43:29 PM5/11/16
to mongodb-user

I have two .Net (C#) applications that are passing "messages" via Mongo. One is a Writer, the other a Reader. The _t type discriminator is causing me issues with deserialization.

 

This is as simplified version of the common class they are using to communicate with:

 

public class MyMessage {
   
public long Id {get; set;}
   
public string MessageText {get; set;}
   
public dynamic OriginalObject {get; set;}
}


The Writer app is actually creating instances of MyMessage by mapping over values from some other type that will be unknown to the Reader app. It maps out the known common elements, and then assigns the entire original object to the dynamic OriginalObject property.

 

public MyMessage CreateMessage(SomeOtherType originalMsg) {
   
return new MyMessage {
       
MessageText = originalMsg.SomeField,
       
OriginalObject = originalMsg        
   
};
}


When the .Net Mongo Driver serializes MyMessage, it adds the _t type discriminator to the OriginalObject sub document.

 

However, the Reader app does not have reference to all of the types that Writer does. When the Reader application attempts to deserialize MyMessage, it throws an Unknown discriminator value error whenever OriginalObject is a type it doesn't have.

 

I've read that the _t type discriminator is added whenever the actual type doesn't match the nominal type. And that makes sense in a true polymorphic, strongly typed scenario. But now that C# has dynamic typing, I would like to use it.

 

Also, if the instance of MyMessage.OriginalMessage is a purely anonymous type, it works perfectly. No _t type discriminator is written. The Reader app happily deserializes it as a dynamic (expando) object and everything works. It's only when the instance of OriginalMessage is some strong type that I have this issue.

 

How do I tell the Mongo Driver not to add the _t type discriminator for dynamic types? I'm also interested in other solutions/work arounds, but simply excluding the _t would be my preferred approach.

 

.Net Framework 4.6.1, MongoDB.Driver 2.2.3

 

More sample code:

 

public void SaveMessages() {
   
var message1 = new MyMessage {
       
Id=1,
       
MessageText="Anonymous Message",
       
OriginalMessage=new{ Field1 = "f1", Field2 = "f2" }
   
};
    _collection
.InsertOne(message1);  // no _t since OriginalMessage is purely anonymous

   
var message2 = new MyMessage {
       
Id=2,
       
MessageText="Strong Message",
       
OriginalMessage=new SomeOtherType{Prop1="p1", Prop2="p2"}
   
};
    _collection
.InsertOne(message2); // has _t since SomeOtherType is not "dynamic"
}


Thanks for the help!

Wan Bachtiar

unread,
May 23, 2016, 8:46:55 PM5/23/16
to mongodb-user

Hi Joe,

Depending on how you would like to deserialise OriginalObject field, you can try these options:

You may also be interested to see Mapping Classes: Polymorphism.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages