From c07b87cf2a8a2ad59c75879626232f017a83ce77 Mon Sep 17 00:00:00 2001 From: Yaroslav Bugaria Date: Mon, 13 Jan 2020 21:12:24 -0800 Subject: [PATCH] Fix serialization with DefaultStyle set to Flow (#70) --- .../Serialization/SerializationTests2.cs | 23 +++++++++++++++++++ .../DefaultObjectSerializerBackend.cs | 9 +++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/SharpYaml.Tests/Serialization/SerializationTests2.cs b/SharpYaml.Tests/Serialization/SerializationTests2.cs index 260b3891..3b50d02e 100644 --- a/SharpYaml.Tests/Serialization/SerializationTests2.cs +++ b/SharpYaml.Tests/Serialization/SerializationTests2.cs @@ -1038,6 +1038,29 @@ public void TestStyles() Assert.AreEqual(textReference, text); } + /// + /// Tests the default style. + /// + [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 { diff --git a/SharpYaml/Serialization/Serializers/DefaultObjectSerializerBackend.cs b/SharpYaml/Serialization/Serializers/DefaultObjectSerializerBackend.cs index b01f70a7..eb9a0b45 100644 --- a/SharpYaml/Serialization/Serializers/DefaultObjectSerializerBackend.cs +++ b/SharpYaml/Serialization/Serializers/DefaultObjectSerializerBackend.cs @@ -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