Comments (2)
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.
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)
- Support Enums HOT 2
- Misleading error message on deserialization when subtype is unknown HOT 1
- Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch HOT 2
- Incorrect link to license
- New Major to depend on newer Newtonsoft.Json HOT 3
- KnownSubTypeWithProperty it would be great to support aliases HOT 3
- Support (serializing) sub types with generic type parameters when using JsonSubtypesConverterBuilder HOT 7
- MaxDepth not copied over when creating internal JsonReader
- Serialize type discriminator when using attributes too HOT 6
- Use in API calls?
- Example code in readme fails, just completely wrong according to visual studio HOT 1
- JsonSubtypesConverterBuilder SetFallbackSubtype HOT 1
- wrong link in v2.0.1 release notes HOT 1
- JsonReaderException does not contain a constructor that takes 5 arguments HOT 4
- Persistent settings HOT 1
- Base Class Properties not being Deserialized HOT 2
- Outdated version of NewtsonSoft.Json used
- deserializing dates with jsonsubtypes does not match same result values as stock newtonsoft
- Error on deserialization "Could not find member ... on type" when MissingMemberHandling=error and there is no discriminator field on the dto HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonsubtypes.