-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adjusted SerializationInterceptionExtension to account for activate…
…d types. - Added support for a generalized interceptor.
- Loading branch information
1 parent
94151ad
commit a00f7af
Showing
5 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/ExtendedXmlSerializer/ExtensionModel/Instances/SerializationActivator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using ExtendedXmlSerializer.ContentModel.Format; | ||
using System; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel.Instances | ||
{ | ||
/// <summary> | ||
/// A generalized serialization interceptor base class for convenience. | ||
/// </summary> | ||
public abstract class SerializationActivator : ISerializationInterceptor | ||
{ | ||
/// <inheritdoc /> | ||
public virtual object Serializing(IFormatWriter writer, object instance) => instance; | ||
|
||
/// <inheritdoc /> | ||
public abstract object Activating(Type instanceType); | ||
|
||
/// <inheritdoc /> | ||
public virtual object Deserialized(IFormatReader reader, object instance) => instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue451Tests_Reported.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.ExtensionModel.Instances; | ||
using FluentAssertions; | ||
using JetBrains.Annotations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue451Tests_Reported | ||
{ | ||
[Fact] | ||
public void TestInterceptor() | ||
{ | ||
// language=XML | ||
const string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<Issue451Tests_Reported-Processor> | ||
<Enabled>true</Enabled> | ||
<Filters> | ||
<Filter> | ||
<Type>ISO</Type> | ||
</Filter> | ||
</Filters> | ||
</Issue451Tests_Reported-Processor>"; | ||
|
||
var serializer = new ConfigurationContainer().EnableImplicitTyping(typeof(Processor)) | ||
.Type<Processor>() | ||
.WithInterceptor(new Interceptor()) | ||
.Create(); | ||
|
||
var contentStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); | ||
using var reader = XmlReader.Create(contentStream); | ||
var processor = (Processor)serializer.Deserialize(reader); | ||
processor.Should().NotBeNull(); | ||
processor.Enabled.Should().BeTrue(); | ||
processor.Filters.Should().NotBeEmpty(); | ||
processor.Filters.Only().Type.Should().Be("ISO"); | ||
} | ||
|
||
public class Interceptor : SerializationActivator | ||
{ | ||
public override object 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 interface IService {} | ||
|
||
class Service : IService {} | ||
|
||
public class Processor | ||
{ | ||
// ReSharper disable once NotAccessedField.Local | ||
readonly IService _service; | ||
|
||
public bool Enabled { get; set; } | ||
|
||
public List<Filter> Filters { [UsedImplicitly] get; set; } | ||
|
||
public Processor(IService service) | ||
{ | ||
_service = service; | ||
|
||
Filters = new List<Filter>(); | ||
} | ||
} | ||
|
||
public class Filter | ||
{ | ||
public string Type { get; set; } | ||
} | ||
} | ||
} |