Deserializing a List with null values - C#

685 views
Skip to first unread message

meboz

unread,
May 10, 2012, 9:14:23 PM5/10/12
to mongod...@googlegroups.com
Howdy,

Im struggling to work out how to solve this issue. Im not sure if [BsonDefaultValue] applies here or not.

I have a class as follows...

public class Account : MongoBase
{
  public List<Reminders> Reminders {get; set;}

  public Account(){
    Reminders = new List<Reminder>();
  }
}

For an account that exists in the db without a reminders field, the class gets instatiated and then the Reminders property is set to null, since its null in the db.

How would you overwrite/workaround this behaviour to allow the value from its constructor to not be overwritten in the case where the field doesnt exist?

Cheers,
Byron

Robert Stam

unread,
May 10, 2012, 9:24:03 PM5/10/12
to mongod...@googlegroups.com
See:


So in future versions of the driver, values set in the constructor won't be overwritten unless the database actually has a value for that property.

In the meantime, [BsonDefaultValue] doesn't quite do the trick because an argument to an attribute must be a compile-time constant. You could work around it for now by registering the class map in code like this:

    BsonClassMap.RegisterClassMap<Account>(cm =>
    {
        cm.AutoMap();
        cm.GetMemberMap(c => c. Reminders).SetDefaultValue(new List<Reminders>());
    });

Or you could get the latest source for the driver from github because CSHARP-467 has already been implemented.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/yUKWa1MfadAJ.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

meboz

unread,
May 10, 2012, 10:45:14 PM5/10/12
to mongod...@googlegroups.com
Thanks Robert.

Ill grab latest source. Thanks for the quick reply.

Cheers,
Byron
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

meboz

unread,
May 10, 2012, 11:35:03 PM5/10/12
to mongod...@googlegroups.com
Hi Robert,

I grabbed the latest source but the same problem existed.

I rolled back to 1.4.2, and tried the SetDefaultValue approach as you mentioned, but received this error which I thought was very odd...

Unable to cast object of type 'NameSpace.Reminder[]' to type 'NameSpace.MongoAccount'.


note: there are some sematic differences to my original post (class names, types etc).

Any ideas?

Cheers,
Byron

Robert Stam

unread,
May 10, 2012, 11:39:32 PM5/10/12
to mongod...@googlegroups.com
I'm surprised to hear about either problem. I did a quick test on my machine before posting my reply and did not get any errors.

If you are willing to post a complete program I can probably help you further. Even a stack trace for the error message you received would be helpful.

To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/zcPIYRfJhvYJ.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

Robert Stam

unread,
May 11, 2012, 1:20:39 AM5/11/12
to mongod...@googlegroups.com
Here's the test program I used:


When I run the test with the 1.4.2 C# driver I get this output:

List is: null
Press Enter to continue

which shows the issue you described, that the value you initialized in the constructor is being set back to null during deserialization because the document is missing a List element.

When I run the test with the latest driver from the master branch in github I get this output:

List is: System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.
0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Press Enter to continue

which shows the issue is fixed in the master branch on github because the value set in the constructor is no longer being overwritten.

If I uncomment the code that initializes the class map I get the same output whether I run the test with the 1.4.2 C# driver or the master branch from github.

Let me know if the test program I am using doesn't reflect your use case.

meboz

unread,
May 11, 2012, 1:55:26 AM5/11/12
to mongod...@googlegroups.com
Hi Robert,

I can see where I was making a mistake here.

Rather than my Reminders field being 'unset' in the db, it was infact, null. So the expected behaviour of mongo is now no surprise.

This will help me in the situation im in...

BsonClassMap.RegisterClassMap<Account>(x =>
                                      {
                                      x.AutoMap();
                                      x.GetMemberMap(r => r.Reminders).SetIgnoreIfNull(true);
                                      });

Cheers,
Byron
Reply all
Reply to author
Forward
0 new messages