By default YamlDotNet does not serialize private properties. Is it possible to serialize also private properties? In the end I would like to serialize all properties that do have the YamlMember
Attribute.
using System;
using System.IO;
using YamlDotNet.Serialization;
class Foo
{
public Foo() {}
public Foo(int t1, int t2)
{
this.T1 = t1;
this.T2 = t2;
}
[YamlMember(Alias = "t1")]
private int T1 { get; set; }
[YamlMember(Alias = "t2")]
public int T2 { get; private set; }
public void Print()
{
Console.WriteLine($"{this.T1} - {this.T2}");
}
}
public class Program
{
public static void Main()
{
var serializer = new SerializerBuilder().Build();
var deserializer = new DeserializerBuilder().Build();
var foo = new Foo(1, 2);
var strWriter = new StringWriter();
serializer.Serialize(strWriter, foo);
// no t1 has been serialized
Console.WriteLine(strWriter.ToString());
var str = @"
t1: 3
t2: 4
";
// Fails, "can't find t1"
var foo2 = (Foo)deserializer.Deserialize(str, typeof(Foo));
foo2.Print();
}
}
I noticed there are some similar questions here on Stackoverflow, unfortunately without any answer, e.g. 1, 2, or this answer, that says it's easy to change but unfortunately not how.