Skip to content

Error serializing data filtered by Linq #204

@juho-hanhimaki

Description

@juho-hanhimaki

I am running into an issue serializing collections that are filtered with Linq. For example the following example fails on the collection that is filtered using Linq.

            var people = new List<MyClass>()
            {
                new MyClass{Age = 99,FirstName = "hoge",LastName = "huga",},
                new MyClass{Age = 53,FirstName = "a",LastName = "b",},
                new MyClass{Age = 3,FirstName = "d",LastName = "c",},
            };

            // ok
            IEnumerable<MyClass> listAsEnumerable = (IEnumerable<MyClass>)people;
            var bytes = MessagePack.MessagePackSerializer.Typeless.Serialize(listAsEnumerable);
            var obj = MessagePackSerializer.Typeless.Deserialize(bytes);

            // fails in serialization
            IEnumerable<MyClass> filteredEnumerable = people.Where(f => f.Age > 18);
            bytes = MessagePack.MessagePackSerializer.Typeless.Serialize(filteredEnumerable);
            obj = MessagePackSerializer.Typeless.Deserialize(bytes);

...
    [MessagePackObject]
    public class MyClass
    {
        // Key is serialization index, it is important for versioning.
        [Key(0)]
        public int Age { get; set; }

        [Key(1)]
        public string FirstName { get; set; }

        [Key(2)]
        public string LastName { get; set; }
    }

The filtered collection is IEnumerable. As workaround I could call .ToArray() on the IEnumerable, but that would not be ideal as it would allocate new array in memory.

The exception is:
MessagePack.Internal.MessagePackDynamicObjectResolverException: 'can't find matched constructor. type:System.Func`2[[ConsoleApp9.MyClass, ConsoleApp9, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions