Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialization errors after adding Camelcase convention pack

I am using the latest C#-driver for MongoDB. I added the following code to my program to serialize in camelcase:

var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);

However, I get issues when trying to query documents after using the serialization. For example:

var query = _collection.AsQueryable<TimeSeries>();
Console.WriteLine(query.ToJson());

returns the following:

{ "_id" : ObjectId("54af0e848c27be15fc47a0d9"), "Name" : null, "Time" : null }

i.e, all properties seem to be null except for the id.

The object is serialized correctly, the field names are in camel case ("name" and "time") and each document contains the correct data ("name": Test 1" and "time": 2014).

It seems like the problem is that the query function doesn't realize that the fields are in camelcase and therefore returns null. Therefore I am unable to deserialize any objects.

Is there any way to avoid this error?

like image 699
Philip Bergström Avatar asked Jan 08 '15 23:01

Philip Bergström


1 Answers

I managed to solve the problem. The problem was that I defined the convention pack after creating a MongoDB connection. This was solved by registring the Conventionpack before initializing the connection to with the MongoDB.

like image 124
Philip Bergström Avatar answered Sep 21 '22 23:09

Philip Bergström