Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/PANDATEEN/YamlDotNet into…
Browse files Browse the repository at this point in the history
… PANDATEEN-master
  • Loading branch information
aaubry committed Mar 30, 2021
2 parents b5b84c9 + a94fe78 commit 0a8b53e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
39 changes: 39 additions & 0 deletions YamlDotNet.Test/Serialization/YamlCommentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Xunit;
using Xunit.Abstractions;
using YamlDotNet.Serialization;

namespace YamlDotNet.Test.Serialization
{
public class YamlCommentTests
{
protected readonly ITestOutputHelper Output;
public YamlCommentTests(ITestOutputHelper helper)
{
Output = helper;
}

[Fact]
public void SerializationWithComment()
{
var person = new Person();
person.Name = "PandaTea";
person.Age = 100;
person.Sex = "male";

Serializer serializer = new Serializer();
string result = serializer.Serialize(person);

Output.WriteLine(result);
}

class Person
{
[YamlMember(Description = "this is a yaml comment about name property")]
public string Name { get; set; }
[YamlMember(Description = "this is age")]
public int Age { get; set; }
[YamlMember(Description = "male or female")]
public string Sex { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor val
break;
}

if (yamlMember != null)
{
if (yamlMember.Description != null)
{
context.Emit(new Core.Events.Comment(yamlMember.Description, false));
}
}

return base.EnterMapping(key, value, context);
}
}
}
}
5 changes: 5 additions & 0 deletions YamlDotNet/Serialization/YamlMemberAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ namespace YamlDotNet.Serialization
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class YamlMemberAttribute : Attribute
{
/// <summary>
/// Decription/Comment about this property
/// </summary>
public string? Description { get; set; }

/// <summary>
/// Specifies that this property should be serialized as the given type, rather than using the actual runtime value's type.
/// </summary>
Expand Down

0 comments on commit 0a8b53e

Please sign in to comment.