mongodb-csharp-driver ignore if empty attribute

1,184 views
Skip to first unread message

Jentor

unread,
Dec 27, 2011, 6:57:29 AM12/27/11
to mongodb-user
Current serialization implementation always adds new array item into
final result when serializing IEnumerable items, even if array is
empty (but not null). Is it possible to write custom attribute
IgnoreIfEmpty ? Or should I write my own IgnoreIfNull convention,
which would check whether item.Count == 0? If so, can you please help
me with that. Thanks.

craiggwilson

unread,
Dec 27, 2011, 8:55:31 AM12/27/11
to mongod...@googlegroups.com
Yes, looks like that convention is probably named inappropriately.  It should probably be IIgnoreConvention.  Regardless, that is the place you can hook in to indicate that you don't want to serialize empty elements.

Alex Brown

unread,
Jul 3, 2012, 12:37:11 PM7/3/12
to mongod...@googlegroups.com
I'm also looking for this...
Could you submit a patch?

craiggwilson

unread,
Jul 5, 2012, 9:48:43 AM7/5/12
to mongod...@googlegroups.com
I'm sorry, Alex, but what are you looking for?

Alex Brown

unread,
Jul 5, 2012, 10:02:23 AM7/5/12
to mongod...@googlegroups.com
Looking for a way of ignoring empty IEnumerables... so they don't get serialized.

craiggwilson

unread,
Jul 5, 2012, 10:52:14 AM7/5/12
to mongod...@googlegroups.com
I don't believe there is currently a way to do this.  If you need this functionality, file a jira at jira.mongodb.org and we'll look at it and prioritize.

craiggwilson

unread,
Jul 5, 2012, 11:05:38 AM7/5/12
to mongod...@googlegroups.com
Alex, Robert had another suggestion.  For each property, we look for a method called ShouldSerializeXXX, where XXX is your property name.  You can return true or false here as to whether or not to serialize the value.

public class Person
{
   //...

   public IEnumerable<string> Addresses { get; set; }

   private bool ShouldSerializeAddresses()
   {
       return Addresses.Count() > 0;

Alex Brown

unread,
Jul 5, 2012, 12:19:40 PM7/5/12
to mongod...@googlegroups.com
Using your example of Person / Addresses, I used the following, which works:

            BsonClassMap.RegisterClassMap<Person>(cm =>
            {
                cm.AutoMap();

                cm.GetMemberMap(c => c.CompetitorBests)
                    .SetShouldSerializeMethod(
                        obj => ((Person)obj).Addresses.Any());
            });



--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Dotnet Developer_C

unread,
Jul 25, 2014, 5:54:46 PM7/25/14
to mongod...@googlegroups.com
May I please know where should we write the BsonClassMap.RegisterClassMap .....................................etc. If I write in my .cs file(which is a model file in mvc) Visual studio is giving me a red squiggle saying that its used as a type instead BsonClassMap.Regis....is a method.

 public class class_name
    {
        public int xxx { get; set; }
        public string xxxx { get; set; }
        public List<string> ABC{ get; set; }

        private bool ShouldSerializeABC()
        {

            return ABC.Count() > 0;
        }
      
       BsonClassMap.RegisterClassMap<class_name>(cm=>
    {
    cm.AutoMap();
         cm.GetMemberMap(c => c.CompetitorBests)
                    .SetShouldSerializeABC(
                        obj => ((class_name)obj).ABC.Any());
    
    }      
Reply all
Reply to author
Forward
0 new messages