Skip to content

Commit

Permalink
Added referenced (existing instance) deserializer overload for XmlReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Jul 29, 2024
1 parent c493567 commit e474001
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Text;
using System.Xml;
using XmlReader = System.Xml.XmlReader;

namespace ExtendedXmlSerializer.Configuration
{
Expand Down Expand Up @@ -32,7 +33,8 @@ public ReferencedDeserializationContext(IExtendedXmlSerializer @this, T existing
/// <param name="serializer">The serializer to configure.</param>
/// <param name="existing">The existing target instance.</param>
/// <param name="settings">The xml reader settings used for deserialization.</param>
public ReferencedDeserializationContext(IExtendedXmlSerializer serializer, T existing, XmlReaderSettings settings)
public ReferencedDeserializationContext(IExtendedXmlSerializer serializer, T existing,
XmlReaderSettings settings)
: this(InstanceReaders.Default.Get(serializer), existing, settings) {}

ReferencedDeserializationContext(IInstanceReader reader, T existing, XmlReaderSettings settings)
Expand Down Expand Up @@ -76,7 +78,18 @@ public T Deserialize(XmlReaderSettings settings, string data)
/// <returns>The initial provided target instance, assigned with values discovered in the provided document.</returns>
public T Deserialize(XmlReaderSettings settings, Stream stream)
{
var reader = new XmlReaderFactory(settings, settings.NameTable.Context()).Get(stream);
using var reader = new XmlReaderFactory(settings, settings.NameTable.Context()).Get(stream);
return Deserialize(reader);
}

/// <summary>
/// Deserializes a document represented by the provided stream and assigns any values into the provided instance
/// context, using the provided reader settings.
/// </summary>
/// <param name="reader">The reader representing the source document.</param>
/// <returns>The initial provided target instance, assigned with values discovered in the provided document.</returns>
public T Deserialize(XmlReader reader)
{
var existing = new Existing(reader, _existing);
var result = (T)_reader.Get(existing);
return result;
Expand Down
77 changes: 77 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue630Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.ExtensionModel.Instances;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using System;
using System.Xml.Serialization;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue630Tests
{
[Fact]
public void Verify()
{
var container = new ConfigurationContainer().Create().ForTesting();
var instance = new Terminate(true);
container.Cycle(instance).All.Should().BeTrue();
}

[Fact]
public void VerifyPassThroughValue()
{
var sut = new ConfigurationContainer().Type<Envelope>().WithMonitor(Monitor.Default).Create().ForTesting();

var instance = new Envelope { Version = 1.2f, SubObject = new() };
sut.Cycle(instance).SubObject.Version.Should().Be(1.2f);
}

sealed class Monitor : ISerializationMonitor<Envelope>
{
public static Monitor Default { get; } = new();

Monitor() {}

public void OnSerializing(IFormatWriter writer, Envelope instance) {}

public void OnSerialized(IFormatWriter writer, Envelope instance) {}

public void OnDeserializing(IFormatReader reader, Type instanceType) {}

public void OnActivating(IFormatReader reader, Type instanceType) {}

public void OnActivated(Envelope instance) {}

public void OnDeserialized(IFormatReader reader, Envelope instance)
{
instance.SubObject.Version = instance.Version;
}
}

sealed class Envelope
{
public float Version {get; set;}
public SubObject SubObject { get; set; }
}
sealed class SubObject {
public string Name {get; set;}
public float Version {get; set;}
}


public sealed class Terminate {
public Terminate() : this(null) {}

public Terminate(bool? all) => _all = all;

[XmlElement(ElementName = "All")]
private bool? _all = null;
public bool All {
get { return _all != null; }
}
}

}
}

0 comments on commit e474001

Please sign in to comment.