### Configure Fallback Subtype and Registration (Property Name Mismatch) Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Configure subtype registration with a fallback and register known subtypes. Note that this example uses 'Value' as the property name for 'ConstantExpression', which might differ from the 'Type' property used for other subtypes. ```csharp settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IExpression)) .SetFallbackSubtype(typeof(UnknownExpression)) .RegisterSubtype(typeof(ConstantExpression), "Value") .Build()); ``` -------------------------------- ### Configure Fallback Subtype and Registration (Type) Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Programmatically configure the fallback subtype and register known subtypes for an interface or base class. This method uses the type of the base class and the property name for type identification. ```csharp settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IExpression), "Type") .SetFallbackSubtype(typeof(UnknownExpression)) .RegisterSubtype(typeof(ConstantExpression), "Constant") .Build()); ``` -------------------------------- ### Register Subtypes with Property Mapping (Generics) Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Configure subtype registration programmatically using generic types for the base class and subtypes, along with the distinguishing property name. ```csharp settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of() .RegisterSubtypeWithProperty("JobTitle") .RegisterSubtypeWithProperty("Skill") .Build()); ``` -------------------------------- ### Register Subtypes with Property Mapping (Type) Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Configure subtype registration programmatically using the type of the base class and the property name that distinguishes subtypes. ```csharp settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(Person)) .RegisterSubtypeWithProperty(typeof(Employee), "JobTitle") .RegisterSubtypeWithProperty(typeof(Artist), "Skill") .Build()); ``` -------------------------------- ### Define Default Class with Attributes Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Use attributes to define a fallback subtype for cases where the JSON does not match any known subtypes. This is useful for handling unknown or unexpected types. ```csharp [JsonConverter(typeof(JsonSubtypes))] [JsonSubtypes.KnownSubType(typeof(ConstantExpression), "Constant")] [JsonSubtypes.FallBackSubType(typeof(UnknownExpression))] public interface IExpression { string Type { get; } } ``` -------------------------------- ### Deserialize by Property Presence with Attributes Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Use attributes to map subtypes based on the presence of specific properties. This is useful when the property name indicates the subtype. ```csharp [JsonConverter(typeof(JsonSubtypes))] [JsonSubtypes.KnownSubTypeWithProperty(typeof(Employee), "JobTitle")] [JsonSubtypes.KnownSubTypeWithProperty(typeof(Artist), "Skill")] public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class Employee : Person { public string Department { get; set; } public string JobTitle { get; set; } } public class Artist : Person { public string Skill { get; set; } } ``` -------------------------------- ### Deserialize by Property Presence with Generic Syntax Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Deserialize a collection of polymorphic objects using generic syntax. Ensure the target type is an interface or base class that the subtypes implement or inherit from. ```csharp string json = "[{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"},"+ "[{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"},"+ "[{\"Skill\":\"Painter\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}]"; var persons = JsonConvert.DeserializeObject>(json); Assert.AreEqual("Painter", (persons.Last() as Artist)?.Skill); ``` -------------------------------- ### Serialize/Deserialize with Type Property Only in JSON Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Configure JsonSubTypes through JsonSerializerSettings to handle type properties not defined in attributes. This requires explicit registration of sub-types and their discriminators. ```csharp public abstract class Animal { public int Age { get; set; } } public class Dog : Animal { public bool CanBark { get; set; } = true; } public class Cat : Animal { public int Lives { get; set; } = 7; } public enum AnimalType { Dog = 1, Cat = 2 } ``` ```csharp var settings = new JsonSerializerSettings(); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Animal), "Type") // type property is only defined here .RegisterSubtype(typeof(Cat), AnimalType.Cat) .RegisterSubtype(typeof(Dog), AnimalType.Dog) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); ``` ```csharp var settings = new JsonSerializerSettings(); settings.Converters.Add(JsonSubtypesConverterBuilder .Of("Type") // type property is only defined here .RegisterSubtype(AnimalType.Cat) .RegisterSubtype(AnimalType.Dog) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); ``` ```csharp var cat = new Cat { Age = 11, Lives = 6 } var json = JsonConvert.SerializeObject(cat, settings); Assert.Equal("{\"Lives\":6,\"Age\":11,\"Type\":2}", json); var result = JsonConvert.DeserializeObject(json, settings); Assert.Equal(typeof(Cat), result.GetType()); Assert.Equal(11, result.Age); Assert.Equal(6, (result as Cat)?.Lives); ``` -------------------------------- ### DeserializeObject with Custom Type Mapping Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Utilize JsonSubtypes.KnownSubType attributes to map specific values to sub-types when deserializing. This method supports various value types for mapping, not just strings. ```csharp [JsonConverter(typeof(JsonSubtypes), "Sound")] [JsonSubtypes.KnownSubType(typeof(Dog), "Bark")] [JsonSubtypes.KnownSubType(typeof(Cat), "Meow")] public class Animal { public virtual string Sound { get; } public string Color { get; set; } } public class Dog : Animal { public override string Sound { get; } = "Bark"; public string Breed { get; set; } } public class Cat : Animal { public override string Sound { get; } = "Meow"; public bool Declawed { get; set{}; } ``` ```csharp var animal = JsonConvert.DeserializeObject("{\"Sound\":\"Bark\",\"Breed\":\"Jack Russell Terrier\"}"); Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed); ``` -------------------------------- ### DeserializeObject with Custom Type Property Name Source: https://github.com/manuc66/jsonsubtypes/blob/master/README.md Use the JsonConverter attribute with the custom type property name to deserialize JSON into an interface. Ensure types are in the same assembly and namespace or fully qualified. ```csharp [JsonConverter(typeof(JsonSubtypes), "Kind")] public interface IAnimal { string Kind { get; } } public class Dog : IAnimal { public string Kind { get; } = "Dog"; public string Breed { get; set; } } public class Cat : IAnimal { public string Kind { get; } = "Cat"; public bool Declawed { get; set{}; } ``` ```csharp var animal = JsonConvert.DeserializeObject("{\"Kind\":\"Dog\",\"Breed\":\"Jack Russell Terrier\"}"); Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.