Skip to content

Commit d6a3c1f

Browse files
authored
Merge pull request #1017 from ogretmenb/fix_indentation
fix: serialization using IndentedTextWriter causes missing indentation +semver:fix
2 parents 63721ee + 51f46f5 commit d6a3c1f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

YamlDotNet.Test/Serialization/SerializationTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// SOFTWARE.
2121

2222
using System;
23+
using System.CodeDom.Compiler;
2324
using System.Collections;
2425
using System.Collections.Concurrent;
2526
using System.Collections.Generic;
@@ -329,6 +330,35 @@ public void SerializeWithCRNewLine()
329330
result.Should().Be(expectedResult);
330331
}
331332

333+
334+
/// <summary>
335+
/// Tests the serialization of a dictionary containing a list of strings using IndentedTextWriter.
336+
/// </summary>
337+
[Fact]
338+
public void SerializeWithTabs()
339+
{
340+
var tabString = " ";
341+
using var writer = new StringWriter();
342+
using var indentedTextWriter = new IndentedTextWriter(writer, tabString) { Indent = 2 };
343+
344+
//create a dictionary with a list of strings to test the tabbed serialization
345+
var items = new List<string> { "item 1", "item 2" };
346+
var list = new Dictionary<string, List<string>> { { "key", items } };
347+
348+
SerializerBuilder
349+
.Build()
350+
.Serialize(indentedTextWriter, list);
351+
352+
//split serialized output into lines
353+
var lines = indentedTextWriter.InnerWriter.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
354+
355+
//expected indentation
356+
var indent = string.Join(string.Empty, Enumerable.Repeat(tabString, indentedTextWriter.Indent).ToList());
357+
358+
//check that the serialized lines (excluding the first and last) start with the expected indentation
359+
lines.Skip(1).Take(lines.Length - 2).Where(element => element.StartsWith(indent)).Should().HaveCount(items.Count);
360+
}
361+
332362
[Fact]
333363
public void DeserializeExplicitType()
334364
{

YamlDotNet/Core/Emitter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public class Emitter : IEmitter
6767
private bool isIndentation;
6868
private readonly bool forceIndentLess;
6969
private readonly bool useUtf16SurrogatePair;
70-
private readonly string newLine;
7170

7271
private bool isDocumentEndWritten;
7372

@@ -150,9 +149,9 @@ public Emitter(TextWriter output, EmitterSettings settings)
150149
this.skipAnchorName = settings.SkipAnchorName;
151150
this.forceIndentLess = !settings.IndentSequences;
152151
this.useUtf16SurrogatePair = settings.UseUtf16SurrogatePairs;
153-
this.newLine = settings.NewLine;
154152

155153
this.output = output;
154+
this.output.NewLine = settings.NewLine;
156155
this.outputUsesUnicodeEncoding = IsUnicode(output.Encoding);
157156
}
158157

@@ -1937,7 +1936,7 @@ private void WriteBreak(char breakCharacter = '\n')
19371936
{
19381937
if (breakCharacter == '\n')
19391938
{
1940-
output.Write(newLine);
1939+
output.WriteLine();
19411940
}
19421941
else
19431942
{

0 commit comments

Comments
 (0)