JsonSubTypes icon indicating copy to clipboard operation
JsonSubTypes copied to clipboard

Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch

Open mulder999 opened this issue 4 years ago • 2 comments

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 avatar Mar 02 '21 16:03 mulder999

@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 avatar Mar 17 '21 21:03 manuc66

@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.

mulder999 avatar Mar 18 '21 11:03 mulder999