[CSHARP-1056] BsonDocument [DataContract] serialization error Created: 05/Sep/14  Updated: 08/Sep/14  Resolved: 08/Sep/14

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.9.2
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Tony Davis Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I need to serialize a BsonDocument using the DataContractSerializer.
I have a class Play that has a BsonDocument (see stub below).

[BsonIgnoreExtraElements]
[DataContract(Namespace="f1")]
public class Play
{
...
[DataMember][BsonElement] public BsonDocument OD

{ get; set; }

}

Note: the first element in the BsonDocument is a BsonInt32.

When using the DataContractSerializer, I get the following error:
Type 'MongoDB.Bson.BsonInt32' with data contract name 'BsonInt32:http://schemas.datacontract.org/2004/07/MongoDB.Bson' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.



 Comments   
Comment by Craig Wilson [ 08/Sep/14 ]

Your use case here is almost exactly the purpose of why Automapper was built.

I'm going to close this as Won't Fix as this problem isn't an area we are focused on. Feel free to leave a comment if you disagree.

Craig

Comment by Tony Davis [ 08/Sep/14 ]

I own both sides (client side app and server side) so that is not an issue. It's looking like I'm going to have to roll my own object layer to handle it and then write the (ORM like code) to map my object layer to mongo and back. It's been nice so far to not have to do that with all my other objects. A BsonDocument was almost a perfect match for what I needed for the dynamic user defined variables, but it can't translate to XML or across a WCF border in native form as a [DataMember]. So back to the drawing board for a different solution as it doesn't appear a BsonDocument is easily fixable to support that.

Comment by Craig Wilson [ 06/Sep/14 ]

Are you forcing your users on the client-side of this stack to take a dependency on MongoDB as well, just to deserialize a BsonDocument?

Comment by Tony Davis [ 06/Sep/14 ]

I allow the user to add additional data variables dynamically at run-time in order to customize the application to their needs. Using a BsonDocument handles this very nicely - well until I need to send it through a WCF interface or export to XML. I tried out exporting a Dictionary<string, object>. It is very expensive via XML because it must add all the type information for each element in a very verbose way. I need something that works well on both sides: (1) mongo database storage and (2) WFC and for importing/exporting to/from XML. This is needed when working in a Windows stack.

Comment by Craig Wilson [ 05/Sep/14 ]

Hi Tony,

Sorry you are having trouble. BsonDocument and it's cohorts (BsonInt32, BsonString, etc...) don't play well with the DataContractSerializer. I'd suggest you use something like a Dictionary<string, object> instead. Alternatively, instead of having a catch-all type of property like this, go ahead and define your structure in a class. I know this can't always be done. Perhaps you could provide the reason you are using a BsonDocument here?

Craig

Generated at Wed Feb 07 21:38:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.