Be able to support custom inheritance discriminator field
See original GitHub issueSource JSON
{
"pets": [
{
"className": "Cat",
"color": "white",
"breed": "Aidi"
},
{
"className": "Dog",
"color": "white",
"declawed": false
}
]
}
destination types
class Root {
List<Annimal> pets { get; set;}
}
class Annimal {
string ClassName { get; set;} // not mandatory
string Color { get; set;}
}
class Dog : Annimal {
string breed { get; set;}
}
class Cat : Annimal {
bool declawed { get; set;}
}
Expected behavior
Be able to serialize the given json with few elegant configuration properties. Concretely if we were able to configure by some way the JsonTypeReflector.TypePropertyName
per inheritance hierarchy it might help us.
This would be very helpful to support the
- swagger discriminator feature explained here: http://swagger.io/specification/#models-with-polymorphism-support-91
- json-ld typed values that use
@type
: https://json-ld.org/spec/ED/json-ld-syntax/20120522/#typed-values
Actual behavior
Actually we are forced to implement some sub-optimal/complex solutions, see: https://stackoverflow.com/questions/9490345/json-net-change-type-field-to-another-name
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:22 (1 by maintainers)
Top Results From Across the Web
Code First: Avoid discriminator column and keep inheritance
I want to get rid of the Discriminator field as I do not need the table BaseEntity to be created nor I need...
Read more >Hibernate Inheritance Mapping
A practical guide to understanding different inheritance mapping strategies with JPA / Hibernate.
Read more >Inheritance - EF Core
Table-per-hierarchy and discriminator configuration. By default, EF maps the inheritance using the table-per-hierarchy (TPH) pattern. TPH uses ...
Read more >Inheritance and Polymorphism
The discriminator keyword can be used by various API consumers. One possible example are code generation tools: they can use discriminator to generate...
Read more >Inheritance With Custom Discriminator Field
Short version: I want to do the equivalent of this in Morphia: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Because you have to draw a line at what point you stop allow customization. I have decided to draw the line here.
Another issue with the choice of the
$type
value is that it conflicts when storing a model to MongoDB, since its a MongoDB reserved name.Although it’s possible to write a custom converter as seen in this thread, it sure would be nice to be able to change the name value without having to write a custom converter.
I do understand @JamesNK 's point that you need to draw a line somewhere, but perhaps that line can be reconsidered? 😃