Skip to content

Unable to serialize types that implement IEnumerable but are not collections #169

@samcragg

Description

@samcragg

We're trying to serialize a type that has different properties but also implements IEnumerable, which causes MsgPack to think it's a collection. Is there a way of marking the type so it can be serialized normally (the actual class has a lot of properties to have to create a custom serializer by hand). Here's a quick demo of what I mean:

[Serializable]
public class NonCollectionType : IEnumerable<int>
{
    public int Property { get; set; }

    public IEnumerator<int> GetEnumerator()
    {
        yield break;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

As soon as you try to call GetSerializer<NonCollectionType> it will throw an exception saying the Add method is missing. I've also tried to implement the interface explicitly, however, it still thinks it's a collection...

I've managed to smash the source to do what I want by modifying ReflectionExtensions.GetCollectionTraits and ReflectionExtensions.TryCreateCollectionTraitsForHasGetEnumeratorType to check if the add method is null before creating the collection traits, however, this causes some of the unit tests to fail.

Perhaps another option would be to add a property to the SerializationContext of NonCollectionTypes (similar to how you have DefaultCollectionTypes) that you can register your type with and then you can quickly check for that at the start of the GetCollectionTraits method (we're trying to avoid custom attributes as the assembly defining the datatypes doesn't have a reference to MsgPack)

Let me know if you like either of the ideas and I can submit a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRequires or request to feature enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions