Skip to content

Instantly share code, notes, and snippets.

View keith5000's full-sized avatar

Keith Leonard keith5000

  • Columbia, Maryland, USA
View GitHub Profile
@keith5000
keith5000 / DerivedTypeJsonConverter.cs
Created December 15, 2019 21:05
JSON converter to support derived types/polymorphism (ASP.NET Core 3)
/*
This gist uses a custom JsonConverter to support derived types in ASP.NET Core 3 requests and responses. By default, .NET Core
does not support derived types in JSON serialization; the properties of derived types are not serialized in responses, and in
requests there is inherently a lack of support in JSON for identifying inherited types.
More info: https://stackoverflow.com/questions/59308763/derived-types-properties-missing-in-json-response-from-asp-net-core-api
To implement this code in your project:
1. Copy the DerivedTypeJsonConverter class below into your project.
2. For each of your base types, create a class that derives from DerivedTypeJsonConverter. Follow the MyResultJsonConverter
example below.