Skip to content

Commit

Permalink
Added support for DataContractSerializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Harmon committed May 17, 2018
1 parent d10fa71 commit 3560ee3
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 8 deletions.
11 changes: 10 additions & 1 deletion SimpleSOAPClient.sln
Expand Up @@ -21,7 +21,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "other", "other", "{6C152128
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "resources", "resources", "{A2159B1B-25D6-4AFA-A30A-22207B226377}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleSOAPClient", "src\SimpleSOAPClient\SimpleSOAPClient.csproj", "{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleSOAPClient", "src\SimpleSOAPClient\SimpleSOAPClient.csproj", "{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{63D634C9-3F19-4305-8831-D47A380C9EAB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleSOAPClient.Tests", "tests\SimpleSOAPClient.Tests\SimpleSOAPClient.Tests.csproj", "{07625BFB-1644-415D-B2B8-468152E05C3C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +37,10 @@ Global
{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA}.Release|Any CPU.Build.0 = Release|Any CPU
{07625BFB-1644-415D-B2B8-468152E05C3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07625BFB-1644-415D-B2B8-468152E05C3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07625BFB-1644-415D-B2B8-468152E05C3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07625BFB-1644-415D-B2B8-468152E05C3C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -41,6 +49,7 @@ Global
{E136A35F-1EC1-4A90-9369-C278E33BA475} = {A2159B1B-25D6-4AFA-A30A-22207B226377}
{6C152128-CC1B-4140-9FE1-8E86B013AB88} = {A2159B1B-25D6-4AFA-A30A-22207B226377}
{BD232C77-3DEB-4CB1-8BC7-30A4C1A50DEA} = {68E461E4-64AD-4CC4-959D-613F5C84242B}
{07625BFB-1644-415D-B2B8-468152E05C3C} = {63D634C9-3F19-4305-8831-D47A380C9EAB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B2AD5ABD-A184-4781-910A-F187301694F8}
Expand Down
43 changes: 36 additions & 7 deletions src/SimpleSOAPClient/Helpers/XmlHelpers.cs
Expand Up @@ -20,13 +20,25 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion
#endregion

using System.Runtime.CompilerServices;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System;

[assembly: InternalsVisibleTo("SimpleSOAPClient.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e9d4b3865537a3" +
"5aabe1076ab836c58e47a3315970568d17b1d58b6d08a648e6333a714112adeb9481d79cd3a529" +
"ab6ee0e643b9098fa703ca085202968cb4792fc1ccc0d85fff62ed01993ed67f5cf5c2fac83622" +
"e019654eab372c6c4ecefbc8198b267ed757b30da82779857ca6861204961aa175ef48a7e79ad3" +
"b0d754bd")]
namespace SimpleSOAPClient.Helpers
{
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;




/// <summary>
/// Helper class with extensions for XML manipulation
Expand Down Expand Up @@ -60,6 +72,15 @@ public static string ToXmlString<T>(this T item, bool removeXmlDeclaration)
NamespaceHandling = NamespaceHandling.OmitDuplicates
}))
{
#if NETSTANDARD2_0 || NET45
if (Attribute.IsDefined(item.GetType(), typeof(System.Runtime.Serialization.DataContractAttribute)))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
serializer.WriteObject(xmlWriter, item);
xmlWriter.Flush();
return textWriter.ToString();
}
#endif
new XmlSerializer(item.GetType())
.Serialize(xmlWriter, item, EmptyXmlSerializerNamespaces);
return textWriter.ToString();
Expand Down Expand Up @@ -111,10 +132,18 @@ public static T ToObject<T>(this string xml)
{
if (string.IsNullOrWhiteSpace(xml)) return default(T);

using (var textWriter = new StringReader(xml))
using (var stringReader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(stringReader))
{
var result = (T)new XmlSerializer(typeof(T)).Deserialize(textWriter);
#if NETSTANDARD2_0 || NET45
if (Attribute.IsDefined(typeof(T), typeof(System.Runtime.Serialization.DataContractAttribute)))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(xmlReader);
}
#endif

var result = (T)new XmlSerializer(typeof(T)).Deserialize(xmlReader);
return result;
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/SimpleSOAPClient.Tests/DataContractModel.cs
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace SimpleSOAPClient.Tests
{
[DataContract(Name = "DataContractModel", Namespace ="urn:simplesoapclient:test")]
public class DataContractModel
{
[DataMember(Order = 0)]
public string String { get; set; }

[DataMember(Order = 1)]
public int Int { get; set; }

[DataMember(Order = 2)]
public bool Bool { get; set; }

[DataMember(Order = 3)]
public string[] Array { get; set; }
}
}
24 changes: 24 additions & 0 deletions tests/SimpleSOAPClient.Tests/SimpleSOAPClient.Tests.csproj
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyOriginatorKeyFile>../../tools/SimpleSOAPClient.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<SignAssembly>true</SignAssembly>

<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SimpleSOAPClient\SimpleSOAPClient.csproj" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions tests/SimpleSOAPClient.Tests/XmlHelpersTests.cs
@@ -0,0 +1,87 @@
using SimpleSOAPClient.Helpers;
using System.Collections.Generic;
using System.Xml.Linq;
using Xunit;

namespace SimpleSOAPClient.Tests
{
public class XmlHelpersTests
{
const string SERIALIZED_XML = @"<XmlModel xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""urn:simplesoapclient:test""><String>foo</String><Int>42</Int><Bool>true</Bool><Array><string>One</string><string>Two</string><string>Three</string></Array></XmlModel>";
const string SERIALIZED_DATACONTRACT = @"<DataContractModel xmlns=""urn:simplesoapclient:test"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><String>foo</String><Int>42</Int><Bool>true</Bool><Array xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>One</a:string><a:string>Two</a:string><a:string>Three</a:string></Array></DataContractModel>";

readonly XmlModel _xmlModel = new XmlModel
{
String = "foo",
Bool = true,
Int = 42,
Array = new[] { "One", "Two", "Three" }
};

readonly DataContractModel _dataContractModel = new DataContractModel
{
String = "foo",
Bool = true,
Int = 42,
Array = new[] { "One", "Two", "Three" }
};

[Fact]
public void ToObject_XmlSerializer()
{
var result = XmlHelpers.ToObject<XmlModel>(SERIALIZED_XML);
Assert.Equal("foo", result.String);
Assert.True(result.Bool);
Assert.Equal(42, result.Int);
Assert.Equal(3, result.Array.Length);
}

[Fact]
public void ToObject_DataContractSerializer()
{
var result = XmlHelpers.ToObject<DataContractModel>(SERIALIZED_DATACONTRACT);
Assert.Equal("foo", result.String);
Assert.True(result.Bool);
Assert.Equal(42, result.Int);
Assert.Equal(3, result.Array.Length);
}


[Fact]
public void ToXElement_XmlSerializer()
{
var result = XmlHelpers.ToXElement(_xmlModel);
var actualXml = result.ToString();
Assert.Equal(SERIALIZED_XML, actualXml, new XmlEqualityComparer());
}

[Fact]
public void ToXElement_DataContractSerializer()
{
var result = XmlHelpers.ToXElement(_dataContractModel);
var actualXml = result.ToString();
Assert.Equal(SERIALIZED_DATACONTRACT, actualXml, new XmlEqualityComparer());
}

[Fact]
public void ToXmlString_XmlSerializer()
{
var result = XmlHelpers.ToXmlString(_xmlModel);
Assert.Equal(SERIALIZED_XML, result, new XmlEqualityComparer());
}

[Fact]
public void ToXmlString_DataContractSerializer()
{
var result = XmlHelpers.ToXmlString(_dataContractModel);
Assert.Equal(SERIALIZED_DATACONTRACT, result, new XmlEqualityComparer());
}

class XmlEqualityComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y) => XmlNormalizer.DeepEqualsWithNormalization(XDocument.Parse(x), XDocument.Parse(y), null);

public int GetHashCode(string obj) => XmlNormalizer.Normalize(XDocument.Parse(obj), null).GetHashCode();
}
}
}
20 changes: 20 additions & 0 deletions tests/SimpleSOAPClient.Tests/XmlModel.cs
@@ -0,0 +1,20 @@
using System.Xml.Serialization;

namespace SimpleSOAPClient.Tests
{
[XmlRoot(Namespace = "urn:simplesoapclient:test")]
public class XmlModel
{
[XmlElement(Order = 0)]
public string String { get; set; }

[XmlElement(Order = 1)]
public int Int { get; set; }

[XmlElement(Order = 2)]
public bool Bool { get; set; }

[XmlArray(Order = 3)]
public string[] Array { get; set; }
}
}

0 comments on commit 3560ee3

Please sign in to comment.