Serialize .NET struct types and custom serializers

504 views
Skip to first unread message

Sign Theta

unread,
Dec 28, 2019, 10:13:08 PM12/28/19
to mongodb-csharp
Hi Guys,

I am new to MongoDB and have been going through the C# driver doc and am a little lost as to how to implement custom serializers. Could anyone help me on this please?

It all started when I tried to store an object that contained System.Drawing.Color as one of its properties and it turns out, because Color is a struct and structs are value type and therefore it has to be custom handled for MongoDB.

i have trawled the web for info and could not find that works for me. one site that suggested a method here but it did not work for me as it was throwing exception "ReadBsonType can only be called when State is Type, not when State is Value"

I modified the code linked above as follows but now at fieldInfo.SetValue(result, value); it says no set method found. i understand what is happening but not sure WHY it is though.

could any one help or at least suggest an alternative solution>

thanks in advance!


public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
                {
                        var reader = context.Reader;
                        var nt = args.NominalType;
                        var result = (object)(new T());    //Activator.CreateInstance(nt));

                        reader.ReadStartDocument();
                        while (reader.State != BsonReaderState.EndOfDocument)
                        {
                                var state = context.Reader.State;

                                Debug.WriteLine($"State: {state}");

                                switch (state)
                                {
                                        case BsonReaderState.Name:
                                                var name = reader.ReadName(new Utf8NameDecoder());
                                                Debug.WriteLine(name);
                                                var fieldInfo = nt.GetField(name);
                                                object value = null;

                                                if (fieldInfo != null)
                                                {
                                                        value = BsonSerializer.Deserialize(reader, fieldInfo.FieldType);
                                                        fieldInfo.SetValue(result, value);
                                                        Debug.WriteLine($"Name: {name}, Value: {value}");
                                                }
                                                else
                                                {
                                                        var propertyInfo = nt.GetProperty(name);

                                                        if (propertyInfo != null)
                                                        {
                                                                value = BsonSerializer.Deserialize(reader, propertyInfo.PropertyType);

                                                                if (!propertyInfo.CanWrite) break;
                                                                propertyInfo.SetValue(result, value, null);
                               
                                                                Debug.WriteLine($"Name: {name}, Value: {value}");
                                                        }
                                                }
                                                break;

                                        case BsonReaderState.Type:
                                                var type = reader.ReadBsonType();
                                                Debug.WriteLine(type);
                                                break;

                                        case BsonReaderState.Value:
                                                reader.SkipValue();
                                                break;
                                }
                        }

            reader.ReadEndDocument();

                        Debug.WriteLine("============================");

                        return (T) result;
                }

Sign Theta

unread,
Dec 29, 2019, 12:36:05 AM12/29/19
to mongodb-csharp
OK, Just checked the doco and it seems that the A,R,G,B props are computed params so it makes sense as to why the set method is not found...

... but i still need regarding serializing value types.
Reply all
Reply to author
Forward
0 new messages