Skip to content

Commit

Permalink
Merge pull request #1017 from ogretmenb/fix_indentation
Browse files Browse the repository at this point in the history
fix: serialization using IndentedTextWriter causes missing indentation

+semver:fix
  • Loading branch information
EdwardCooke authored Dec 23, 2024
2 parents 63721ee + 51f46f5 commit d6a3c1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
30 changes: 30 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// SOFTWARE.

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -329,6 +330,35 @@ public void SerializeWithCRNewLine()
result.Should().Be(expectedResult);
}


/// <summary>
/// Tests the serialization of a dictionary containing a list of strings using IndentedTextWriter.
/// </summary>
[Fact]
public void SerializeWithTabs()
{
var tabString = " ";
using var writer = new StringWriter();
using var indentedTextWriter = new IndentedTextWriter(writer, tabString) { Indent = 2 };

//create a dictionary with a list of strings to test the tabbed serialization
var items = new List<string> { "item 1", "item 2" };
var list = new Dictionary<string, List<string>> { { "key", items } };

SerializerBuilder
.Build()
.Serialize(indentedTextWriter, list);

//split serialized output into lines
var lines = indentedTextWriter.InnerWriter.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

//expected indentation
var indent = string.Join(string.Empty, Enumerable.Repeat(tabString, indentedTextWriter.Indent).ToList());

//check that the serialized lines (excluding the first and last) start with the expected indentation
lines.Skip(1).Take(lines.Length - 2).Where(element => element.StartsWith(indent)).Should().HaveCount(items.Count);
}

[Fact]
public void DeserializeExplicitType()
{
Expand Down
5 changes: 2 additions & 3 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class Emitter : IEmitter
private bool isIndentation;
private readonly bool forceIndentLess;
private readonly bool useUtf16SurrogatePair;
private readonly string newLine;

private bool isDocumentEndWritten;

Expand Down Expand Up @@ -150,9 +149,9 @@ public Emitter(TextWriter output, EmitterSettings settings)
this.skipAnchorName = settings.SkipAnchorName;
this.forceIndentLess = !settings.IndentSequences;
this.useUtf16SurrogatePair = settings.UseUtf16SurrogatePairs;
this.newLine = settings.NewLine;

this.output = output;
this.output.NewLine = settings.NewLine;
this.outputUsesUnicodeEncoding = IsUnicode(output.Encoding);
}

Expand Down Expand Up @@ -1937,7 +1936,7 @@ private void WriteBreak(char breakCharacter = '\n')
{
if (breakCharacter == '\n')
{
output.Write(newLine);
output.WriteLine();
}
else
{
Expand Down

0 comments on commit d6a3c1f

Please sign in to comment.