[CSHARP-716] Add support for serializing type Tuple introduced in .NET Framework 4 Created: 28/Mar/13  Updated: 02/Apr/15  Resolved: 10/Sep/14

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

Type: New Feature Priority: Major - P3
Reporter: Mary Hamlin Assignee: Robert Stam
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 64-bit


Attachments: Text File TupleSerializer.cs     Text File c-sharp driver bug.txt    
Issue Links:
Depends
depends on CSHARP-625 Make .NET Framework 4 the minimum req... Closed

 Description   

I have a c# Fund class with a property of type Dictionary<Tuple<int,DateTime>, int> called Quartiles. I added a number of Fund objects to a collection in mongodb called "funds". They were added without any errors. I then attempted to read back the fund documents from the collection. I got an error trying to access the first document in the collection. The error message said there was a problem serializing the Quartiles property and that an item with the same key had already been added. I then attempted to inspect a sample Fund document using the mongo shell and it looks like it wasn't created properly.

All relevant information is attached.



 Comments   
Comment by Githook User [ 10/Sep/14 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-716: Renamed TupleSerializer field names and added public properties for the ItemSerializers.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/032385f26fa134b73e85940432529bb918283918

Comment by Githook User [ 10/Sep/14 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-716: Add serializers for .NET's Tuple types.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/1950146eb1a5512435993f0eb5f09b1edfa8a2ba

Comment by Robert Stam [ 31/Mar/13 ]

This issue depends on CSHARP-625 because we can't add serializers for .NET Framework 4 specific types until we make .NET Framework 4 the minimum required framework version for the C# driver.

Comment by Robert Stam [ 30/Mar/13 ]

One note about the sample TupleSerializer: it stores the items of the tuple in a BSON array.

That seemed like the most natural representation of a Tuple in a BSON document, although other representations are possible. For example, a Tuple could also have been represented as an embedded document instead of an array:

{ Item1 : value1, Item2 : value2 }

Which in some cases might be a better representation (depending on what sort of queries you write and how you might want to index your documents). The sample TupleSerializer could be modified to use this representation instead.

Comment by Robert Stam [ 30/Mar/13 ]

While the driver itself can't yet support serializing .NET Framework 4 types, there is nothing preventing a custom serializer being registered for them. I'm attaching a sample serializer for Tuple<T1, T2> that you could incorporate into your application to make serialization of Tuple<T1, T2> work with the current version of the driver (see the attached TupleSerializer.cs file). This serializer will work with any types T1 and T2 as long as T1 and T2 are themselves properly serializable.

In order for this serializer to be available you must register it as follows:

BsonSerializer.RegisterGenericSerializerDefinition(typeof(Tuple<,>), typeof(TupleSerializer<,>));

You must register serializers as early as possible in the lifecycle of your application. At the very least the serializer must be registered before any attempt is made to serialize or deserialize a Tuple.

This serializer could be modified slightly to create new serializers for other Tuples of different cardinality.

Comment by Robert Stam [ 30/Mar/13 ]

Version 1.8 of the C# driver is built against .NET Framework 3.5. While it can be used with applications that target .NET Framework 4, it does not have built-in serializers for types, like Tuple, that were introduced in .NET Framework 4.

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