Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize Private Properties #429

Open
surr34 opened this issue Aug 23, 2019 · 2 comments
Open

Serialize Private Properties #429

surr34 opened this issue Aug 23, 2019 · 2 comments

Comments

@surr34
Copy link

surr34 commented Aug 23, 2019

Is it possible to serialize private properties as well? In the end I want to serialize all properties that have the YamlMember. I found this question in variations multiple times on Stackoverflow without any answers.

Example:
I want to have both t1 and t2 serialized:

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);
        // t1 has not 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();
    }
}
@aaubry
Copy link
Owner

aaubry commented Aug 26, 2019

Unfortunately it is not possible at this time. Doing so would require to replace a component that is hard-coded, but I will look into fixing that.
Also, your use case seems to make a lot of sense and should probably be supported out of the box.

@leo60228
Copy link

leo60228 commented Dec 2, 2020

Is there any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants