[CSHARP-263] Deserialization of array that was serialized with nominalType object fails due to lack of type information Created: 07/Jul/11  Updated: 02/Apr/15  Resolved: 09/Aug/11

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

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-301 Deserialization of Dictionary that wa... Closed
is related to CSHARP-429 Cannot deserialize System.Object from... Closed

 Description   

Can reproduce using this class:

public class C

{ public ObjectId Id; public object Obj; }

and this code:

collection.RemoveAll();
var c = new C { Obj = new int[]

{ 1, 2, 3 }

};
collection.Insert(c);

var r = collection.FindOneAs<C>(); // fails



 Comments   
Comment by Robert Stam [ 09/Aug/11 ]

Created a new issue CSHARP-301 for Dictionary so that this issue can be closed for arrays.

Comment by Robert Stam [ 08/Jul/11 ]

Running the above code results in the following document being inserted into the database:

> db.test.find()

{ "_id" : ObjectId("4e170e44e447ad78a009cf75"), "Obj" : [ 1, 2, 3 ] }

>

The question is, what could this be serialized as? Perhaps:

> db.test.find()
{ "_id" : ObjectId("4e170e44e447ad78a009cf75"), "Obj" :

{ "_t" : "System.Int32[]", "_v" : [ 1, 2, 3 ] }

}
>

Not very cross platform... (includes .NET type name).

A very similar problem occurs with Dictionaries:

var c = new C { Obj = new Dictionary<string, int> {

{ "a", 1 }

,

{ "b", 2 }

,

{ "c", 3 }

} };
collection.Insert(c);

which serializes as:

> db.test.find()
{ "_id" : ObjectId("4e171014e447adbd34b71987"), "Obj" :

{ "a" : 1, "b" : 2, "c" : 3 }

}
>

And also can't be deserialized due to the lack of type information.

Another question is whether you should get back an instance of the exact same type as you started with. If not, these two examples could simply be deserialized as a BsonArray and a BsonDocument, but that's different than what you started with (an Int32[] and a Dictionary<string, int>).

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