Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-string discriminators using System.Text.Json #1724

Conversation

david-brink-talogy
Copy link
Contributor

System.Text.Json supports non-string discriminators using JsonDerivedType. This PR stringifies the discriminator value.

Fixes #1723

From https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-8-0#mix-and-match-type-discriminator-formats

[JsonDerivedType(typeof(ThreeDimensionalPoint), typeDiscriminator: 3)]
[JsonDerivedType(typeof(FourDimensionalPoint), typeDiscriminator: "4d")]
public class BasePoint
{
    public int X { get; set; }
    public int Y { get; set; }
}

public class ThreeDimensionalPoint : BasePoint
{
    public int Z { get; set; }
}

public sealed class FourDimensionalPoint : ThreeDimensionalPoint
{
    public int W { get; set; }
}

@RicoSuter RicoSuter merged commit 590632a into RicoSuter:master Nov 19, 2024
2 checks passed
@david-brink-talogy david-brink-talogy deleted the features/stj_integer_discriminator branch November 22, 2024 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Text.Json TypeDiscriminator property type is not string
2 participants