Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for deserialization of interface types #601

Closed
dotlegacy opened this issue Apr 4, 2021 · 5 comments
Closed

Support for deserialization of interface types #601

dotlegacy opened this issue Apr 4, 2021 · 5 comments
Labels
feedback-needed More feedback is required

Comments

@dotlegacy
Copy link
Contributor

dotlegacy commented Apr 4, 2021

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
I was wondering if there is a particular reason for not supporting the deserialization of interface types by allowing the user to specify the mapping of the interface type to the concrete type. I am aware that we can use tags to support the mapping of interface types but I believe the yaml output is a lot cleaner without using the tags.

Describe the solution you'd like
A clear and concise description of what you want to happen.
var deserializer = new DeserializerBuilder()
.WithTypeMapping<ICar, Car>()
.Build();

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

I have successfully created a POC of this with some slight modifications to the existing DefaultObjectFactory and added a custom MappingNodeTypeResolver that simply updates the currrentType (which is an interface) to return the concrete type from the mapping.

Let me know if this is something that would fit this project and I will push a PR.

@aaubry
Copy link
Owner

aaubry commented Apr 5, 2021

The supported way to do this is to register a custom object factory as follows:

var deserializer = new DeserializerBuilder()
    .WithObjectFactory(type =>
    {
        if (type == typeof(ICar))
        {
            return new Car();
        }
        
        return defaultFactory.Create(type);
    })
    .Build();
}

Isn't this good enough ?

@aaubry aaubry added the feedback-needed More feedback is required label Apr 5, 2021
@dotlegacy
Copy link
Contributor Author

This will work if my interface contract has a public set method but I personally choose to have immutable objects in my design. It also gets messier if there are multiple interfaces that I need to map. I believe adding the proposed method will look cleaner and certainly follows the fluent API you have for the builder class.

@aaubry
Copy link
Owner

aaubry commented Apr 5, 2021

I understand, and this makes sense. Let's have a look at your PR then.

@dotlegacy
Copy link
Contributor Author

The PR is available. Let me know what you think.

@aaubry aaubry closed this as completed in b722b07 Apr 8, 2021
@aaubry
Copy link
Owner

aaubry commented Apr 8, 2021

A fix for this issue has been released in version 11.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback-needed More feedback is required
Projects
None yet
Development

No branches or pull requests

2 participants