c# driver NeverSerializeDefaultValueConvention not effecting value types

191 views
Skip to first unread message

Kevin Lewis

unread,
Aug 23, 2011, 3:45:48 PM8/23/11
to mongodb-user
I have a NeverSerializeDefaultValueConvention set and it seems to be
working fine for reference types, but not for value types. I have a
lot default ObjectId and Guid values taking up space that I would like
to prevent. I can get it to work like I want by manually mapping the
classes and calling SetSerializeDefaultValue(false) on the ObjectId
and Guid properties. I'd rather not have to do that manually for every
property though. What am I doing wrong to get the
NeverSerializeDefaultValueConvention to effect value types?

Kevin Lewis

unread,
Aug 23, 2011, 3:56:09 PM8/23/11
to mongodb-user
I realized just after this post that I was wrong about it effecting
reference types. I also have an AlwaysIgnoreNullConvention set and
that seems to be what is keeping all the default null values out for
reference types. So best I can tell,
NeverSerializeDefaultValueConvention isn't doing anything at all.

sridhar

unread,
Aug 23, 2011, 5:54:43 PM8/23/11
to mongodb-user
NeverSerializeDefaultValueConvention is used in conjunction with
BsonDefaultValue. If you specify a default value for a field, you then
use the NeverSerializeDefaultValueConvention to not serialize it. An
example of this can be found in the unit tests for the driver at
BsonUnitTests\DefaultSerializer\Attributes\BsonAttributeTests.cs

If you want attributes to not be serialized then tag the field/
property using BsonIgnore as specified in
http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Ignoringafieldorproperty.
Hope this helps

Kevin Lewis

unread,
Aug 23, 2011, 8:04:25 PM8/23/11
to mongodb-user
I have set default values. I've created a custom default value
convention that returns Guid.Empty for all Guids and ObjectId.Empty
for all object IDs. It in combination with manually doing the class
maps is the only way I've been able to prevent them from being saved.
If I take out my CustomDefaultValueConvention or the manual class
maps, the values get saved. I was really hoping that my
CustomDefaultValueConvention in combination with the provided
NeverSerializeDefaultValueConvention would work so that I don't have
to manually map every single field I want to prevent defaults for.
Below is an example of what is working. I'll get the source and look
through it and the unit tests to see why it isn't doing what I think
it should when I have a little more time to devote to it.

var conventions = new ConventionProfile();
conventions.SetDefaultValueConvention(new
CustomDefaultValueConvention());
conventions.SetSerializeDefaultValueConvention(new
NeverSerializeDefaultValueConvention());
conventions.SetIgnoreExtraElementsConvention(new
AlwaysIgnoreExtraElementsConvention());
conventions.SetIgnoreIfNullConvention(new
AlwaysIgnoreIfNullConvention());
BsonClassMap.RegisterConventions(conventions, t => t.FullName != null
&& t.FullName.StartsWith("Asi.AccuAuto"));

BsonClassMap.RegisterClassMap<Policy>(cm =>
{
cm.AutoMap();
cm.GetMemberMap(c =>
c.CommissionId).SetSerializeDefaultValue(false);
cm.GetMemberMap(c =>
c.PriorPolicyId).SetSerializeDefaultValue(false);
cm.GetMemberMap(c =>
c.ManagementCompanyId).SetSerializeDefaultValue(false);
});

On Aug 23, 5:54 pm, sridhar <srid...@10gen.com> wrote:
> NeverSerializeDefaultValueConvention is used in conjunction with
> BsonDefaultValue. If you specify a default value for a field, you then
> use the NeverSerializeDefaultValueConvention to not serialize it. An
> example of this can be found in the unit tests for the driver at
> BsonUnitTests\DefaultSerializer\Attributes\BsonAttributeTests.cs
>
> If you want attributes to not be serialized then tag the field/
> property using BsonIgnore as specified inhttp://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutor....

sridhar

unread,
Aug 23, 2011, 8:20:20 PM8/23/11
to mongodb-user
Thanks for the sample code. I will look into this too.

Robert Stam

unread,
Aug 24, 2011, 11:17:01 AM8/24/11
to mongodb-user
I've created a JIRA ticket for this:

https://jira.mongodb.org/browse/CSHARP-310

At the very least there is a usability concern about how to correctly
use NeverSerializeDefaultValueConvention, but as stated in the ticket,
I believe there are one or more minor bugs preventing this from
working correctly.

The JIRA ticket has an attachment (called "Program.cs") with some
sample code (a full program) adapted from the sample code provided by
Kevin (thanks Kevin!).

Kevin Lewis

unread,
Aug 24, 2011, 4:45:33 PM8/24/11
to mongodb-user
Excellent, glad to know I wasn't crazy :)
Reply all
Reply to author
Forward
0 new messages