© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
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));
var data = Encoding.UTF8.GetBytes(JsonSerializer.Serialize<List<BaseType>>(deriveTypesList));
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ JSON Deserialize Derived Class
C#CC# / help
2y ago
❔ System.Text.Json deserializing and serializing polymorphic classes WITHOUT a discriminator
C#CC# / help
3y ago
System.Text.Json.JsonException: A possible object cycle was detected
C#CC# / help
2y ago
❔ ✅ System.Text.Json paths
C#CC# / help
3y ago