C#C
C#3y ago
Boats

Serializing a derived class with System.Text.Json

Hello I'm currently running into the following error: "Cannot convert List<DerivedType> to List<BaseType> when trying to use System.Text.Json to serialize a derive class.

DerivedType has a bunch of properties that I am not interested in serializing, The BaseType has properties such as Name, StartTime, and Endtime that I am interested in storing. Is there a way to get this to work. The only thing i can get working is to loop through the list of derive types and just convert them to the base type and passing that list through to the serialize.

I've added a snippet below:

var data = Encoding.UTF8.GetBytes(JsonSerializer.Serialize<List<BaseType>>(deriveTypesList));
Was this page helpful?