public void SerializationRespectsScalarStyleOverride() { var writer = new StringWriter(); var obj = new ScalarStyleExample(); var overrides = new YamlAttributeOverrides(); var style1 = new YamlMemberAttribute(); style1.ScalarStyle = ScalarStyle.DoubleQuoted; var style2 = new YamlMemberAttribute(); style2.ScalarStyle = ScalarStyle.Literal; overrides.Add(typeof(ScalarStyleExample), "LiteralString", style1); overrides.Add(typeof(ScalarStyleExample), "DoubleQuotedString", style2); var serializer = new Serializer(overrides: overrides); serializer.Serialize(writer, obj); var serialized = writer.ToString(); Dump.WriteLine(serialized); serialized.Should() .Be("LiteralString: \"Test\"\r\nDoubleQuotedString: |-\r\n Test\r\n", "the properties should be specifically styled"); }
public void RoundtripAliasOverride() { var writer = new StringWriter(); var input = new NameConvention { AliasTest = "Fourth" }; var overrides = new YamlAttributeOverrides(); var attribute = new YamlMemberAttribute(); attribute.Alias = "fourthOverride"; overrides.Add(typeof(NameConvention), "AliasTest", attribute); var serializer = new Serializer(overrides: overrides); serializer.Serialize(writer, input, input.GetType()); var text = writer.ToString(); // Todo: use RegEx once FluentAssertions 2.2 is released text.TrimEnd('\r', '\n').Should().Be("fourthOverride: Fourth"); var deserializer = new Deserializer(overrides: overrides); var output = deserializer.Deserialize <NameConvention>(UsingReaderFor(text)); output.AliasTest.Should().Be(input.AliasTest); }
public void SerializationRespectsYamlIgnoreOverride() { var writer = new StringWriter(); var obj = new Simple(); var overrides = new YamlAttributeOverrides(); var ignore = new YamlIgnoreAttribute(); overrides.Add(typeof(Simple), "aaa", ignore); var serializer = new Serializer(overrides: overrides); serializer.Serialize(writer, obj); var serialized = writer.ToString(); Dump.WriteLine(serialized); serialized.Should().NotContain("aaa"); }
public void SerializationBaseAttributeOverride() { var writer = new StringWriter(); var obj = new Derived { DerivedProperty = "Derived", BaseProperty = "Base" }; var overrides = new YamlAttributeOverrides(); var ignore = new YamlIgnoreAttribute(); overrides.Add(typeof(Base), "BaseProperty", ignore); var serializer = new Serializer(overrides: overrides); serializer.Serialize(writer, obj); var serialized = writer.ToString(); Dump.WriteLine(serialized); serialized.Should() .Be("DerivedProperty: Derived\r\n", "the base property should be specifically ignored"); }
public void AttributeOverridesAndNamingConventionDoNotConflictInLegacyConfiguration() { var overrides = new YamlAttributeOverrides(); overrides.Add <Foo>(f => f.IsRequired, new YamlMemberAttribute { Alias = "Required" }); var namingConvention = new CamelCaseNamingConvention(); var serializer = new Serializer(namingConvention: namingConvention, overrides: overrides); var yaml = serializer.Serialize(new Foo { IsRequired = true }); Assert.Contains("required: true", yaml); var deserializer = new Deserializer(namingConvention: namingConvention, overrides: overrides); var deserializedFoo = deserializer.Deserialize <Foo>(yaml); Assert.True(deserializedFoo.IsRequired); }
public static void Add <TClass>(this YamlAttributeOverrides overrides, Expression <Func <TClass, object> > propertyAccessor, Attribute attribute) { var property = propertyAccessor.AsProperty(); overrides.Add(typeof(TClass), property.Name, attribute); }
public void RoundtripAliasOverride() { var writer = new StringWriter(); var input = new NameConvention { AliasTest = "Fourth" }; var overrides = new YamlAttributeOverrides(); var attribute = new YamlMemberAttribute(); attribute.Alias = "fourthOverride"; overrides.Add(typeof(NameConvention), "AliasTest", attribute); var serializer = new Serializer(overrides: overrides); serializer.Serialize(writer, input, input.GetType()); var text = writer.ToString(); // Todo: use RegEx once FluentAssertions 2.2 is released text.TrimEnd('\r', '\n').Should().Be("fourthOverride: Fourth"); var deserializer = new Deserializer(overrides: overrides); var output = deserializer.Deserialize<NameConvention>(UsingReaderFor(text)); output.AliasTest.Should().Be(input.AliasTest); }