question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Be able to support custom inheritance discriminator field

See original GitHub issue

Source 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

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:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

39reactions
JamesNKcommented, Jun 11, 2017

Because you have to draw a line at what point you stop allow customization. I have decided to draw the line here.

8reactions
sfmskywalkercommented, Jan 18, 2021

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? 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found