Skip to content

Commit

Permalink
Make YamlMemberAttribute.DefaultValuesHandling actually usable
Browse files Browse the repository at this point in the history
Fixes #466
  • Loading branch information
aaubry committed Feb 17, 2020
1 parent c452b9e commit 5db187e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public DefaultValuesObjectGraphVisitor(DefaultValuesHandling handling, IObjectGr

public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
{
var configuration = key.GetCustomAttribute<YamlMemberAttribute>()?.DefaultValuesHandling ?? this.handling;
var configuration = this.handling;
var yamlMember = key.GetCustomAttribute<YamlMemberAttribute>();
if (yamlMember != null && yamlMember.IsDefaultValuesHandlingSpecified)
{
configuration = yamlMember.DefaultValuesHandling;
}

switch (configuration)
{
case DefaultValuesHandling.OmitNull:
Expand Down
10 changes: 9 additions & 1 deletion YamlDotNet/Serialization/YamlMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ public sealed class YamlMemberAttribute : Attribute
/// </summary>
public ScalarStyle ScalarStyle { get; set; }

private DefaultValuesHandling? defaultValuesHandling;

/// <summary>
/// Overrides how null and default values should be handled for this property.
/// </summary>
public DefaultValuesHandling? DefaultValuesHandling { get; set; }
public DefaultValuesHandling DefaultValuesHandling
{
get => defaultValuesHandling.GetValueOrDefault();
set => defaultValuesHandling = value;
}

public bool IsDefaultValuesHandlingSpecified => defaultValuesHandling.HasValue;

/// <summary>
/// Initializes a new instance of the <see cref="YamlMemberAttribute" /> class.
Expand Down

0 comments on commit 5db187e

Please sign in to comment.