From 37d1534579dabd7cdb3f0399cf28383b7cc2b626 Mon Sep 17 00:00:00 2001
From: "Aista, Ltd"
Date: Tue, 29 Nov 2022 07:49:44 +0200
Subject: [PATCH 1/2] Fix for #746
---
.../Serialization/SerializationTests.cs | 54 +++++++++++++++++++
YamlDotNet.Test/files/list.yaml | 2 +-
YamlDotNet/Core/Emitter.cs | 4 +-
YamlDotNet/Core/EmitterSettings.cs | 21 +++++++-
YamlDotNet/Serialization/SerializerBuilder.cs | 10 ++++
5 files changed, 88 insertions(+), 3 deletions(-)
diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs
index f6855d0dd..8679844b1 100644
--- a/YamlDotNet.Test/Serialization/SerializationTests.cs
+++ b/YamlDotNet.Test/Serialization/SerializationTests.cs
@@ -272,6 +272,60 @@ public void SerializeCustomTags()
result.Should().Be(expectedResult);
}
+ [Fact]
+ public void SerializeWithCRLFNewLine()
+ {
+ var expectedResult = Yaml
+ .ReaderFrom("list.yaml")
+ .ReadToEnd()
+ .NormalizeNewLines()
+ .Replace(Environment.NewLine, "\r\n");
+
+ var list = new string[] {"one", "two", "three"};
+ var result = SerializerBuilder
+ .WithNewLine("\r\n")
+ .Build()
+ .Serialize(list);
+
+ result.Should().Be(expectedResult);
+ }
+
+ [Fact]
+ public void SerializeWithLFNewLine()
+ {
+ var expectedResult = Yaml
+ .ReaderFrom("list.yaml")
+ .ReadToEnd()
+ .NormalizeNewLines()
+ .Replace(Environment.NewLine, "\n");
+
+ var list = new string[] {"one", "two", "three"};
+ var result = SerializerBuilder
+ .WithNewLine("\n")
+ .Build()
+ .Serialize(list);
+
+ result.Should().Be(expectedResult);
+ }
+
+ [Fact]
+ public void SerializeWithCRNewLine()
+ {
+ var expectedResult = Yaml
+ .ReaderFrom("list.yaml")
+ .ReadToEnd()
+ .NormalizeNewLines()
+ .Replace(Environment.NewLine, "\r");
+
+ var list = new string[] {"one", "two", "three"};
+ var result = SerializerBuilder
+ .WithNewLine("\r")
+ .Build()
+ .Serialize(list);
+
+ result.Should().Be(expectedResult);
+ }
+
[Fact]
public void DeserializeExplicitType()
{
diff --git a/YamlDotNet.Test/files/list.yaml b/YamlDotNet.Test/files/list.yaml
index 6ef9f6041..291246557 100644
--- a/YamlDotNet.Test/files/list.yaml
+++ b/YamlDotNet.Test/files/list.yaml
@@ -1,3 +1,3 @@
- one
- two
-- three
\ No newline at end of file
+- three
diff --git a/YamlDotNet/Core/Emitter.cs b/YamlDotNet/Core/Emitter.cs
index 9871944ca..44fb59b46 100644
--- a/YamlDotNet/Core/Emitter.cs
+++ b/YamlDotNet/Core/Emitter.cs
@@ -66,6 +66,7 @@ public class Emitter : IEmitter
private bool isWhitespace;
private bool isIndentation;
private readonly bool forceIndentLess;
+ private readonly string newLine;
private bool isDocumentEndWritten;
@@ -147,6 +148,7 @@ public Emitter(TextWriter output, EmitterSettings settings)
this.maxSimpleKeyLength = settings.MaxSimpleKeyLength;
this.skipAnchorName = settings.SkipAnchorName;
this.forceIndentLess = !settings.IndentSequences;
+ this.newLine = settings.NewLine;
this.output = output;
this.outputUsesUnicodeEncoding = IsUnicode(output.Encoding);
@@ -1917,7 +1919,7 @@ private void WriteBreak(char breakCharacter = '\n')
{
if (breakCharacter == '\n')
{
- output.WriteLine();
+ output.Write(newLine);
}
else
{
diff --git a/YamlDotNet/Core/EmitterSettings.cs b/YamlDotNet/Core/EmitterSettings.cs
index ee6c85302..69ffeaccd 100644
--- a/YamlDotNet/Core/EmitterSettings.cs
+++ b/YamlDotNet/Core/EmitterSettings.cs
@@ -35,6 +35,11 @@ public sealed class EmitterSettings
///
public int BestWidth { get; } = int.MaxValue;
+ ///
+ /// New line characters.
+ ///
+ public string NewLine { get; } = Environment.NewLine;
+
///
/// If true, write the output in canonical form.
///
@@ -64,7 +69,7 @@ public EmitterSettings()
{
}
- public EmitterSettings(int bestIndent, int bestWidth, bool isCanonical, int maxSimpleKeyLength, bool skipAnchorName = false, bool indentSequences = false)
+ public EmitterSettings(int bestIndent, int bestWidth, bool isCanonical, int maxSimpleKeyLength, bool skipAnchorName = false, bool indentSequences = false, string? newLine = null)
{
if (bestIndent < 2 || bestIndent > 9)
{
@@ -87,6 +92,7 @@ public EmitterSettings(int bestIndent, int bestWidth, bool isCanonical, int maxS
MaxSimpleKeyLength = maxSimpleKeyLength;
SkipAnchorName = skipAnchorName;
IndentSequences = indentSequences;
+ NewLine = newLine ?? Environment.NewLine;
}
public EmitterSettings WithBestIndent(int bestIndent)
@@ -122,6 +128,19 @@ public EmitterSettings WithMaxSimpleKeyLength(int maxSimpleKeyLength)
);
}
+ public EmitterSettings WithNewLine(string newLine)
+ {
+ return new EmitterSettings(
+ BestIndent,
+ BestWidth,
+ IsCanonical,
+ MaxSimpleKeyLength,
+ SkipAnchorName,
+ IndentSequences,
+ newLine
+ );
+ }
+
public EmitterSettings Canonical()
{
return new EmitterSettings(
diff --git a/YamlDotNet/Serialization/SerializerBuilder.cs b/YamlDotNet/Serialization/SerializerBuilder.cs
index e8134cb68..63c993c30 100755
--- a/YamlDotNet/Serialization/SerializerBuilder.cs
+++ b/YamlDotNet/Serialization/SerializerBuilder.cs
@@ -309,6 +309,16 @@ public SerializerBuilder JsonCompatible()
.WithEventEmitter(inner => new JsonEventEmitter(inner), loc => loc.InsteadOf());
}
+ ///
+ /// Allows you to override the new line character to use when serializing to YAML.
+ ///
+ /// NewLine character(s) to use when serializing to YAML.
+ public SerializerBuilder WithNewLine(string newLine)
+ {
+ this.emitterSettings = this.emitterSettings.WithNewLine(newLine);
+ return this;
+ }
+
///
/// Registers an additional to be used by the serializer
/// before emitting an object graph.
From a989db3a202963b383cc53d89aa9d13a91f9d462 Mon Sep 17 00:00:00 2001
From: "Aista, Ltd"
Date: Thu, 1 Dec 2022 08:13:25 +0200
Subject: [PATCH 2/2] Ensuring all constructors are applying newLine AND
indentSequences
To avoid loosing information when another "With" method is invoked afterwards
---
YamlDotNet/Core/EmitterSettings.cs | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/YamlDotNet/Core/EmitterSettings.cs b/YamlDotNet/Core/EmitterSettings.cs
index 69ffeaccd..234bcafc4 100644
--- a/YamlDotNet/Core/EmitterSettings.cs
+++ b/YamlDotNet/Core/EmitterSettings.cs
@@ -102,7 +102,9 @@ public EmitterSettings WithBestIndent(int bestIndent)
BestWidth,
IsCanonical,
MaxSimpleKeyLength,
- SkipAnchorName
+ SkipAnchorName,
+ IndentSequences,
+ NewLine
);
}
@@ -113,7 +115,9 @@ public EmitterSettings WithBestWidth(int bestWidth)
bestWidth,
IsCanonical,
MaxSimpleKeyLength,
- SkipAnchorName
+ SkipAnchorName,
+ IndentSequences,
+ NewLine
);
}
@@ -124,7 +128,9 @@ public EmitterSettings WithMaxSimpleKeyLength(int maxSimpleKeyLength)
BestWidth,
IsCanonical,
maxSimpleKeyLength,
- SkipAnchorName
+ SkipAnchorName,
+ IndentSequences,
+ NewLine
);
}
@@ -159,7 +165,9 @@ public EmitterSettings WithoutAnchorName()
BestWidth,
IsCanonical,
MaxSimpleKeyLength,
- true
+ true,
+ IndentSequences,
+ NewLine
);
}
@@ -171,7 +179,8 @@ public EmitterSettings WithIndentedSequences()
IsCanonical,
MaxSimpleKeyLength,
SkipAnchorName,
- true
+ true,
+ NewLine
);
}
}
| |