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

Process inline comments with DictionaryDeserializer-2 #868

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
84 changes: 84 additions & 0 deletions YamlDotNet.Samples/DeserializeWithComment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 System.IO;
using Xunit.Abstractions;
using YamlDotNet.Core.ParsingComments;
using YamlDotNet.Samples.Helpers;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;


namespace YamlDotNet.Samples
{
public class DeserializeWithComment
{
private readonly ITestOutputHelper output;

public DeserializeWithComment(ITestOutputHelper output)
{
this.output = output;
}

[Sample(
DisplayName = "Deserializing with comments",
Description = "Shows how to process comments"
)]
public void Main()
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.BuildWithCommentsDeserializer();
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.JsonCompatible()
.Build();

var content = WithInlineCommentsAndList;

var parser = new ParserWithComments(new ScannerWithComments(new StringReader(content)));
var yamlObject = deserializer.Deserialize<object>(parser);

var json = serializer.Serialize(yamlObject);

output.WriteLine(json);
Console.WriteLine(json);

}

private const string WithInlineCommentsAndList =
@"valuelist:
- string1 #{1st comment}
- string2
# block comment
- string3 #{2nd comment}
simplevalue: 12
objectlist:
- att1: 12
att2: v1
- att1: 13
att2: v2
- att1: 14 #3rd comment
att2: v3 #4th comment
";
}
}
37 changes: 37 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Core.ParsingComments;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.Callbacks;
using YamlDotNet.Serialization.NamingConventions;
Expand Down Expand Up @@ -1647,6 +1648,42 @@ public void YamlConvertiblesAreAbleToEmitAndParseComments()
Assert.Equal("The value", parsed.Value);
}

