runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Json Deserialize issue with class has "new modifier" property

Open nhuthan opened this issue 7 years ago • 1 comments

Here is simple code:

public class Program
{
	public static void Main()
	{
		var str = "{\"P1\": 123}";
		var obj = JsonSerializer.Deserialize<B>(str);	//--> throw
		
	}
}

public class A{
	public int P1{get;set;}	
	public int P2{get;set;}
}

public class B: A{
	public new float P1{get;set;}
}

And I got an error:

Unhandled exception. System.InvalidOperationException: The JSON property name for 'B.P1' collides with another property.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(JsonClassInfo jsonClassInfo, JsonPropertyInfo jsonPropertyInfo)
   at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.Main()
Command terminated by signal 6

nhuthan avatar Nov 23 '19 01:11 nhuthan

Closing - duplicate of https://github.com/dotnet/corefx/issues/41338, but for deserialization.

layomia avatar Nov 23 '19 04:11 layomia