From e06e771d11425d73124a4aafdb374b3b603e6382 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BD=90=E5=A4=A9=E5=A4=A7=E8=83=96=E5=AD=90?=
<39656945+PANDATEEN@users.noreply.github.com>
Date: Sat, 20 Feb 2021 11:46:56 +0000
Subject: [PATCH 1/4] Update YamlMemberAttribute.cs
add a description attribute about property.
if used,when Serialize(),the result will have a yaml comment on that property.
---
YamlDotNet/Serialization/YamlMemberAttribute.cs | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/YamlDotNet/Serialization/YamlMemberAttribute.cs b/YamlDotNet/Serialization/YamlMemberAttribute.cs
index b11b498ae..598eb501d 100644
--- a/YamlDotNet/Serialization/YamlMemberAttribute.cs
+++ b/YamlDotNet/Serialization/YamlMemberAttribute.cs
@@ -30,6 +30,11 @@ namespace YamlDotNet.Serialization
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class YamlMemberAttribute : Attribute
{
+ ///
+ /// Decription/Comment about this property
+ ///
+ public string? Description { get; set; }
+
///
/// Specifies that this property should be serialized as the given type, rather than using the actual runtime value's type.
///
From 37eb8d4506ac4f1f3936a46e075155d373792263 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BD=90=E5=A4=A9=E5=A4=A7=E8=83=96=E5=AD=90?=
<39656945+PANDATEEN@users.noreply.github.com>
Date: Sat, 20 Feb 2021 11:57:13 +0000
Subject: [PATCH 2/4] Update DefaultValuesObjectGraphVisitor.cs
add a description attribute about property.
if used,when Serialize(),the result will have a yaml comment on that property.
---
.../DefaultValuesObjectGraphVisitor.cs | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs b/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
index 877b5ffb9..1518a9bb2 100644
--- a/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
+++ b/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
@@ -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);
}
}
-}
\ No newline at end of file
+}
From a94fe786fb932019cfc1c5478b48de3b47d3429c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BD=90=E5=A4=A9=E5=A4=A7=E8=83=96=E5=AD=90?=
<39656945+PANDATEEN@users.noreply.github.com>
Date: Tue, 23 Feb 2021 11:22:00 +0800
Subject: [PATCH 3/4] add test about yaml comments.
add a xunit test .cs file about yaml comments.
it`s just an easy comment above the property when Serialize(*).
---
.../Serialization/YamlCommentTests.cs | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 YamlDotNet.Test/Serialization/YamlCommentTests.cs
diff --git a/YamlDotNet.Test/Serialization/YamlCommentTests.cs b/YamlDotNet.Test/Serialization/YamlCommentTests.cs
new file mode 100644
index 000000000..f479d65c5
--- /dev/null
+++ b/YamlDotNet.Test/Serialization/YamlCommentTests.cs
@@ -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; }
+ }
+ }
+}
From ed66b8d55483ee271d8d38dda47de2f470b87ff2 Mon Sep 17 00:00:00 2001
From: Antoine Aubry
Date: Tue, 30 Mar 2021 13:27:20 +0100
Subject: [PATCH 4/4] Refactor the comments functionality
---
.../Serialization/YamlCommentTests.cs | 4 +-
.../CommentsObjectGraphVisitor.cs | 50 +++++++++++++++++++
.../DefaultValuesObjectGraphVisitor.cs | 8 ---
YamlDotNet/Serialization/SerializerBuilder.cs | 4 ++
4 files changed, 57 insertions(+), 9 deletions(-)
create mode 100644 YamlDotNet/Serialization/ObjectGraphVisitors/CommentsObjectGraphVisitor.cs
diff --git a/YamlDotNet.Test/Serialization/YamlCommentTests.cs b/YamlDotNet.Test/Serialization/YamlCommentTests.cs
index f479d65c5..f243d28ed 100644
--- a/YamlDotNet.Test/Serialization/YamlCommentTests.cs
+++ b/YamlDotNet.Test/Serialization/YamlCommentTests.cs
@@ -23,7 +23,9 @@ public void SerializationWithComment()
Serializer serializer = new Serializer();
string result = serializer.Serialize(person);
- Output.WriteLine(result);
+ Assert.Contains("# this is a yaml comment about name property", result);
+ Assert.Contains("# this is age", result);
+ Assert.Contains("# male or female", result);
}
class Person
diff --git a/YamlDotNet/Serialization/ObjectGraphVisitors/CommentsObjectGraphVisitor.cs b/YamlDotNet/Serialization/ObjectGraphVisitors/CommentsObjectGraphVisitor.cs
new file mode 100644
index 000000000..b1741ff41
--- /dev/null
+++ b/YamlDotNet/Serialization/ObjectGraphVisitors/CommentsObjectGraphVisitor.cs
@@ -0,0 +1,50 @@
+// This file is part of YamlDotNet - A .NET library for YAML.
+// Copyright (c) Antoine Aubry and contributors
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy of
+// this software and associated documentation files (the "Software"), to deal in
+// the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+// of the Software, and to permit persons to whom the Software is furnished to do
+// so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+using System;
+using YamlDotNet.Core;
+
+namespace YamlDotNet.Serialization.ObjectGraphVisitors
+{
+ public sealed class CommentsObjectGraphVisitor : ChainedObjectGraphVisitor
+ {
+ public CommentsObjectGraphVisitor(IObjectGraphVisitor nextVisitor)
+ : base(nextVisitor)
+ {
+ }
+
+ private static object? GetDefault(Type type)
+ {
+ return type.IsValueType() ? Activator.CreateInstance(type) : null;
+ }
+
+ public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
+ {
+ var yamlMember = key.GetCustomAttribute();
+ if (yamlMember?.Description != null)
+ {
+ context.Emit(new Core.Events.Comment(yamlMember.Description, false));
+ }
+
+ return base.EnterMapping(key, value, context);
+ }
+ }
+}
diff --git a/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs b/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
index 1518a9bb2..a6939877f 100644
--- a/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
+++ b/YamlDotNet/Serialization/ObjectGraphVisitors/DefaultValuesObjectGraphVisitor.cs
@@ -67,14 +67,6 @@ 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);
}
}
diff --git a/YamlDotNet/Serialization/SerializerBuilder.cs b/YamlDotNet/Serialization/SerializerBuilder.cs
index 3c8c87097..dd14cabe4 100755
--- a/YamlDotNet/Serialization/SerializerBuilder.cs
+++ b/YamlDotNet/Serialization/SerializerBuilder.cs
@@ -76,6 +76,10 @@ public SerializerBuilder()
{
typeof(DefaultValuesObjectGraphVisitor),
args => new DefaultValuesObjectGraphVisitor(defaultValuesHandlingConfiguration, args.InnerVisitor)
+ },
+ {
+ typeof(CommentsObjectGraphVisitor),
+ args => new CommentsObjectGraphVisitor(args.InnerVisitor)
}
};