Agent skill
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.
Install this agent skill to your Project
npx add-skill https://github.com/Im5tu/claude/tree/main/skills/dotnet-json-polymorphic
Metadata
Additional technical details for this skill
- author
- Im5tu
- version
- 1.0
- repositoryUrl
- https://github.com/im5tu/dotnet-skills
SKILL.md
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
TypeNameHandlingwith modern attributes
Requirements
- .NET 7 or higher
Steps
-
Ask scope:
- Ask user: "Apply to entire solution or specific project?"
- If specific project, ask which project
-
Search for existing
[JsonPolymorphic]attributes:- Detect existing project conventions
- Note any
TypeDiscriminatorPropertyNamevalues found
-
Extract discriminator conventions:
- Search for
TypeDiscriminatorPropertyNamein existing attributes - Record all unique discriminator property names found
- Search for
-
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
-
Ask about discriminator property name:
- If existing discriminators found, show them to user
- Ask: "What discriminator property name? (Default:
$type, Found: [list existing])"
-
Check for consistency:
- If multiple different discriminators found in codebase
- Ask: "Found multiple discriminators: [list]. Standardize to single value?"
-
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")
- Use full type name as discriminator value (e.g.,
-
Add using directive:
- Ensure
using System.Text.Json.Serialization;is present
- Ensure
-
Verify with build:
bashdotnet build -
Report results:
- List all base types configured
- List all derived type mappings added
- Confirm build status
Example Conversion
Before:
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:
[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:
{
"$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
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
flutter-animations
Comprehensive guide for implementing animations in Flutter. Use when adding motion and visual effects to Flutter apps: implicit animations (AnimatedContainer, AnimatedOpacity, TweenAnimationBuilder), explicit animations (AnimationController, Tween, AnimatedWidget/AnimatedBuilder), hero animations (shared element transitions), staggered animations (sequential/overlapping), and physics-based animations. Includes workflow for choosing the right animation type, implementation patterns, and best practices for performance and user experience.
dotnet-source-gen-logging
Converts logging to use the LoggerMessage source generator for high-performance, AOT-compatible logging. Also use when the user mentions "LoggerMessage," "logging source generator," "high-performance logging," "optimize logging," "AOT logging," or "structured logging source gen." For full AOT analysis, see dotnet-aot-analysis.
dotnet-source-gen-options-validation
Converts options validation to use the compile-time source generator for AOT-compatible, reflection-free validation. Also use when the user mentions "OptionsValidator," "options validation source gen," "AOT options validation," "compile-time validation," "ValidateDataAnnotations replacement," or "reflection-free validation." For full AOT analysis, see dotnet-aot-analysis.
dotnet-enable-testing-platform
Enables the Microsoft Testing Platform runner in global.json. Also use when the user mentions "testing platform," "Microsoft.Testing.Platform," "new test runner," "migrate test runner," "global.json testing," or "modern test platform."
competitor-alternatives
When the user wants to create competitor comparison or alternative pages for SEO and sales enablement. Also use when the user mentions 'alternative page,' 'vs page,' 'competitor comparison,' 'comparison page,' '[Product] vs [Product],' '[Product] alternative,' or 'competitive landing pages.' Covers four formats: singular alternative, plural alternatives, you vs competitor, and competitor vs competitor. Emphasizes deep research, modular content architecture, and varied section types beyond feature tables.
flutter-duit-bdui
Integrate Duit framework into Flutter applications including setup, driver configuration, HTTP/WebSocket transports, custom widgets, and themes. Use when integrating backend-driven UI, configuring Duit, or adding Duit to Flutter applications.
Didn't find tool you were looking for?