Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#311 impl for defining MappingStyle and SequenceStyle via Annotation #462

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions YamlDotNet/Serialization/EventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public sealed class MappingStartEventInfo : ObjectEventInfo
public MappingStartEventInfo(IObjectDescriptor source)
: base(source)
{
Style = source.MappingStyle;
}

public bool IsImplicit { get; set; }
Expand All @@ -96,6 +97,7 @@ public sealed class SequenceStartEventInfo : ObjectEventInfo
public SequenceStartEventInfo(IObjectDescriptor source)
: base(source)
{
Style = source.SequenceStyle;
}

public bool IsImplicit { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions YamlDotNet/Serialization/IObjectDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization
{
Expand Down Expand Up @@ -48,6 +49,13 @@ public interface IObjectDescriptor
/// The style to be used for scalars.
/// </summary>
ScalarStyle ScalarStyle { get; }

/// <summary>
/// The style to be used for sequences.
/// </summary>
SequenceStyle SequenceStyle { get; }

MappingStyle MappingStyle { get; }
}

public static class ObjectDescriptorExtensions
Expand Down
3 changes: 3 additions & 0 deletions YamlDotNet/Serialization/IPropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization
{
Expand All @@ -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<T>() where T : Attribute;

Expand Down
9 changes: 7 additions & 2 deletions YamlDotNet/Serialization/ObjectDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization
{
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected virtual void Traverse<TContext>(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
{
Expand Down Expand Up @@ -180,7 +180,7 @@ protected virtual void TraverseObject<TContext>(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;
}

Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet/Serialization/PropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization
{
Expand Down Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using System.Reflection;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization.TypeInspectors
{
Expand Down Expand Up @@ -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; } }
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using System.Reflection;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization.TypeInspectors
{
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet/Serialization/YamlAttributeOverridesInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions YamlDotNet/Serialization/YamlAttributesTypeInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object

descriptor.Order = member.Order;
descriptor.ScalarStyle = member.ScalarStyle;
descriptor.SequenceStyle = member.SequenceStyle;
descriptor.MappingStyle = member.MappingStyle;

if (member.Alias != null)
{
Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet/Serialization/YamlMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

namespace YamlDotNet.Serialization
{
Expand Down Expand Up @@ -55,6 +56,16 @@ public sealed class YamlMemberAttribute : Attribute
/// </summary>
public ScalarStyle ScalarStyle { get; set; }

/// <summary>
/// Specifies the sequence style of the property when serialied. This will only affect the serialization of sequence properties.
/// </summary>
public SequenceStyle SequenceStyle { get; set; }

/// <summary>
/// Specifies the mapping style of the property when serialied. This will only affect the serialization of object properties.
/// </summary>
public MappingStyle MappingStyle { get; set; }

/// <summary>
/// Overrides how null and default values should be handled for this property.
/// </summary>
Expand All @@ -66,6 +77,8 @@ public sealed class YamlMemberAttribute : Attribute
public YamlMemberAttribute()
{
ScalarStyle = ScalarStyle.Any;
SequenceStyle = SequenceStyle.Any;
MappingStyle = MappingStyle.Any;
ApplyNamingConventions = true;
}

Expand Down