You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using System;using System.Collections.Generic;using System.Globalization;using System.Text.Json;namespaceSystemTextJsonTest{classProgram{staticvoidMain(string[]args){varapple=new Apple
{Id= Guid.NewGuid(),Bananas=newList<Banana>{new Banana0{Id= Guid.NewGuid(),Value=1},new Banana1{Id= Guid.NewGuid(),Value= DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}}};//The property "Value" of the derived class is lost//{"Id":"fead1a5a-87a2-41c2-a1ba-c48fda1da67a","Bananas":[{"Id":"8fe8ff02-4bc8-42db-8c44-e014ae522d00"},{"Id":"236edd74-8cdd-40f9-87e0-70fe0a2adba4"}]}varjson= JsonSerializer.Serialize(apple);
Console.WriteLine(json);
Console.ReadLine();}}publicclassApple{publicGuidId{get;set;}publicList<Banana> Bananas {get;set;}}publicclassBanana{publicGuidId{get;set;}}publicclassBanana0:Banana{publicintValue{get;set;}}publicclassBanana1:Banana{publicstringValue{get;set;}}}
The text was updated successfully, but these errors were encountered:
using System;using System.Collections.Generic;using System.Globalization;using System.Text.Json;namespaceSystemTextJsonTest{classProgram{staticvoidMain(string[]args){varapple=new Apple
{Id= Guid.NewGuid(),Bananas=newList<Banana>{new Banana0{Id= Guid.NewGuid(),Value=1},new Banana1{Id= Guid.NewGuid(),Value= DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}}};//The property "Value" of the derived class is lost//{"Id":"fead1a5a-87a2-41c2-a1ba-c48fda1da67a","Bananas":[{"Id":"8fe8ff02-4bc8-42db-8c44-e014ae522d00"},{"Id":"236edd74-8cdd-40f9-87e0-70fe0a2adba4"}]}varjson= JsonSerializer.Serialize(apple);
Console.WriteLine(json);
Console.ReadLine();}}publicclassApple{publicGuidId{get;set;}publicList<Banana> Bananas {get;set;}}publicclassBanana{publicGuidId{get;set;}}publicclassBanana0:Banana{publicintValue{get;set;}}publicclassBanana1:Banana{publicstringValue{get;set;}}}
As @huoyaoyuan mentioned, this behaviour is by design. We are working on adding opt-in behaviour that should permit polymorphic serialization in future releases.
Target framework 5.0,here is the demo.
The text was updated successfully, but these errors were encountered: