-
Notifications
You must be signed in to change notification settings - Fork 47
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
Custom type resolver #451
Comments
Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details. |
Thanks for writing in @netaques and for the feedback. I am in agreement that the documentation could be better. It could always be better. 😁 I have given it a few tries now and there seems to always be a few areas that could see more love. Unfortunately, my time is limited these days (#383) so we will have to lean on perhaps some contributions from the community if we can get to that point. That stated, what you are looking for is interception, which is actually something we almost did with #264 as a first pass but then relegated it to basic monitoring. The thought was that we could introduce interception if it was something that the community requested. So it was basically easy enough to copy/paste/modify a new feature for you. Please check out this build here: I have added a passing test demonstrating how to use this feature here: home/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue451Tests.cs Lines 11 to 68 in 4ac9d77
Please let me know if this will allow you to do what you are aiming to achieve and/or if you have any further suggestions/additions to the interface/API and I will look into it for you. 👍 |
@Mike-E-angelo thank you very much for fast reaction. This feature is precisely what I am looking for. Maybe a non generic version would be nice (using [Test]
public void TestInterceptor()
{
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Processor>
<Enabled>true</Enabled>
<Filters>
<Filter>
<Type>ISO</Type>
</Filter>
</Filters>
</Processor>";
var serializer = new ConfigurationContainer()
.EnableImplicitlyDefinedDefaultValues()
.EnableMemberExceptionHandling()
.EnableImplicitTyping(typeof(Processor))
.Type<Processor>()
.WithInterceptor(new Interceptor())
.Create();
var contentStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
Processor result;
using (var reader = XmlReader.Create(contentStream))
{
result = (Processor)serializer.Deserialize(reader);
}
}
public class Interceptor : ISerializationInterceptor<Processor>
{
public Processor Serializing(IFormatWriter writer, Processor instance)
{
return instance;
}
public Processor Activating(Type instanceType)
{
// processor should be retrieved from IoC container, but created manually for simplicity of test
var processor = new Processor(new Service());
return processor;
}
public Processor Deserialized(IFormatReader reader, Processor instance)
{
return instance;
}
}
public interface IService
{
}
class Service : IService
{
}
public class Processor
{
private readonly IService _service;
public bool Enabled { get; set; }
public List<Filter> Filters { get; set; }
public Processor(IService service)
{
_service = service;
Filters = new List<Filter>();
}
}
public class Filter
{
public string Type { get; set; }
} |
OK great @netaques thank you for the sample code I have reproduced it on my end and am looking into this now. I will also add support for |
Alright @netaques check this build out here: I have also added your test to the suite. Additionally, I have added support for a generalized/non-generic interceptor by way of a home/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue451Tests_Reported.cs Lines 16 to 78 in a00f7af
Please let me know if there are any further issues you encounter. If not, I will deploy this to NuGet tomorrow. 🤞 |
Thank you very much, @Mike-E-angelo. I really appreciate so fast reaction. I can confirm that with your latest commit everything works like a charm. I believe this feature will help to other developers as well. |
Great! Good to hear the good news, @netaques. I have posted this to NuGet as https://www.nuget.org/packages/ExtendedXmlSerializer/ Thank you for improving ExtendedXmlSerializer! Please do let me know of any additional issues you find around this by posting a comment here and I will look into it for you. Closing for now.
|
Is it possible to control a creation of objects during deserialization? I have a class to be deserialized that does not has a default constructor because I need inject dependencies into it. Ideally I would like to find a place where ExtendedXmlSerializer tell me: "Here is a type, give me an instance of that". I think that Extend method and ISerializerExtension might be a way but I really do not know how since documentation about this topic is quite poor.
The text was updated successfully, but these errors were encountered: