Skip to content

Commit

Permalink
Merge pull request #187 from aaubry/Archanian-issue-135-single-quote-…
Browse files Browse the repository at this point in the history
…serialization-error

Archanian issue 135 single quote serialization error
  • Loading branch information
aaubry authored Jun 29, 2016
2 parents 1befe05 + 804adfd commit 5dbf200
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
25 changes: 25 additions & 0 deletions YamlDotNet.Test/Core/EmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,31 @@ 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(new Scalar(input)));
var yaml = EmittedTextFrom(events);

string expected = string.Format("\"{0}\"", 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)
{
return string.Join(Environment.NewLine, lines);
Expand Down
23 changes: 19 additions & 4 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private class ScalarData
public bool isBlockPlainAllowed;
public bool isSingleQuotedAllowed;
public bool isBlockAllowed;
public bool hasSingleQuotes;
public ScalarStyle style;
}

Expand Down Expand Up @@ -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;
Expand All @@ -334,23 +336,28 @@ private void AnalyzeScalar(Scalar scalar)
var lineBreaks = false;

var specialCharacters = !ValueIsRepresentableInOutputEncoding(value);
var singleQuotes = false;

var isFirst = true;
while (!buffer.EndOfInput)
{
if (isFirst)
{
if (buffer.Check(@"#,[]{}&*!|>\""%@`"))
if (buffer.Check(@"#,[]{}&*!|>\""%@`'"))
{
flowIndicators = true;
blockIndicators = true;
leadingQuote = buffer.Check('\'');
singleQuotes |= buffer.Check('\'');
}

if (buffer.Check("?:"))
{
flowIndicators = true;
if (followedByWhitespace)
{
blockIndicators = true;
}
}

if (buffer.Check('-') && followedByWhitespace)
Expand Down Expand Up @@ -380,6 +387,8 @@ private void AnalyzeScalar(Scalar scalar)
flowIndicators = true;
blockIndicators = true;
}

singleQuotes |= buffer.Check('\'');
}

if (!specialCharacters && !buffer.IsPrintable())
Expand Down Expand Up @@ -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;
Expand All @@ -478,7 +487,7 @@ private void AnalyzeScalar(Scalar scalar)
scalarData.isSingleQuotedAllowed = false;
scalarData.isBlockAllowed = false;
}

scalarData.isMultiline = lineBreaks;
if (lineBreaks)
{
Expand All @@ -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)
Expand Down Expand Up @@ -890,7 +905,7 @@ private void SelectScalarStyle(ParsingEvent evt)
{
if ((flowLevel != 0 && !scalarData.isFlowPlainAllowed) || (flowLevel == 0 && !scalarData.isBlockPlainAllowed))
{
style = ScalarStyle.SingleQuoted;
style = (scalarData.isSingleQuotedAllowed && !scalarData.hasSingleQuotes) ? ScalarStyle.SingleQuoted : ScalarStyle.DoubleQuoted;
}
if (string.IsNullOrEmpty(scalarData.value) && (flowLevel != 0 || isSimpleKeyContext))
{
Expand Down

0 comments on commit 5dbf200

Please sign in to comment.