Reputation: 1195
I am trying to serialize a class I have and save it to a Mongo database I have. The issue I have is that the class I have inherits from another base class from a 3rd party library (Unity3D). The class is specifically MonoBehaviour.
Example:
public class Weapon : MonoBehaviour
{
public bool PreviewEnabled;
public float Spread { get; set; }
public float Spacing { get; set; }
public int Count { get; set; }
public float Chaos { get; set; }
}
So this saves to my DB without issue, however when I try to deserialize and convert it back to an instance of Weapon I get an error from the MongoDB assemblies.
I know this has to do with my base class as if I look at the document in my DB it now has a few extra fields/properties in it that are not in my Weapon class, but I know are a part of the base class, Monobehaviour. Here is an example of one saved document:
{
"_id": {
"$oid": "54dd153596a433238cd3ea42"
},
"name": "PlayerPreview",
"hideFlags": 0,
"active": true,
"tag": "Player",
"enabled": true,
"useGUILayout": true,
"PreviewEnabled": true,
"Spread": 0,
"Spacing": 1,
"Count": 2,
"Chaos": 0,
}
So I imagine the error happens because it is trying to map things that exist in a Monobehaviour like enabled
and useGUILayout
to non-existent properties in my Weapon class.
How can I tell MongoDB to ignore fields / properties specifically in this Monobehaviour base class considering the fact that I do not have the source for this class - its in the Unity3D library that I have not got access to?
Here is the error I get:
(System.Runtime.CompilerServices.ExecutionScope,object,object) <IL 0x00016, 0x0007f>
MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (MongoDB.Bson.IO.BsonReader,System.Type,System.Type,MongoDB.Bson.Serialization.IBsonSerializationOptions) <IL 0x00183, 0x00967>
MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (MongoDB.Bson.IO.BsonReader,System.Type,MongoDB.Bson.Serialization.IBsonSerializationOptions) <IL 0x0004d, 0x001e7>
MongoDB.Driver.Internal.MongoReplyMessage`1<Weapon>.ReadFrom (MongoDB.Bson.IO.BsonBuffer,MongoDB.Bson.Serialization.IBsonSerializationOptions) <0x00772>
MongoDB.Driver.Internal.MongoConnection.ReceiveMessage<Weapon> (MongoDB.Bson.IO.BsonBinaryReaderSettings,MongoDB.Bson.Serialization.IBsonSerializer,MongoDB.Bson.Serialization.IBsonSerializationOptions) <0x0030d>
Also to note, I have tested that this all works by saving a normal class with no inheritance of a base class to the DB and deserialized it back into my game with the same logic I am using for the above.
Upvotes: 2
Views: 2424
Reputation: 46311
Yes, that's possible using ConventionPack
though it might be better to use a different model instead and convert back and forth in this case - after all, you're getting into trouble if the type changes in new release...
var pack = new ConventionPack();
pack.Add(new IgnoreExtraElementsConvention(true));
ConventionRegistry.Register("sloppy", pack,
t => typeof(MonoBehaviour).IsAssignableFrom(t));
I didn't read that question carefully, sorry. After all, the values are present in your class. The problem is that MongoDB doesn't know the class inheritance, and you can't decorate the base class with [BsonDisriminator(Required=true)]
.
While you can also do that during runtime, using foreign classes for serialization will be a constant source of pain, especially if they decide to make the class immutable in certain ways. I'd use a separate model and use map back and forth, maybe using AutoMapper to avoid stupid copy code. (This is the second time I recommended AutoMapper in 5 minutes, but no, I'm not affiliated in any way...)
Upvotes: 2