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
Comments
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 ? |
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. |
I understand, and this makes sense. Let's have a look at your PR then. |
The PR is available. Let me know what you think. |
A fix for this issue has been released in version 11.1.0. |
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.
The text was updated successfully, but these errors were encountered: