[CSHARP-376] Use IBsonSerializationOptions in Deserialize/Serialize methods in the various Bson/Serialization/Serializers/* Created: 04/Jan/12  Updated: 20/Mar/14  Resolved: 04/Jan/12

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

Type: Bug Priority: Blocker - P1
Reporter: Doug Marien Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: driver, serialization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-284 Add a way to provide serialization at... Closed
Backwards Compatibility: Fully Compatible

 Description   

Possibly related to CSHARP-284 and CSHARP-308?

I noticed in the various serializers in Bson/Serialization/Serializers/ that the Deserialize and Serialize methods were ignoring the given IBsonSerializationOptions. Is this by design?

I recently came across an issue by attempting to use the BsonRepresentationAttribute on a collection, e.g.:

[BsonRepresentation(BsonType.Int64, AllowOverflow = true)]
public List<UInt64> Ids

{ get; set; }

Then I hit an OverflowException when calling ToJson on my object:

System.OverflowException: Arithmetic operation resulted in an overflow.
at MongoDB.Bson.Serialization.Options.RepresentationSerializationOptions.ToInt64(UInt64 value)
at MongoDB.Bson.Serialization.Serializers.UInt64Serializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.SerializeMember(BsonWriter bsonWriter, Object obj, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
at MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj, Type nominalType, IBsonSerializationOptions options, JsonWriterSettings settings)
at MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj, Type nominalType, JsonWriterSettings settings)
at MongoDB.Bson.BsonExtensionMethods.ToJson(Object obj, Type nominalType)
at MongoDB.Bson.BsonExtensionMethods.ToJson[TNominalType](TNominalType obj)



 Comments   
Comment by Robert Stam [ 04/Jan/12 ]

It's not a C# restriction... I probably didn't say it very well!

The custom attribute is being applied to the property named Ids. The C# driver assumes that this attribute is defining serialization options for the serializer for List<UInt64> (in other words, the serializer for the Ids property as a whole), not for the serializer for UInt64.

What is needed is some way to provide options that are applied to the elements of the collection rather than to the collection itself.

By the way, you don't get compile time errors because as far as C# is concerned any attribute can be applied to any property (subject to the AttributeUsage defined for it). And you don't get a runtime error because the serializer for List<UInt64> ignores any options provided because it doesn't use any.

Comment by Doug Marien [ 04/Jan/12 ]

I didn't realize C# had a restriction as to the usage of custom attributes? Were you planning on using BsonRepresentationAttribute to apply to a List<> or other collection? In that case does AllowOverflow or AllowTruncation make sense? Or are you planning on creating a new attribute that is more explicit about being applied to the collection types?

Comment by Robert Stam [ 04/Jan/12 ]

The way you have it written the attribute is being applied to the List, not to the elements of the list (which there isn't a way to do yet).

This is related to CSHARP-284, which is about having a way to specify serialization options for the elements themselves as opposed to the list as a whole.

I'm going to close this one and leave CSHARP-284 open.

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