Skip to content

Commit

Permalink
Merge pull request #72 from yariker/issue_70
Browse files Browse the repository at this point in the history
Fix serialization with DefaultStyle set to Flow (#70)
  • Loading branch information
xoofx authored Feb 21, 2020
2 parents 779a4ab + c07b87c commit efc09a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 23 additions & 0 deletions SharpYaml.Tests/Serialization/SerializationTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,29 @@ public void TestStyles()
Assert.AreEqual(textReference, text);
}

/// <summary>
/// Tests the default style.
/// </summary>
[Test]
public void TestDefaultStyle()
{
var testObject = new
{
Name = "John Doe",
Value = 42,
Items = new[] { 1, 2, 3 },
};

var settings1 = new SerializerSettings {DefaultStyle = YamlStyle.Flow};
var serializer1 = new Serializer(settings1);
var yaml1 = serializer1.Serialize(testObject);
Assert.AreEqual("{Items: [1, 2, 3], Name: John Doe, Value: 42}\r\n", yaml1);

var settings2 = new SerializerSettings {DefaultStyle = YamlStyle.Block};
var serializer2 = new Serializer(settings2);
var yaml2 = serializer2.Serialize(testObject);
Assert.AreEqual("Items:\r\n - 1\r\n - 2\r\n - 3\r\nName: John Doe\r\nValue: 42\r\n", yaml2);
}

public class ClassWithKeyTransform
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ public virtual YamlStyle GetStyle(ref ObjectContext objectContext)
}
}

style = objectContext.Instance == null || count >= objectContext.SerializerContext.Settings.LimitPrimitiveFlowSequence || !isPrimitiveElementType
? YamlStyle.Block
: YamlStyle.Flow;
if (isPrimitiveElementType)
{
style = objectContext.Instance == null || count >= objectContext.SerializerContext.Settings.LimitPrimitiveFlowSequence
? YamlStyle.Block
: YamlStyle.Flow;
}
}

// If not defined, get the default style
Expand Down

0 comments on commit efc09a7

Please sign in to comment.