System.Text.Json.JsonSerializer exception for derived objects
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
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; }
}
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
Thank you for confirmation and pointing me to similar issues.
I'll switch back to Newtonsoft.Json for now.