[Fact]
public void ChildrenWithInlineComments()
{
var input = @"# document starts with comment
valuelist:
- string1 #{1st comment}
- string2
# block comment
- string3 #{2nd comment}
simplevalue: 12
objectlist:
- att1: 12
att2: v1
- att1: 13
att2: v2
- att1: 14 #3rd comment
att2: v3 #4th comment
";

var expected = @"{""valuelist"": [{""value"": ""string1"", ""comment"": ""{1st comment}""}, {""value"": ""string2"", ""comment"": """"}, {""value"": ""string3"", ""comment"": ""{2nd comment}""}], ""simplevalue"": {""value"": ""12"", ""comment"": """"}, ""objectlist"": [{""att1"": {""value"": ""12"", ""comment"": """"}, ""att2"": {""value"": ""v1"", ""comment"": """"}}, {""att1"": {""value"": ""13"", ""comment"": """"}, ""att2"": {""value"": ""v2"", ""comment"": """"}}, {""att1"": {""value"": ""14"", ""comment"": ""3rd comment""}, ""att2"": {""value"": ""v3"", ""comment"": ""4th comment""}}]}
";

var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.BuildWithCommentsDeserializer();
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.JsonCompatible()
.Build();
var parser = new ParserWithComments(new ScannerWithComments(new StringReader(input)));

var yamlObject = deserializer.Deserialize<object>(parser);
var actual = serializer.Serialize(yamlObject);

Assert.Equal(expected, actual);
}
public class CommentWrapper<T> : IYamlConvertible
{
public string Comment { get; set; }
Expand Down
1 change: 1 addition & 0 deletions YamlDotNet/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class Parser : IParser
{
pendingEvents.Enqueue(new Events.Comment(commentToken.Value, commentToken.IsInline, commentToken.Start, commentToken.End));
scanner.ConsumeCurrent();
currentToken = scanner.Current;
}
else
{
Expand Down
117 changes: 117 additions & 0 deletions YamlDotNet/Core/ParsingComments/DictionaryDeserializerWithComments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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 System.Collections;
using System.Collections.Generic;
using System.Linq;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NodeDeserializers;

namespace YamlDotNet.Core.ParsingComments
{
public abstract class DictionaryDeserializerWithComments : DictionaryDeserializer
{
public DictionaryDeserializerWithComments(bool duplicateKeyChecking) : base(duplicateKeyChecking)
{
}

protected override void Deserialize(Type tKey, Type tValue, IParser parser, Func<IParser, Type, object?> nestedObjectDeserializer, IDictionary result)
{
parser.TryConsume<Comment>(out var _);
var property = parser.Consume<MappingStart>();
while (!parser.TryConsume<MappingEnd>(out var _))
{
((IParserWithComments)parser).SkipFollowingComments();
var key = nestedObjectDeserializer(parser, tKey);
((IParserWithComments)parser).SkipFollowingComments();
var originalValue = nestedObjectDeserializer(parser, tValue);
var valueWithComment = ParseStringWithComment((IParserWithComments)parser, originalValue);
var listValueWithComment = ParseListWithComment(valueWithComment);
var valuePromise = listValueWithComment as IValuePromise;
AddKeyValue(result, property, key, listValueWithComment, valuePromise);
}
}

private static object? ParseListWithComment(object? value)
{
if (value is List<object> list)
{
var newValue = new List<ValueWithComment>();
var stringValue = string.Empty;
var comment = string.Empty;

foreach (var listItem in list)
{
if (listItem is string)
{
if (!string.IsNullOrEmpty(stringValue))
{
newValue.Add(new ValueWithComment(stringValue, comment));
}
stringValue = (string)listItem;
comment = string.Empty;
}
if (listItem is Comment && ((Comment)listItem).IsInline)
{
comment = ((Comment)listItem).Value;
}
}
if (!string.IsNullOrEmpty(stringValue))
{
newValue.Add(new ValueWithComment(stringValue, comment));
}
if (newValue.Any())
{
if (newValue.Any(v => !string.IsNullOrEmpty(v.Comment)))
{
value = newValue;
}
else
{
value = newValue.Select(v => v.Value).ToList();
}
}
}

return value;
}

private static object? ParseStringWithComment(IParserWithComments parser, object? value)
{
if (value is string)
{
var comment = string.Empty;
if (parser.TryConsume<Comment>(out var valueComment))
{
if (value is string && valueComment.IsInline)
{
comment = valueComment.Value;
}
parser.SkipFollowingComments();
}
value = new ValueWithComment(value, comment);
}
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 System.Collections;
using System.Collections.Generic;
using YamlDotNet.Helpers;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.Utilities;

namespace YamlDotNet.Core.ParsingComments
{
public class DictionaryNodeDeserializerWithComments : DictionaryDeserializerWithComments, INodeDeserializer
{
private readonly IObjectFactory objectFactory;

public DictionaryNodeDeserializerWithComments(IObjectFactory objectFactory, bool duplicateKeyChecking) :
base(duplicateKeyChecking)
{
this.objectFactory = objectFactory ?? throw new ArgumentNullException(nameof(objectFactory));
}

public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object?> nestedObjectDeserializer, out object? value)
{
IDictionary? dictionary;
Type keyType, valueType;
var genericDictionaryType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IDictionary<,>));
if (genericDictionaryType != null)
{
var genericArguments = genericDictionaryType.GetGenericArguments();
keyType = genericArguments[0];
valueType = genericArguments[1];

value = objectFactory.Create(expectedType);

dictionary = value as IDictionary;
if (dictionary == null)
{
// Uncommon case where a type implements IDictionary<TKey, TValue> but not IDictionary
dictionary = (IDictionary?)Activator.CreateInstance(typeof(GenericDictionaryToNonGenericAdapter<,>).MakeGenericType(keyType, valueType), value);
}
}
else if (typeof(IDictionary).IsAssignableFrom(expectedType))
{
keyType = typeof(object);
valueType = typeof(object);

value = objectFactory.Create(expectedType);
dictionary = (IDictionary)value;
}
else
{
value = null;
return false;
}

Deserialize(keyType, valueType, parser, nestedObjectDeserializer, dictionary!);

return true;
}
}
}
37 changes: 37 additions & 0 deletions YamlDotNet/Core/ParsingComments/IParserWithComments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.

namespace YamlDotNet.Core.ParsingComments
{
public interface IParserWithComments : IParser
{

/// <summary>
/// Skips following comment events.
/// </summary>
void SkipFollowingComments();

/// <summary>
/// Gets the SkipComments value from its scanner.
/// </summary>
bool SkipComments { get; }
}
}
Loading