From 0acfb552be1f04fac6ad754deac0d3acdf464282 Mon Sep 17 00:00:00 2001 From: lqdchrm Date: Tue, 24 Dec 2019 23:55:37 +0100 Subject: [PATCH] #311 impl for defining MappingStyle and SequenceStyle via Annotation --- YamlDotNet/Serialization/EventInfo.cs | 2 ++ YamlDotNet/Serialization/IObjectDescriptor.cs | 8 ++++++++ YamlDotNet/Serialization/IPropertyDescriptor.cs | 3 +++ YamlDotNet/Serialization/ObjectDescriptor.cs | 9 +++++++-- .../FullObjectGraphTraversalStrategy.cs | 4 ++-- YamlDotNet/Serialization/PropertyDescriptor.cs | 13 +++++++++++++ .../TypeInspectors/ReadableFieldsTypeInspector.cs | 7 ++++++- .../ReadablePropertiesTypeInspector.cs | 7 ++++++- .../YamlAttributeOverridesInspector.cs | 13 +++++++++++++ .../Serialization/YamlAttributesTypeInspector.cs | 2 ++ YamlDotNet/Serialization/YamlMember.cs | 13 +++++++++++++ 11 files changed, 75 insertions(+), 6 deletions(-) diff --git a/YamlDotNet/Serialization/EventInfo.cs b/YamlDotNet/Serialization/EventInfo.cs index b1632a40c..6a14670e6 100644 --- a/YamlDotNet/Serialization/EventInfo.cs +++ b/YamlDotNet/Serialization/EventInfo.cs @@ -77,6 +77,7 @@ public sealed class MappingStartEventInfo : ObjectEventInfo public MappingStartEventInfo(IObjectDescriptor source) : base(source) { + Style = source.MappingStyle; } public bool IsImplicit { get; set; } @@ -96,6 +97,7 @@ public sealed class SequenceStartEventInfo : ObjectEventInfo public SequenceStartEventInfo(IObjectDescriptor source) : base(source) { + Style = source.SequenceStyle; } public bool IsImplicit { get; set; } diff --git a/YamlDotNet/Serialization/IObjectDescriptor.cs b/YamlDotNet/Serialization/IObjectDescriptor.cs index 05c8abc08..3346a2fcc 100644 --- a/YamlDotNet/Serialization/IObjectDescriptor.cs +++ b/YamlDotNet/Serialization/IObjectDescriptor.cs @@ -21,6 +21,7 @@ using System; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization { @@ -48,6 +49,13 @@ public interface IObjectDescriptor /// The style to be used for scalars. /// ScalarStyle ScalarStyle { get; } + + /// + /// The style to be used for sequences. + /// + SequenceStyle SequenceStyle { get; } + + MappingStyle MappingStyle { get; } } public static class ObjectDescriptorExtensions diff --git a/YamlDotNet/Serialization/IPropertyDescriptor.cs b/YamlDotNet/Serialization/IPropertyDescriptor.cs index 1d252b3c3..d677f5be1 100644 --- a/YamlDotNet/Serialization/IPropertyDescriptor.cs +++ b/YamlDotNet/Serialization/IPropertyDescriptor.cs @@ -21,6 +21,7 @@ using System; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization { @@ -32,6 +33,8 @@ public interface IPropertyDescriptor Type? TypeOverride { get; set; } int Order { get; set; } ScalarStyle ScalarStyle { get; set; } + SequenceStyle SequenceStyle { get; set; } + MappingStyle MappingStyle { get; set; } T GetCustomAttribute() where T : Attribute; diff --git a/YamlDotNet/Serialization/ObjectDescriptor.cs b/YamlDotNet/Serialization/ObjectDescriptor.cs index 7bf529a59..3d42df950 100644 --- a/YamlDotNet/Serialization/ObjectDescriptor.cs +++ b/YamlDotNet/Serialization/ObjectDescriptor.cs @@ -21,6 +21,7 @@ using System; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization { @@ -30,19 +31,23 @@ public sealed class ObjectDescriptor : IObjectDescriptor public Type Type { get; private set; } public Type StaticType { get; private set; } public ScalarStyle ScalarStyle { get; private set; } + public SequenceStyle SequenceStyle { get; private set; } + public MappingStyle MappingStyle { get; private set; } public ObjectDescriptor(object? value, Type type, Type staticType) - : this(value, type, staticType, ScalarStyle.Any) + : this(value, type, staticType, ScalarStyle.Any, SequenceStyle.Any, MappingStyle.Any) { } - public ObjectDescriptor(object? value, Type type, Type staticType, ScalarStyle scalarStyle) + public ObjectDescriptor(object? value, Type type, Type staticType, ScalarStyle scalarStyle, SequenceStyle sequenceStyle, MappingStyle mappingStyle) { Value = value; Type = type ?? throw new ArgumentNullException(nameof(type)); StaticType = staticType ?? throw new ArgumentNullException(nameof(staticType)); ScalarStyle = scalarStyle; + SequenceStyle = sequenceStyle; + MappingStyle = mappingStyle; } } } \ No newline at end of file diff --git a/YamlDotNet/Serialization/ObjectGraphTraversalStrategies/FullObjectGraphTraversalStrategy.cs b/YamlDotNet/Serialization/ObjectGraphTraversalStrategies/FullObjectGraphTraversalStrategy.cs index f9b11dad8..50bc83c9c 100644 --- a/YamlDotNet/Serialization/ObjectGraphTraversalStrategies/FullObjectGraphTraversalStrategy.cs +++ b/YamlDotNet/Serialization/ObjectGraphTraversalStrategies/FullObjectGraphTraversalStrategy.cs @@ -152,7 +152,7 @@ protected virtual void Traverse(object name, IObjectDescriptor value, { // This is a nullable type, recursively handle it with its underlying type. // Note that if it contains null, the condition above already took care of it - Traverse("Value", new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle), visitor, context, path); + Traverse("Value", new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle, value.SequenceStyle, value.MappingStyle), visitor, context, path); } else { @@ -180,7 +180,7 @@ protected virtual void TraverseObject(IObjectDescriptor value, IObject { var genericArguments = genericDictionaryType.GetGenericArguments(); var adaptedDictionary = Activator.CreateInstance(typeof(GenericDictionaryToNonGenericAdapter<,>).MakeGenericType(genericArguments), value.Value)!; - TraverseDictionary(new ObjectDescriptor(adaptedDictionary, value.Type, value.StaticType, value.ScalarStyle), visitor, genericArguments[0], genericArguments[1], context, path); + TraverseDictionary(new ObjectDescriptor(adaptedDictionary, value.Type, value.StaticType, value.ScalarStyle, value.SequenceStyle, value.MappingStyle), visitor, genericArguments[0], genericArguments[1], context, path); return; } diff --git a/YamlDotNet/Serialization/PropertyDescriptor.cs b/YamlDotNet/Serialization/PropertyDescriptor.cs index a2a123035..74eb35fe4 100644 --- a/YamlDotNet/Serialization/PropertyDescriptor.cs +++ b/YamlDotNet/Serialization/PropertyDescriptor.cs @@ -21,6 +21,7 @@ using System; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization { @@ -52,6 +53,18 @@ public ScalarStyle ScalarStyle set { baseDescriptor.ScalarStyle = value; } } + public SequenceStyle SequenceStyle + { + get { return baseDescriptor.SequenceStyle; } + set { baseDescriptor.SequenceStyle = value; } + } + + public MappingStyle MappingStyle + { + get { return baseDescriptor.MappingStyle; } + set { baseDescriptor.MappingStyle = value; } + } + public bool CanWrite { get { return baseDescriptor.CanWrite; } diff --git a/YamlDotNet/Serialization/TypeInspectors/ReadableFieldsTypeInspector.cs b/YamlDotNet/Serialization/TypeInspectors/ReadableFieldsTypeInspector.cs index 8472aeaa3..70f4c94ea 100644 --- a/YamlDotNet/Serialization/TypeInspectors/ReadableFieldsTypeInspector.cs +++ b/YamlDotNet/Serialization/TypeInspectors/ReadableFieldsTypeInspector.cs @@ -24,6 +24,7 @@ using System.Linq; using System.Reflection; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization.TypeInspectors { @@ -56,6 +57,8 @@ public ReflectionFieldDescriptor(FieldInfo fieldInfo, ITypeResolver typeResolver this.fieldInfo = fieldInfo; this.typeResolver = typeResolver; ScalarStyle = ScalarStyle.Any; + SequenceStyle = SequenceStyle.Any; + MappingStyle = MappingStyle.Any; } public string Name { get { return fieldInfo.Name; } } @@ -64,6 +67,8 @@ public ReflectionFieldDescriptor(FieldInfo fieldInfo, ITypeResolver typeResolver public int Order { get; set; } public bool CanWrite { get { return !fieldInfo.IsInitOnly; } } public ScalarStyle ScalarStyle { get; set; } + public SequenceStyle SequenceStyle { get; set; } + public MappingStyle MappingStyle { get; set; } public void Write(object target, object? value) { @@ -80,7 +85,7 @@ public IObjectDescriptor Read(object target) { var propertyValue = fieldInfo.GetValue(target); var actualType = TypeOverride ?? typeResolver.Resolve(Type, propertyValue); - return new ObjectDescriptor(propertyValue, actualType, Type, ScalarStyle); + return new ObjectDescriptor(propertyValue, actualType, Type, ScalarStyle, SequenceStyle, MappingStyle); } } } diff --git a/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs b/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs index 46932ab3c..606de90f1 100644 --- a/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs +++ b/YamlDotNet/Serialization/TypeInspectors/ReadablePropertiesTypeInspector.cs @@ -24,6 +24,7 @@ using System.Linq; using System.Reflection; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization.TypeInspectors { @@ -63,6 +64,8 @@ public ReflectionPropertyDescriptor(PropertyInfo propertyInfo, ITypeResolver typ this.propertyInfo = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo)); this.typeResolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver)); ScalarStyle = ScalarStyle.Any; + SequenceStyle = SequenceStyle.Any; + MappingStyle = MappingStyle.Any; } public string Name => propertyInfo.Name; @@ -71,6 +74,8 @@ public ReflectionPropertyDescriptor(PropertyInfo propertyInfo, ITypeResolver typ public int Order { get; set; } public bool CanWrite => propertyInfo.CanWrite; public ScalarStyle ScalarStyle { get; set; } + public SequenceStyle SequenceStyle { get; set; } + public MappingStyle MappingStyle { get; set; } public void Write(object target, object? value) { @@ -87,7 +92,7 @@ public IObjectDescriptor Read(object target) { var propertyValue = propertyInfo.ReadValue(target); var actualType = TypeOverride ?? typeResolver.Resolve(Type, propertyValue); - return new ObjectDescriptor(propertyValue, actualType, Type, ScalarStyle); + return new ObjectDescriptor(propertyValue, actualType, Type, ScalarStyle, SequenceStyle, MappingStyle); } } } diff --git a/YamlDotNet/Serialization/YamlAttributeOverridesInspector.cs b/YamlDotNet/Serialization/YamlAttributeOverridesInspector.cs index c6f79355f..fa997342f 100644 --- a/YamlDotNet/Serialization/YamlAttributeOverridesInspector.cs +++ b/YamlDotNet/Serialization/YamlAttributeOverridesInspector.cs @@ -23,6 +23,7 @@ using System.Collections.Generic; using System.Linq; using YamlDotNet.Core; +using YamlDotNet.Core.Events; using YamlDotNet.Serialization.TypeInspectors; namespace YamlDotNet.Serialization @@ -90,6 +91,18 @@ public ScalarStyle ScalarStyle set { baseDescriptor.ScalarStyle = value; } } + public SequenceStyle SequenceStyle + { + get { return baseDescriptor.SequenceStyle; } + set { baseDescriptor.SequenceStyle = value; } + } + + public MappingStyle MappingStyle + { + get { return baseDescriptor.MappingStyle; } + set { baseDescriptor.MappingStyle = value; } + } + public void Write(object target, object? value) { baseDescriptor.Write(target, value); diff --git a/YamlDotNet/Serialization/YamlAttributesTypeInspector.cs b/YamlDotNet/Serialization/YamlAttributesTypeInspector.cs index 4d4f02796..dc462aa2e 100644 --- a/YamlDotNet/Serialization/YamlAttributesTypeInspector.cs +++ b/YamlDotNet/Serialization/YamlAttributesTypeInspector.cs @@ -55,6 +55,8 @@ public override IEnumerable GetProperties(Type type, object descriptor.Order = member.Order; descriptor.ScalarStyle = member.ScalarStyle; + descriptor.SequenceStyle = member.SequenceStyle; + descriptor.MappingStyle = member.MappingStyle; if (member.Alias != null) { diff --git a/YamlDotNet/Serialization/YamlMember.cs b/YamlDotNet/Serialization/YamlMember.cs index ed6038552..a5ebec470 100644 --- a/YamlDotNet/Serialization/YamlMember.cs +++ b/YamlDotNet/Serialization/YamlMember.cs @@ -21,6 +21,7 @@ using System; using YamlDotNet.Core; +using YamlDotNet.Core.Events; namespace YamlDotNet.Serialization { @@ -55,6 +56,16 @@ public sealed class YamlMemberAttribute : Attribute /// public ScalarStyle ScalarStyle { get; set; } + /// + /// Specifies the sequence style of the property when serialied. This will only affect the serialization of sequence properties. + /// + public SequenceStyle SequenceStyle { get; set; } + + /// + /// Specifies the mapping style of the property when serialied. This will only affect the serialization of object properties. + /// + public MappingStyle MappingStyle { get; set; } + /// /// Overrides how null and default values should be handled for this property. /// @@ -66,6 +77,8 @@ public sealed class YamlMemberAttribute : Attribute public YamlMemberAttribute() { ScalarStyle = ScalarStyle.Any; + SequenceStyle = SequenceStyle.Any; + MappingStyle = MappingStyle.Any; ApplyNamingConventions = true; }