paiden / Nett

.Net library for TOML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Static properties are included in serialization

sighol opened this issue · comments

When reading into a class that has static properties, this causes the static property to change, which may not be intended.

public class MyClass
{
    public static string MyStaticProp { get; set; } = "testing";
    public string MyProp { get; set; } = "MyProp";
}

[TestMethod]
public void MyTestMethod3()
{
    // str includes the MyStaticProp value
    var str = Toml.WriteString(new MyClass());
    
    var serialized = "MyProp = \"AnotherValue\"\nMyStaticProp=\"Something else\"";
    var myclass2 = Toml.ReadString<MyClass>(serialized);
    // MyClass.MyStaticProp is now set to "Something else"
}

Another problem arises when a class has a self referencing static property, like a Size class.

public class Size
{
    public double Width { get; set; }
    public double Height { get; set; }
    public static Size Empty { get; } = new Size { Width = 0, Height = 0};
}

Parsing the Size class causes a StackOverflowException.

Netwonsoft.Json ignores static properties. Should this be the case for Nett as well?

Yes, static properties definitely should be ignored as I cannot think of any use cases where someone wants to serialize static properties. I never thought about statics during the implementation.... :(