[CSHARP-236] Can't map an user defined type (class or struct) to a bson value Created: 02/Jun/11  Updated: 02/Apr/15  Resolved: 02/Jun/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.1

Type: Bug Priority: Major - P3
Reporter: Silvio Massari Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: bson, serialization, userDefined
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7


Attachments: File MongoDriverBug.7z    

 Description   

In my project I'm using a user defined struct named Identity as id's type. So I implemented a BsonSerializer and an IIDGenerator. Then I registered them using the lines below:

BsonSerializer.RegisterSerializer(typeof(Identity), new IdentitySerializer());
BsonSerializer.RegisterIdGenerator(typeof(Identity?), new IdentityGenerator());

Then I defined an Order class the uses the Identity type:

public class Order
{
public Identity? Id

{ get; set; }
public decimal Amount { get; set; }

}

If I save a new Order into a collection, everything run well.
But when I want to save an existing Order an ArgumentException is being thrown with this message: ".NET type MongoDriverBug.Identity cannot be mapped to a BsonValue"

So I did some debug and found that the exception was thrown by the method below:

public static BsonValue MapToBsonValue(object value) {
BsonValue bsonValue;
if (TryMapToBsonValue(value, out bsonValue))

{ return bsonValue; }

var message = string.Format(".NET type {0} cannot be mapped to a BsonValue", value.GetType().FullName);
throw new ArgumentException(message);
}

I added some lines into the method to workaround the problem. The workaround tries to find a serializer for the value's type and if a serializer does exist, then it uses the serializer to convert the value to a valid BsonValue.

public static BsonValue MapToBsonValue(object value) {
BsonValue bsonValue;
if (TryMapToBsonValue(value, out bsonValue)) { return bsonValue; }

// ---------------------------------------------------------
// WORKAROUND START
var type = value.GetType();
var serializer = BsonSerializer.LookupSerializer(type);
if (serializer != null)
{
using (var stream = new MemoryStream())
{
using (var writer = BsonWriter.Create(stream))
{
writer.WriteStartDocument();
writer.WriteName("x");
serializer.Serialize(writer, type, value, null);
writer.WriteEndDocument();
writer.Flush();

stream.Seek(0, SeekOrigin.Begin);

using (var reader = BsonReader.Create(stream))

{ reader.ReadStartDocument(); reader.ReadName(); return BsonValue.ReadFrom(reader); }

}
}
}
// WORKAROUND END
// ---------------------------------------------------------

var message = string.Format(".NET type

{0}

cannot be mapped to a BsonValue", value.GetType().FullName);
throw new ArgumentException(message);
}



 Comments   
Comment by Silvio Massari [ 02/Jun/11 ]

Hi Robert.
Yes, it is fixez at master branch.
Sorry I didn't find that the issue was already reported.
Thanks for your time.
Silvio

Comment by Robert Stam [ 02/Jun/11 ]

I believe this is the same issue as:

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

Can you test with the latest version of the master branch from github?

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