dotnet-json-polymorphic
/dotnet-json-polymorphic
Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 2
No signals detected. Repository changed on 15.09.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
Triggers from the description
Original author description
Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
How it works
- When to Use
- Requirements
- Steps
- Example Conversion
- Notes
- Documentation
SKILL.md preview
---
name: dotnet-json-polymorphic
description: Configures polymorphic JSON serialization with [JsonPolymorphic] and [JsonDerivedType] attributes. Also use when the user mentions "polymorphic JSON," "JsonDerivedType," "JSON inheritance," "type discriminator," "serialize derived types," or "JSON polymorphism." For full JSON source generation, see dotnet-source-gen-json.
license: MIT
metadata:
author: Im5tu
version: "1.0"
repositoryUrl: https://github.com/im5tu/dotnet-skills
allowed-tools: Bash(dotnet:*), Read, Edit, Write, Glob, Grep, AskUserQuestion
---
Configure polymorphic JSON serialization using `[JsonPolymorphic]` and `[JsonDerivedType]` attributes for type-safe inheritance hierarchies.
## When to Use
- Serializing inheritance hierarchies with System.Text.Json
- Adding type discriminators for polymorphic deserialization
- Preparing polymorphic types for AOT-compatible JSON source generation
- Replacing Newtonsoft.Json `TypeNameHandling` with `[JsonPolymorphic]`/`[JsonDerivedType]`
## Requirements
- .NET 7 or higher
## Steps
1. **Ask scope**:
- Ask user: "Apply to entire solution or specific project?"
- If specific project, ask which project
2. **Search for existing `[JsonPolymorphic]` attributes**:
- Detect existing project conventions
- Note any `TypeDiscriminatorPropertyName` values found
3. **Extract discriminator conventions**:
- Search for `TypeDiscriminatorPropertyName` in existing attributes
- Record all unique discriminator property names found
4. **Find polymorphic candidates**:
- Search for abstract classes with derived types
- Search for interfaces with implementing classes used in serialization
- Look for classes with virtual members that are inherited
5. **Ask about discriminator property name**:
- If existing discriminators found, show them to user
- Ask: "What discriminator property name? (Default: `$type`, Found: [list existing])"
6. **Check for consistency**:
- If multiple different discriminators found in codebase
- Ask: "Found multiple discriminators: [list]. Standardize to single value?"
7. **Apply attributes to base types**:
```csharp
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(DerivedA), typeDiscriminator: "DerivedA")]
[JsonDerivedType(typeof(DerivedB), typeDiscriminator: "DerivedB")]
public abstract class BaseClass
{
}
```
- Use full type name as discriminator value (e.g., `"WeatherForecastWithCity"`)
8. **Add using directive**:
- Ensure `using System.Text.Json.Serialization;` is present
9. **Verify with build**:
```bash
dotnet build
```
10. **Report results**:
- List all base types configured
- List all derived type mappings added
- Confirm build status
## Example Conversion
**Before:**
```csharp
public abstract class Notification
{
public string Message { get; set; }
}
public class EmailNotification : Notification
{
public string EmailAddress { get; set; }
}
public class SmsNotification : Notification
{
public string PhoneNumber { get; set; }
}
```
**After:**
```csharp
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(EmailNotification), typeDiscriminator: "EmailNotification")]
[JsonDerivedType(typeof(SmsNotification), typeDiscriminator: "SmsNotification")]
public abstract class Notification
{
public string Message { get; set; }
}
public class EmailNotification : Notification
{
public string EmailAddress { get; set; }
}
public class SmsNotification : Notification
{
public string PhoneNumber { get; set; }
}
```
**JSON Output:**
```json
{
"$type": "EmailNotification",
"message": "Hello",
"emailAddress": "user@example.com"
}
```
## Notes
- **AOT Compatibility**: Metadata-based source generation is supported; fast-path source generation is NOT supported for polymorphic types
- **Serialization requirement**: Must serialize using the base type for polymorphism to work (e.g., `JsonSerializer.Serialize<Notification>(emailNotification)`)
- **Discriminator values**: String discriminators are recommended over integers for readability and forward compatibility
- **Nested hierarchies**: Each level in the hierarchy needs its own `[JsonPolymorphic]` attribute if it has derived types
- **Interface support**: Interfaces can also use `[JsonPolymorphic]` when they define the contract for serialization
## Documentation
- [System.Text.Json Polymorphism](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism)
Versions
The database has no other skills named dotnet-json-polymorphic.
Database snapshot: 13.11.2026 · package version e8910b6f142a (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100dotnet-source-gen-json
Configures System.Text.Json source generation for AOT-compatible JSON serialization. Also use when the user mentions "JSON source generator," "JsonSerializerContext," "JsonSerializable," "AOT JSON," "reflection-free JSON," or "System.Text.Json source gen." For polymorphic types,… - 100dotnet-aot-analysis
Analyzes and configures a .NET project or solution for Native AOT compatibility. Orchestrates source generator skills and applies AOT settings. Also use when the user mentions "AOT," "native AOT," "trimming," "publish AOT," "AOT compatibility," or "ahead-of-time compilation."… - 100unit-test-json-serialization
Provides patterns for unit testing JSON serialization/deserialization with Jackson and `@JsonTest`. Validates JSON mapping, custom serializers, date formats, and polymorphic types. Use when testing JSON serialization, validating custom serializers, or writing JSON unit tests in… - 100flutter-implement-json-serialization
Use when writing manual JSON serializers with dart:convert and Dart pattern matching or automating model serialization. - 100refactoring-conditionals-polymorphism-strategy
Use this skill when replace nested conditional logic and switches with polymorphic Strategy and Command patterns. - 100flutter-handling-http-and-json
Executes HTTP requests and handles JSON serialization in a Flutter app. Use when integrating with REST APIs or parsing structured data from external sources.