Skip to content

Commit

Permalink
Support XmlArrayAttribute #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Sep 12, 2018
1 parent d504aa3 commit 65b4a4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
8 changes: 8 additions & 0 deletions Gu.Xml.Tests/XmlTests.SerializeWithXmlAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SerializeWithXmlAttributes
new TestCaseData(new FieldWithXmlAttributeAttributeExplicitName { Value = 1 }),
new TestCaseData(new With<WithXmlEnumAttribute> { Value = WithXmlEnumAttribute.One }),
new TestCaseData(new With<WithXmlEnumAttribute> { Value = WithXmlEnumAttribute.Two }),
new TestCaseData(new WithXmlArrayAttribute { Ints = new[] { 1, 2, 3 } }),
};

[TestCaseSource(nameof(Values))]
Expand Down Expand Up @@ -95,6 +96,13 @@ public class FieldWithXmlElementAttributeExplicitName
public int Value = 1;
}

public class WithXmlArrayAttribute
{
[XmlArray(ElementName = "Numbers")]
[XmlArrayItem(ElementName = "Int32")]
public int[] Ints { get; set; }
}

public interface IValue
{
// ReSharper disable once UnusedMember.Global
Expand Down
3 changes: 1 addition & 2 deletions Gu.Xml/Writers/CollectionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ private static void WriteItems(XmlWriter writer, IEnumerable enumerable)
{
foreach (var item in enumerable)
{
var name = item == null ? "NULL" : RootName.Get(item.GetType());
writer.WriteElement(name, item);
writer.WriteElement(item == null ? "NULL" : RootName.Get(item.GetType()), item);
writer.WriteLine();
}
}
Expand Down
23 changes: 16 additions & 7 deletions Gu.Xml/Writers/ComplexValueWriter/ElementWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,11 @@ private static ElementWriter<TSource, TValue> CreateWriter<TSource, TValue>(stri

private static bool TryGetElementName(MemberInfo member, out string name)
{
name = null;
if (member.TryGetCustomAttribute(out System.Xml.Serialization.XmlElementAttribute xmlAttribute))
{
name = xmlAttribute.ElementName ?? string.Empty;
}
else if (member.TryGetCustomAttribute(out System.Xml.Serialization.SoapElementAttribute soapAttribute))
if (TryGetNameFromAttribute<System.Xml.Serialization.XmlElementAttribute>(member, x => x.ElementName, out name) ||
TryGetNameFromAttribute<System.Xml.Serialization.SoapElementAttribute>(member, x => x.ElementName, out name) ||
TryGetNameFromAttribute<System.Xml.Serialization.XmlArrayAttribute>(member, x => x.ElementName, out name))
{
name = soapAttribute.ElementName ?? string.Empty;
return true;
}

if (name == null)
Expand All @@ -140,5 +137,17 @@ private static bool TryGetElementName(MemberInfo member, out string name)

return true;
}

private static bool TryGetNameFromAttribute<TAttribute>(MemberInfo member, Func<TAttribute, string> getName, out string name)
where TAttribute : Attribute
{
name = null;
if (member.TryGetCustomAttribute(out TAttribute attribute))
{
name = getName(attribute);
}

return !string.IsNullOrEmpty(name);
}
}
}
24 changes: 12 additions & 12 deletions Gu.Xml/Writers/EnumWriter{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public void Write(TextWriter writer, T value)
writer.Write(this.cache.GetOrAdd(value, x => this.ToString(x)));
}

private static bool TryGetNameFromAttribute<TAttribute>(MemberInfo member, Func<TAttribute, string> getName, out string name)
where TAttribute : Attribute
{
name = null;
if (member.TryGetCustomAttribute(out TAttribute attribute))
{
name = getName(attribute);
}

return !string.IsNullOrEmpty(name);
}

private string ToString(T value)
{
if (typeof(T).GetMember(value.ToString()).TryFirst(out var member))
Expand All @@ -44,17 +56,5 @@ private string ToString(T value)

return Enum.Format(typeof(T), value, this.format).Replace(",", string.Empty);
}

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

return !string.IsNullOrEmpty(name);
}
}
}

0 comments on commit 65b4a4c

Please sign in to comment.