-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
JsonPolymorphic Deserialization Fails When TypeDiscriminatorPropertyName Not First Property #78447
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionI am attempting to deserialize external json from a service which is structured in a way that I can take advantage of Is there a work around? I assume this is for performance to only look ahead for the first property, but I believe it is common to not have control over the ordering of the json. I couldn't find any documentation about this limitation either. Reproduction Stepsusing System.Text.Json;
using System.Text.Json.Serialization;
public class PolymorphicJsonExampleUpper
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerOptions.Default);
[JsonPolymorphic(TypeDiscriminatorPropertyName = "Type")]
[JsonDerivedType(typeof(TypeA), "a")]
[JsonDerivedType(typeof(TypeB), "b")]
private abstract record Base(string Type);
private sealed record TypeA(string Type, int ValueA) : Base(Type);
[Test]
public void FromJsonA()
{
const string jsonA = """{"Type":"a","ValueA":1}""";
var valueA = JsonSerializer.Deserialize<Base>(jsonA, JsonSerializerOptions);
Assert.That(valueA, Is.TypeOf<TypeA>());
}
[Test]
public void FromJsonA_Revered()
{
const string jsonA = """{"ValueA":1,"Type":"a"}""";
var valueA = JsonSerializer.Deserialize<Base>(jsonA, JsonSerializerOptions);
Assert.That(valueA, Is.TypeOf<TypeA>());
}
} Expected behaviorNo exceptions thrown during test Actual behaviorException thrown:
Regression?No response Known WorkaroundsNo response Configuration
Other informationNo response
|
Duplicate of #72604. |
Description
I am attempting to deserialize external json from a service which is structured in a way that I can take advantage of
JsonPolymorphic
attribute, however it appears that it needs the type discriminator as the first property in the json object. I don't have control over this external source of json to adjust it, and many libraries treat json objects as an unordered collection.Is there a work around? I assume this is for performance to only look ahead for the first property, but I believe it is common to not have control over the ordering of the json. I couldn't find any documentation about this limitation either.
Reproduction Steps
Expected behavior
No exceptions thrown during test
PolymorphicJsonExample.FromJsonA_Reverse()
and it successfully passes.Actual behavior
Exception thrown:
Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: