MongoDB.Bson Error: "Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is Array."

1,248 views
Skip to first unread message

Tim

unread,
Aug 3, 2011, 5:15:21 AM8/3/11
to mongodb-csharp
Hello,

I believe Robert Stam has created a ticket for this already (below)
but I wanted to see if anyone had an update on it? This is happening
because I have an entity I'm storing as a collection has an object as
a property (method parameter). I can view the collection fine in
MongoVue, and it casts the type correctly when expanding the document
(sometimes it might be a reference, value type, or a list of reference
types).

https://jira.mongodb.org/browse/CSHARP-263

Please let me know!
Tim

Robert Stam

unread,
Aug 3, 2011, 9:31:55 AM8/3/11
to mongodb-csharp
Work on CSHARP-263 has not yet begun. If you have any suggestions for
what the serialized form should look like please comment on the JIRA.
Thanks.

Serialization of these objects works fine. It is only deserialization
that fails due to the lack of type information.

Tim

unread,
Aug 3, 2011, 5:42:14 PM8/3/11
to mongodb-csharp
I will... I was hoping a solution might be found for deserialization,
not serialization... I'm assuming the reflection would be more
expensive, but maybe a parameter coudl be passed to use reflection on
individual properties when deserializing? I know this must be
possible because MongoVue reads the documents and their object
properties perfectly (casting each unique object correctly, so I can
expand nested reference types, etc). I believe I read that MongoVue
is using the same driver, it must be handling it differently for
deserialization. I'm actually using fluent mongo, is it possible the
issue is there, and not in MongoDB.Bson assembly?

Robert Stam

unread,
Aug 3, 2011, 5:48:54 PM8/3/11
to mongodb-csharp
Just to confirm. Serialization is working. But there is not enough
information in the serialized form to deserialize. That's what
CSHARP-263 is about.

I think MongoVue is reading all data using BsonDocument, not your
custom type (after all, MongoVue doesn't know about your classes).

There is nothing wrong with the data itself, just that when trying to
deserialize a plain BsonArray into an object the serializer doesn't
know what concrete .NET type to use.

You could read it as a BsonDocument also. It's only when you try to
read it as in instance of your own class that deserialization runs
into the problem.

Tim

unread,
Aug 3, 2011, 6:24:23 PM8/3/11
to mongodb-csharp
Yes serialization and saving to mongo is working fine, it's only when
I attempt read from this specific collection that has an object as a
property. What you mention there about MongoVue makes sense, it
doesn't know my entity schema, it must purely be using reflection, and
on a per document basis (not for the entire collection) since each
document in the collection may / will have a unique schema.

With that, I'm wondering if it makes sense to just read this
collection as raw BSON documents, instead of as my typed entity, or
possibly try to use collection<object>("collectionname") when
retrieving this particular collection. Before you sent this, I was
thinking of possibly just storing this property as xml, so it would
just be a string. This wouldn't be ideal, but a work around.

I see what you're saying about assigning the type when serialization
now, that would be a good option in this instance. To do that, the
serializer would need to be aware of reference types outside of it's
own collection type though... would that be possible? To have it
reference a data entity assembly to populate a list of known reference
types? That way during serialization so you could set "car" as a
type, even if the collection itself was of type "house".

Tim

unread,
Aug 3, 2011, 8:09:08 PM8/3/11
to mongodb-csharp
Just as a thought jogger, .Net does this in WCF with Data Contract
Known Types... decorating classes with known type attributes so they
can later be deserialized properly. But I believe this is done at
compile time, which wouldn't work in this scenario.

Data Contract Known Type Example:

[DataContract]
[KnownType(typeof(CircleType))]
[KnownType(typeof(TriangleType))]
public class CompanyLogo2
{
[DataMember]
private Shape ShapeOfLogo;
[DataMember]
private int ColorOfLogo;

}

http://msdn.microsoft.com/en-us/library/ms730167.aspx
Reply all
Reply to author
Forward
0 new messages