Skip to content

Commit

Permalink
Add support for System.Runtime.Serialization attributes. #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Sep 10, 2018
1 parent b4e9b3e commit da40c18
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Gu.Xml.Tests/Helpers/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static string DataContractSerializer(object value)
}

return sb.Replace("utf-16", "utf-8")
.Replace("xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Gu.Xml.Tests\"", string.Empty)
.Replace(" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Gu.Xml.Tests\"", string.Empty)
.ToString();
}
}
Expand Down
5 changes: 4 additions & 1 deletion Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class SerializeWithDataContractAttributes
[TestCaseSource(nameof(Values))]
public void Serialize(object value)
{
var expected = Reference.DataContractSerializer(value);
var expected = Reference.DataContractSerializer(value)
.Replace("XmlTests.SerializeWithDataContractAttributes.", string.Empty);
var actual = Xml.Serialize(value);
if (actual == expected)
{
Expand Down Expand Up @@ -53,12 +54,14 @@ public void Serialize(object value)
[DataContract]
public class WithDataContractAttribute
{
[DataMember]
public int Value { get; set; } = 1;
}

[DataContract(Name = "Name")]
public class WithDataContractAttributeExplicitName
{
[DataMember]
public int Value { get; set; } = 1;
}

Expand Down
28 changes: 16 additions & 12 deletions Gu.Xml/Internals/RootName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Xml.Serialization;

internal static class RootName
{
Expand All @@ -16,18 +15,11 @@ public static string Get(Type type)

private static string Create(Type type)
{
if (Attribute.GetCustomAttribute(type, typeof(XmlRootAttribute)) is XmlRootAttribute xmlRoot &&
xmlRoot.ElementName is string elementName &&
!string.IsNullOrEmpty(elementName))
if (TryGetNameFromAttribute<System.Xml.Serialization.XmlRootAttribute>(type, x => x.ElementName, out var name) ||
TryGetNameFromAttribute<System.Xml.Serialization.SoapTypeAttribute>(type, x => x.TypeName, out name) ||
TryGetNameFromAttribute<System.Runtime.Serialization.DataContractAttribute>(type, x => x.Name, out name))
{
return elementName;
}

if (Attribute.GetCustomAttribute(type, typeof(SoapTypeAttribute)) is SoapTypeAttribute soapType &&
soapType.TypeName is string typeName &&
!string.IsNullOrEmpty(typeName))
{
return typeName;
return name;
}

if (!type.IsGenericType &&
Expand Down Expand Up @@ -69,5 +61,17 @@ void Append(Type current)
}
}
}

private static bool TryGetNameFromAttribute<T>(Type type, Func<T, string> getName, out string name)
where T : Attribute
{
name = null;
if (Attribute.GetCustomAttribute(type, typeof(T)) is T attribute)
{
name = getName(attribute);
}

return !string.IsNullOrEmpty(name);
}
}
}
16 changes: 8 additions & 8 deletions Gu.Xml/Writers/ElementWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;

public abstract class ElementWriter
{
Expand Down Expand Up @@ -38,7 +37,7 @@ bool IsIgnoredCalculated()
{
return property.SetMethod == null &&
Attribute.GetCustomAttribute(property.GetMethod, typeof(CompilerGeneratedAttribute)) == null &&
Attribute.GetCustomAttribute(property.GetMethod, typeof(XmlElementAttribute)) == null;
Attribute.GetCustomAttribute(property.GetMethod, typeof(System.Xml.Serialization.XmlElementAttribute)) == null;
}
}

Expand Down Expand Up @@ -78,21 +77,22 @@ private static ElementWriter<TSource, TValue> CreateWriter<TSource, TValue>(stri
private static bool TryGetElementName(MemberInfo member, out string name)
{
name = null;
if (Attribute.GetCustomAttribute(member, typeof(XmlElementAttribute)) is XmlElementAttribute xmlAttribute)
if (Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.XmlElementAttribute)) is System.Xml.Serialization.XmlElementAttribute xmlAttribute)
{
name = xmlAttribute.ElementName ?? string.Empty;
}
else if (Attribute.GetCustomAttribute(member, typeof(SoapElementAttribute)) is SoapElementAttribute soapAttribute)
else if (Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.SoapElementAttribute)) is System.Xml.Serialization.SoapElementAttribute soapAttribute)
{
name = soapAttribute.ElementName ?? string.Empty;
}

if (name == null)
{
if (Attribute.GetCustomAttribute(member, typeof(XmlIgnoreAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(XmlAttributeAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(SoapIgnoreAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(SoapAttributeAttribute)) != null)
if (Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.XmlIgnoreAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.XmlAttributeAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.SoapIgnoreAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(System.Xml.Serialization.SoapAttributeAttribute)) != null ||
Attribute.GetCustomAttribute(member, typeof(System.Runtime.Serialization.IgnoreDataMemberAttribute)) != null)
{
return false;
}
Expand Down

0 comments on commit da40c18

Please sign in to comment.