From 93ced63eb34d32d8e50de10a46212e5dfc7f4a43 Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Mon, 13 Jun 2016 11:47:40 -0700 Subject: [PATCH 1/8] Escape single quotes for string values --- .../EventEmitters/TypeAssigningEventEmitter.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs index 889238f5f..f0f3ad0a5 100644 --- a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs +++ b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs @@ -80,7 +80,7 @@ public override void Emit(ScalarEventInfo eventInfo) case TypeCode.String: case TypeCode.Char: eventInfo.Tag = "tag:yaml.org,2002:str"; - eventInfo.RenderedValue = eventInfo.Source.Value.ToString(); + eventInfo.RenderedValue = Escape(eventInfo.Source.Value.ToString()); suggestedStyle = ScalarStyle.Any; break; @@ -135,5 +135,12 @@ private void AssignTypeIfDifferent(ObjectEventInfo eventInfo) } } } + + private string Escape(string value) + { + return value.Contains("'") + ? string.Format("\"{0}\"", value) + : value; + } } } \ No newline at end of file From 0fa23e90c1d777ab1c5cc6978a710f8ea2bad720 Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Mon, 13 Jun 2016 13:21:53 -0700 Subject: [PATCH 2/8] Fix for serialization of strings containing single quotes --- .../TypeAssigningEventEmitterTests.cs | 76 +++++++++++++++++++ YamlDotNet.Test/YamlDotNet.Test.csproj | 1 + 2 files changed, 77 insertions(+) create mode 100644 YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs diff --git a/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs b/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs new file mode 100644 index 000000000..4c9a324f3 --- /dev/null +++ b/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs @@ -0,0 +1,76 @@ +using FakeItEasy; +using Xunit; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.EventEmitters; + +namespace YamlDotNet.Test.Serialization.EventEmitters +{ + public class TypeAssigningEventEmitterTests + { + private readonly TypeAssigningEventEmitter _emitter; + + public TypeAssigningEventEmitterTests() + { + var nextEmitterMock = A.Fake(); + _emitter = new TypeAssigningEventEmitter(nextEmitterMock, false); + } + + [Fact] + void Emit_StringValueWithSingleQuote_EscapedByDoubleQuotes() + { + string value = "'test"; + var info = new ScalarEventInfo(CreateObjectDescriptor(value)); + + _emitter.Emit(info); + + Assert.Equal(EscapedValue(value), info.RenderedValue); + } + + [Fact] + void Emit_CharValueWithSingleQuote_EscapedByDoubleQuotes() + { + char value = '\''; + var info = new ScalarEventInfo(CreateObjectDescriptor(value)); + + _emitter.Emit(info); + + Assert.Equal(EscapedValue(value.ToString()), info.RenderedValue); + } + + [Fact] + void Emit_StringTypeWithNoSingleQuotes_RemainsUnchanged() + { + string value = "test"; + var info = new ScalarEventInfo(CreateObjectDescriptor(value)); + + _emitter.Emit(info); + + Assert.Equal(value, info.RenderedValue); + } + + [Fact] + void Emit_CharTypeWithNoSingleQuotes_RemainsUnchanged() + { + char value = 'a'; + var info = new ScalarEventInfo(CreateObjectDescriptor(value)); + + _emitter.Emit(info); + + Assert.Equal(value.ToString(), info.RenderedValue); + } + + + private IObjectDescriptor CreateObjectDescriptor(T value) + { + var objectDescriptor = A.Fake(); + A.CallTo(() => objectDescriptor.Type).Returns(typeof(T)); + A.CallTo(() => objectDescriptor.Value).Returns(value); + return objectDescriptor; + } + + private string EscapedValue(string value) + { + return string.Format("\"{0}\"", value); + } + } +} diff --git a/YamlDotNet.Test/YamlDotNet.Test.csproj b/YamlDotNet.Test/YamlDotNet.Test.csproj index b0ea8f9e9..7fe0b4682 100644 --- a/YamlDotNet.Test/YamlDotNet.Test.csproj +++ b/YamlDotNet.Test/YamlDotNet.Test.csproj @@ -98,6 +98,7 @@ + From ec4f82cfebeecad2277c9f4ffc634ea34cd1639f Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Mon, 13 Jun 2016 15:41:32 -0700 Subject: [PATCH 3/8] Changed emitter to double quote strings containing single quotes --- .../EventEmitters/TypeAssigningEventEmitter.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs index f0f3ad0a5..ba95b0625 100644 --- a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs +++ b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs @@ -80,8 +80,10 @@ public override void Emit(ScalarEventInfo eventInfo) case TypeCode.String: case TypeCode.Char: eventInfo.Tag = "tag:yaml.org,2002:str"; - eventInfo.RenderedValue = Escape(eventInfo.Source.Value.ToString()); - suggestedStyle = ScalarStyle.Any; + eventInfo.RenderedValue = eventInfo.Source.Value.ToString(); + suggestedStyle = eventInfo.RenderedValue.Contains("'") + ? ScalarStyle.DoubleQuoted + : ScalarStyle.Any; break; case TypeCode.DateTime: @@ -135,12 +137,5 @@ private void AssignTypeIfDifferent(ObjectEventInfo eventInfo) } } } - - private string Escape(string value) - { - return value.Contains("'") - ? string.Format("\"{0}\"", value) - : value; - } } } \ No newline at end of file From 3152da8f3c78b9907d8dda1101d44d9e411d5bec Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Mon, 13 Jun 2016 15:48:31 -0700 Subject: [PATCH 4/8] Updated unit tests --- .../TypeAssigningEventEmitterTests.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs b/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs index 4c9a324f3..d3016350b 100644 --- a/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs +++ b/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs @@ -1,5 +1,6 @@ using FakeItEasy; using Xunit; +using YamlDotNet.Core; using YamlDotNet.Serialization; using YamlDotNet.Serialization.EventEmitters; @@ -16,47 +17,58 @@ public TypeAssigningEventEmitterTests() } [Fact] - void Emit_StringValueWithSingleQuote_EscapedByDoubleQuotes() + void Emit_StringValueSingleQuote_SetsStyleToDoubleQuoted() { - string value = "'test"; + string value = "'"; var info = new ScalarEventInfo(CreateObjectDescriptor(value)); _emitter.Emit(info); - Assert.Equal(EscapedValue(value), info.RenderedValue); + Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); } [Fact] - void Emit_CharValueWithSingleQuote_EscapedByDoubleQuotes() + void Emit_StringValueTextWithSingleQuotes_SetsStyleToDoubleQuoted() { - char value = '\''; + string value = "'asdf'"; var info = new ScalarEventInfo(CreateObjectDescriptor(value)); _emitter.Emit(info); - Assert.Equal(EscapedValue(value.ToString()), info.RenderedValue); + Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); } [Fact] - void Emit_StringTypeWithNoSingleQuotes_RemainsUnchanged() + void Emit_CharValueSingleQuote_SetsStyleToDoubleQuoted() + { + char value = '\''; + var info = new ScalarEventInfo(CreateObjectDescriptor(value)); + + _emitter.Emit(info); + + Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); + } + + [Fact] + void Emit_StringValueWithNoSingleQuotes_SetsStyleToAny() { string value = "test"; var info = new ScalarEventInfo(CreateObjectDescriptor(value)); _emitter.Emit(info); - Assert.Equal(value, info.RenderedValue); + Assert.Equal(ScalarStyle.Any, info.Style); } [Fact] - void Emit_CharTypeWithNoSingleQuotes_RemainsUnchanged() + void Emit_CharValueWithNoSingleQuotes_SetsStyleToAny() { char value = 'a'; var info = new ScalarEventInfo(CreateObjectDescriptor(value)); _emitter.Emit(info); - Assert.Equal(value.ToString(), info.RenderedValue); + Assert.Equal(ScalarStyle.Any, info.Style); } @@ -65,6 +77,7 @@ private IObjectDescriptor CreateObjectDescriptor(T value) var objectDescriptor = A.Fake(); A.CallTo(() => objectDescriptor.Type).Returns(typeof(T)); A.CallTo(() => objectDescriptor.Value).Returns(value); + A.CallTo(() => objectDescriptor.ScalarStyle).Returns(ScalarStyle.Any); return objectDescriptor; } From 488aef9b144f6ff236585a0ff5e239740d4547e3 Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Fri, 17 Jun 2016 10:41:32 -0700 Subject: [PATCH 5/8] Moved single quotes fix to Emitter rather than TyperAssigningEventEmitter --- YamlDotNet.Test/Core/EmitterTests.cs | 17 ++++ .../TypeAssigningEventEmitterTests.cs | 89 ------------------- YamlDotNet.Test/YamlDotNet.Test.csproj | 1 - YamlDotNet/Core/Emitter.cs | 9 +- .../TypeAssigningEventEmitter.cs | 4 +- 5 files changed, 25 insertions(+), 95 deletions(-) delete mode 100644 YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs diff --git a/YamlDotNet.Test/Core/EmitterTests.cs b/YamlDotNet.Test/Core/EmitterTests.cs index 70e739fab..5c3803e3b 100644 --- a/YamlDotNet.Test/Core/EmitterTests.cs +++ b/YamlDotNet.Test/Core/EmitterTests.cs @@ -352,6 +352,23 @@ public void NewLinesAreNotDuplicatedWhenEmitted(string input, string expected) StreamEnd); } + [Theory] + [InlineData("'.'test")] + [InlineData("'")] + [InlineData("'.'")] + [InlineData("'test")] + [InlineData("'test'")] + public void SingleQuotesAreDoubleQuoted(string input) + { + var events = StreamOf(DocumentWith(PlainScalar(input))); + var yaml = EmittedTextFrom(events); + + string expected = string.Format("\"{0}\"", input); + + yaml.Should().Contain(expected); + } + + private string Lines(params string[] lines) { return string.Join(Environment.NewLine, lines); diff --git a/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs b/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs deleted file mode 100644 index d3016350b..000000000 --- a/YamlDotNet.Test/Serialization/EventEmitters/TypeAssigningEventEmitterTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using FakeItEasy; -using Xunit; -using YamlDotNet.Core; -using YamlDotNet.Serialization; -using YamlDotNet.Serialization.EventEmitters; - -namespace YamlDotNet.Test.Serialization.EventEmitters -{ - public class TypeAssigningEventEmitterTests - { - private readonly TypeAssigningEventEmitter _emitter; - - public TypeAssigningEventEmitterTests() - { - var nextEmitterMock = A.Fake(); - _emitter = new TypeAssigningEventEmitter(nextEmitterMock, false); - } - - [Fact] - void Emit_StringValueSingleQuote_SetsStyleToDoubleQuoted() - { - string value = "'"; - var info = new ScalarEventInfo(CreateObjectDescriptor(value)); - - _emitter.Emit(info); - - Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); - } - - [Fact] - void Emit_StringValueTextWithSingleQuotes_SetsStyleToDoubleQuoted() - { - string value = "'asdf'"; - var info = new ScalarEventInfo(CreateObjectDescriptor(value)); - - _emitter.Emit(info); - - Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); - } - - [Fact] - void Emit_CharValueSingleQuote_SetsStyleToDoubleQuoted() - { - char value = '\''; - var info = new ScalarEventInfo(CreateObjectDescriptor(value)); - - _emitter.Emit(info); - - Assert.Equal(ScalarStyle.DoubleQuoted, info.Style); - } - - [Fact] - void Emit_StringValueWithNoSingleQuotes_SetsStyleToAny() - { - string value = "test"; - var info = new ScalarEventInfo(CreateObjectDescriptor(value)); - - _emitter.Emit(info); - - Assert.Equal(ScalarStyle.Any, info.Style); - } - - [Fact] - void Emit_CharValueWithNoSingleQuotes_SetsStyleToAny() - { - char value = 'a'; - var info = new ScalarEventInfo(CreateObjectDescriptor(value)); - - _emitter.Emit(info); - - Assert.Equal(ScalarStyle.Any, info.Style); - } - - - private IObjectDescriptor CreateObjectDescriptor(T value) - { - var objectDescriptor = A.Fake(); - A.CallTo(() => objectDescriptor.Type).Returns(typeof(T)); - A.CallTo(() => objectDescriptor.Value).Returns(value); - A.CallTo(() => objectDescriptor.ScalarStyle).Returns(ScalarStyle.Any); - return objectDescriptor; - } - - private string EscapedValue(string value) - { - return string.Format("\"{0}\"", value); - } - } -} diff --git a/YamlDotNet.Test/YamlDotNet.Test.csproj b/YamlDotNet.Test/YamlDotNet.Test.csproj index 7fe0b4682..b0ea8f9e9 100644 --- a/YamlDotNet.Test/YamlDotNet.Test.csproj +++ b/YamlDotNet.Test/YamlDotNet.Test.csproj @@ -98,7 +98,6 @@ - diff --git a/YamlDotNet/Core/Emitter.cs b/YamlDotNet/Core/Emitter.cs index 46ed62591..65b951506 100644 --- a/YamlDotNet/Core/Emitter.cs +++ b/YamlDotNet/Core/Emitter.cs @@ -340,7 +340,7 @@ private void AnalyzeScalar(Scalar scalar) { if (isFirst) { - if (buffer.Check(@"#,[]{}&*!|>\""%@`")) + if (buffer.Check(@"#,[]{}&*!|>\""%@`'")) { flowIndicators = true; blockIndicators = true; @@ -478,6 +478,11 @@ private void AnalyzeScalar(Scalar scalar) scalarData.isSingleQuotedAllowed = false; scalarData.isBlockAllowed = false; } + + if (value.Contains("'")) + { + scalarData.isSingleQuotedAllowed = false; + } scalarData.isMultiline = lineBreaks; if (lineBreaks) @@ -890,7 +895,7 @@ private void SelectScalarStyle(ParsingEvent evt) { if ((flowLevel != 0 && !scalarData.isFlowPlainAllowed) || (flowLevel == 0 && !scalarData.isBlockPlainAllowed)) { - style = ScalarStyle.SingleQuoted; + style = scalarData.isSingleQuotedAllowed ? ScalarStyle.SingleQuoted : ScalarStyle.DoubleQuoted; } if (string.IsNullOrEmpty(scalarData.value) && (flowLevel != 0 || isSimpleKeyContext)) { diff --git a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs index ba95b0625..889238f5f 100644 --- a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs +++ b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs @@ -81,9 +81,7 @@ public override void Emit(ScalarEventInfo eventInfo) case TypeCode.Char: eventInfo.Tag = "tag:yaml.org,2002:str"; eventInfo.RenderedValue = eventInfo.Source.Value.ToString(); - suggestedStyle = eventInfo.RenderedValue.Contains("'") - ? ScalarStyle.DoubleQuoted - : ScalarStyle.Any; + suggestedStyle = ScalarStyle.Any; break; case TypeCode.DateTime: From fb341da35976af5e414d9ad150469a731f198ad0 Mon Sep 17 00:00:00 2001 From: Ryan Criddle Date: Tue, 21 Jun 2016 20:15:47 -0700 Subject: [PATCH 6/8] Changed method of setting style when single quotes encountered --- YamlDotNet.Test/Core/EmitterTests.cs | 4 ++-- YamlDotNet/Core/Emitter.cs | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/YamlDotNet.Test/Core/EmitterTests.cs b/YamlDotNet.Test/Core/EmitterTests.cs index 5c3803e3b..d62aab8fc 100644 --- a/YamlDotNet.Test/Core/EmitterTests.cs +++ b/YamlDotNet.Test/Core/EmitterTests.cs @@ -360,9 +360,9 @@ public void NewLinesAreNotDuplicatedWhenEmitted(string input, string expected) [InlineData("'test'")] public void SingleQuotesAreDoubleQuoted(string input) { - var events = StreamOf(DocumentWith(PlainScalar(input))); + var events = StreamOf(DocumentWith(new Scalar(input))); var yaml = EmittedTextFrom(events); - + string expected = string.Format("\"{0}\"", input); yaml.Should().Contain(expected); diff --git a/YamlDotNet/Core/Emitter.cs b/YamlDotNet/Core/Emitter.cs index 65b951506..43e2a34c2 100644 --- a/YamlDotNet/Core/Emitter.cs +++ b/YamlDotNet/Core/Emitter.cs @@ -479,11 +479,6 @@ private void AnalyzeScalar(Scalar scalar) scalarData.isBlockAllowed = false; } - if (value.Contains("'")) - { - scalarData.isSingleQuotedAllowed = false; - } - scalarData.isMultiline = lineBreaks; if (lineBreaks) { @@ -878,7 +873,14 @@ private void SelectScalarStyle(ParsingEvent evt) if (style == ScalarStyle.Any) { - style = scalarData.isMultiline ? ScalarStyle.Folded : ScalarStyle.Plain; + if (scalarData.value.Contains("'")) + { + style = ScalarStyle.DoubleQuoted; + } + else + { + style = scalarData.isMultiline ? ScalarStyle.Folded : ScalarStyle.Plain; + } } if (isCanonical) From ec05ba92ad4ced6f6038401647b200edbd48920e Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Fri, 24 Jun 2016 18:07:00 +0100 Subject: [PATCH 7/8] Add unit test that fails when the scalar should not be quoted. --- YamlDotNet.Test/Core/EmitterTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/YamlDotNet.Test/Core/EmitterTests.cs b/YamlDotNet.Test/Core/EmitterTests.cs index d62aab8fc..10cc4a6b2 100644 --- a/YamlDotNet.Test/Core/EmitterTests.cs +++ b/YamlDotNet.Test/Core/EmitterTests.cs @@ -368,6 +368,14 @@ public void SingleQuotesAreDoubleQuoted(string input) yaml.Should().Contain(expected); } + [Theory] + [InlineData("hello\n'world")] + public void SingleQuotesAreNotDoubleQuotedUnlessNecessary(string input) + { + var events = StreamOf(DocumentWith(new Scalar(input))); + var yaml = EmittedTextFrom(events); + yaml.Should().NotContain("\""); + } private string Lines(params string[] lines) { From 804adfd081cc27b78781d2fef9b6164df3fcc5ca Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Fri, 24 Jun 2016 18:09:39 +0100 Subject: [PATCH 8/8] Change scalar style selection heuristic to keep using folded style for multiline scalars. --- YamlDotNet/Core/Emitter.cs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/YamlDotNet/Core/Emitter.cs b/YamlDotNet/Core/Emitter.cs index 43e2a34c2..132571e87 100644 --- a/YamlDotNet/Core/Emitter.cs +++ b/YamlDotNet/Core/Emitter.cs @@ -93,6 +93,7 @@ private class ScalarData public bool isBlockPlainAllowed; public bool isSingleQuotedAllowed; public bool isBlockAllowed; + public bool hasSingleQuotes; public ScalarStyle style; } @@ -325,6 +326,7 @@ private void AnalyzeScalar(Scalar scalar) var leadingBreak = false; var trailingSpace = false; var trailingBreak = false; + var leadingQuote = false; var breakSpace = false; var spaceBreak = false; @@ -334,6 +336,7 @@ private void AnalyzeScalar(Scalar scalar) var lineBreaks = false; var specialCharacters = !ValueIsRepresentableInOutputEncoding(value); + var singleQuotes = false; var isFirst = true; while (!buffer.EndOfInput) @@ -344,13 +347,17 @@ private void AnalyzeScalar(Scalar scalar) { flowIndicators = true; blockIndicators = true; + leadingQuote = buffer.Check('\''); + singleQuotes |= buffer.Check('\''); } if (buffer.Check("?:")) { flowIndicators = true; if (followedByWhitespace) + { blockIndicators = true; + } } if (buffer.Check('-') && followedByWhitespace) @@ -380,6 +387,8 @@ private void AnalyzeScalar(Scalar scalar) flowIndicators = true; blockIndicators = true; } + + singleQuotes |= buffer.Check('\''); } if (!specialCharacters && !buffer.IsPrintable()) @@ -453,7 +462,7 @@ private void AnalyzeScalar(Scalar scalar) scalarData.isSingleQuotedAllowed = true; scalarData.isBlockAllowed = true; - if (leadingSpace || leadingBreak || trailingSpace || trailingBreak) + if (leadingSpace || leadingBreak || trailingSpace || trailingBreak || leadingQuote) { scalarData.isFlowPlainAllowed = false; scalarData.isBlockPlainAllowed = false; @@ -487,10 +496,16 @@ private void AnalyzeScalar(Scalar scalar) } if (flowIndicators) + { scalarData.isFlowPlainAllowed = false; + } if (blockIndicators) + { scalarData.isBlockPlainAllowed = false; + } + + scalarData.hasSingleQuotes = singleQuotes; } private bool ValueIsRepresentableInOutputEncoding(string value) @@ -873,14 +888,7 @@ private void SelectScalarStyle(ParsingEvent evt) if (style == ScalarStyle.Any) { - if (scalarData.value.Contains("'")) - { - style = ScalarStyle.DoubleQuoted; - } - else - { - style = scalarData.isMultiline ? ScalarStyle.Folded : ScalarStyle.Plain; - } + style = scalarData.isMultiline ? ScalarStyle.Folded : ScalarStyle.Plain; } if (isCanonical) @@ -897,7 +905,7 @@ private void SelectScalarStyle(ParsingEvent evt) { if ((flowLevel != 0 && !scalarData.isFlowPlainAllowed) || (flowLevel == 0 && !scalarData.isBlockPlainAllowed)) { - style = scalarData.isSingleQuotedAllowed ? ScalarStyle.SingleQuoted : ScalarStyle.DoubleQuoted; + style = (scalarData.isSingleQuotedAllowed && !scalarData.hasSingleQuotes) ? ScalarStyle.SingleQuoted : ScalarStyle.DoubleQuoted; } if (string.IsNullOrEmpty(scalarData.value) && (flowLevel != 0 || isSimpleKeyContext)) {