JsonSubTypes
JsonSubTypes copied to clipboard
Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch
Before the fix https://github.com/manuc66/JsonSubTypes/issues/100, we could use the order to attempt best resolution.
The following json {"CommonProp": null, "Optional": null, "Other": null} raises "Ambiguous type resolution" while we would expect ClassC with former algo (or eventually ClassB if fallback could be used for ambiguous cases / but we really prefer former algo to deal with complex class trees).
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(ClassC), nameof(ClassC.Other))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(ClassB), nameof(ClassB.Optional))]
[JsonSubtypes.FallBackSubType(typeof(ClassB))]
public class ClassA
{
public string CommonProp { get; set; }
}
public class ClassB : ClassA
{
public bool? Optional { get; set; }
}
public class ClassC : ClassB
{
public string Other { get; set; }
}
@mulder999
Could this match your expectations ?
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(ClassC), nameof(ClassC.Other), stopLookupOnMath: true)]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(ClassB), nameof(ClassB.Optional))]
[JsonSubtypes.FallBackSubType(typeof(ClassB))]
public class ClassA
{
public string CommonProp { get; set; }
}
``
@manuc66 Thanks for coming back.
I guess there is a minor typo stopLookupOnMatch beside that, I believe this would do, assuming we can add it on several attributes.