Git Product home page Git Product logo

Comments (2)

UtkarshPhilips avatar UtkarshPhilips commented on June 6, 2024

Seems like there is still a workaround. If I remove JsonSubtypesWithPropertyConverterBuilder converter from settings, the deserialization still succeeds in differentiating between Cat and Dog type based on property.

After this, I'm able to successfully deserialize into Cat, BlackDog and WhiteDog classes.

However this could still be a good feature as it can be made a little bit more explicit.

from jsonsubtypes.

manuc66 avatar manuc66 commented on June 6, 2024

Hello @UtkarshPhilips

I can't reproduce the issue: https://dotnetfiddle.net/TdOd4r

Runnable sample: ```csharp using System; using JsonSubTypes; using Newtonsoft.Json;
    public abstract class Animal
    {
        string LegsCount { get; set; }
    }

    public class Dog : Animal
    {
        public virtual DogColors Color { get; }
        public string Breed { get; set; }
    }

// Leaf Class
public class Cat : Animal
{
public bool Declawed { get; set; }
}

    public enum DogColors
    {
        Black,
        White
    }

// Leaf Class
public class BlackDog : Dog
{
public override DogColors Color { get; } = DogColors.Black;
}

// Leaf Class
public class WhiteDog : Dog
{
public override DogColors Color { get; } = DogColors.White;
}

public class Program
{
public static void Main()
{
var settings = new JsonSerializerSettings();

        settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder
            .Of<Animal>()
            .RegisterSubtypeWithProperty<Dog>("Breed")
            .RegisterSubtypeWithProperty<Cat>("Declawed")
            .Build());

        settings.Converters.Add(JsonSubtypesConverterBuilder
            .Of<Dog>("Color")
            .RegisterSubtype<BlackDog>(DogColors.Black)
            .RegisterSubtype<WhiteDog>(DogColors.White)
            .Build());
	

        object result;
        result = JsonConvert.DeserializeObject<Animal>("{\n    \"LegsCount\": \"4\",\n    \"Declawed\": true\n}", settings);
        Console.WriteLine(result.GetType());
        result = JsonConvert.DeserializeObject<Animal>("{\n    \"LegsCount\": \"4\",\n    \"Breed\": \"Doberman\",\n    \"Color\": \"Black\"\n}", settings);
        Console.WriteLine(result.GetType());
        result = JsonConvert.DeserializeObject<Animal>("{\n    \"LegsCount\": \"4\",\n    \"Breed\": \"Husky\",\n    \"Color\": \"White\"\n}", settings);
        Console.WriteLine(result.GetType());
        try
        {
            result = JsonConvert.DeserializeObject<Animal>("{\n    \"LegsCount\": \"4\",\n    \"Breed\": \"Chihuahua\"\n}", settings);
            Console.WriteLine(result.GetType());
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
}

}

</details>

from jsonsubtypes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.