From da40c18a4eb35f09803799a29a935a5589a960f5 Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Mon, 10 Sep 2018 12:16:41 +0200 Subject: [PATCH] Add support for System.Runtime.Serialization attributes. #20. --- Gu.Xml.Tests/Helpers/Reference.cs | 2 +- ...sts.SerializeWithDataContractAttributes.cs | 5 +++- Gu.Xml/Internals/RootName.cs | 28 +++++++++++-------- Gu.Xml/Writers/ElementWriter.cs | 16 +++++------ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Gu.Xml.Tests/Helpers/Reference.cs b/Gu.Xml.Tests/Helpers/Reference.cs index 8b5c868..0f56791 100644 --- a/Gu.Xml.Tests/Helpers/Reference.cs +++ b/Gu.Xml.Tests/Helpers/Reference.cs @@ -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(); } } diff --git a/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs b/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs index d8c7c90..3defafe 100644 --- a/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs +++ b/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs @@ -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) { @@ -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; } diff --git a/Gu.Xml/Internals/RootName.cs b/Gu.Xml/Internals/RootName.cs index 070b9b9..c1688fe 100644 --- a/Gu.Xml/Internals/RootName.cs +++ b/Gu.Xml/Internals/RootName.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Concurrent; using System.Text; - using System.Xml.Serialization; internal static class RootName { @@ -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(type, x => x.ElementName, out var name) || + TryGetNameFromAttribute(type, x => x.TypeName, out name) || + TryGetNameFromAttribute(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 && @@ -69,5 +61,17 @@ void Append(Type current) } } } + + private static bool TryGetNameFromAttribute(Type type, Func 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); + } } } diff --git a/Gu.Xml/Writers/ElementWriter.cs b/Gu.Xml/Writers/ElementWriter.cs index 186438b..1695c68 100644 --- a/Gu.Xml/Writers/ElementWriter.cs +++ b/Gu.Xml/Writers/ElementWriter.cs @@ -3,7 +3,6 @@ using System; using System.Reflection; using System.Runtime.CompilerServices; - using System.Xml.Serialization; public abstract class ElementWriter { @@ -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; } } @@ -78,21 +77,22 @@ private static ElementWriter CreateWriter(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; }