runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.Text.Json.JsonSerializer exception for derived objects

Open bashocz opened this issue 7 years ago • 2 comments

I don't know if this is an issue or it is the behavior by design. However Newtonsoft.Json works well in described example below. New JsonSerializer throws an exception (The JSON property name for '{propertyName}' collides with another property.) in both if deserializes json to object as well as serializes object to json. Example: class Program { static void Main(string[] args) { var obj1 = System.Text.Json.JsonSerializer.Deserialize("{ "Value": 1.23 }"); // throws an exception

        var obj2 = new Bar { Value = 1.23, };
        var text = System.Text.Json.JsonSerializer.Serialize(obj2); // throws an exception as well
    }
}

public class Foo
{
    public int Value { get; set; }
}

public class Bar : Foo
{
    public new double Value { get; set; }
}

bashocz avatar Oct 12 '19 13:10 bashocz

It is not support (yet). Similar issues:

https://github.com/dotnet/corefx/issues/39031 https://github.com/dotnet/corefx/issues/41325 https://github.com/dotnet/corefx/issues/38650

Symbai avatar Oct 12 '19 18:10 Symbai

Thank you for confirmation and pointing me to similar issues.

I'll switch back to Newtonsoft.Json for now.

bashocz avatar Oct 12 '19 19:10 bashocz