deduction based polymorphism
See original GitHub issueI would like to communicate the need of the deduction based polymorphism feature, that has been recently implemented in jackson. With the help of jackson-module-kotlin it’s possible to de-serialize the following jsons with no need of configuring/transmitting/maintaining additional type information.
@JsonTypeInfo(use=Id.DEDUCTION)
class Shape
data class Polygon(val sides: Int, val sideLength: Int) : Shape
data class Circle(val radius: Int) : Shape
{"sides" : 3, "sideLength" : 10 }
{"radius": 5 }
ObjectMapper mapper = new ObjectMapper().registerModule(new KotlinModule());
mapper.registerModule(KotlinModule());
mapper.readValue(" {\"radius\": 5 } ", Shape.class);
mapper.readValue(" {\"sides\" : 3, \"sideLength\" : 10 } ", Shape.class)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Deduction-Based Polymorphism in Jackson 2.12 - Baeldung
In this tutorial, we'll explore how we can use the deduction-based polymorphism feature from the Jackson library.
Read more >Jackson 2.12 Most Wanted (1/5) - cowtowncoder - Medium
Deduction -Based Polymorphism ... The oldest open feature request jackson-databind#43 — filed in 2012 — was to support polymorphic types that do not...
Read more >Polymorphic subtype deduction based on 'presence ... - GitHub
Polymorphic subtype deduction based on 'presence' of fields does not appear to take 'absence' of fields into account #2976.
Read more >JsonTypeInfo.Id (Jackson-annotations 2.12.0 API) - FasterXML
Means that no serialized typing-property is used. Types are deduced based on the fields available. Deduction is limited to the names of fields...
Read more >Handling Polymorphism in Automated Deduction | SpringerLink
Polymorphism has become a common way of designing short and reusable programs by abstracting generic definitions from type-specific ones.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
This array of sealedSubclasses
you’ve mentioned above is tricky to pass, since there is more than just 1st level of subclasses, there is the whole sealed class hierarchy to pass.
So needed to try out something different and taken look at JsonTransformingSerializer which seems to be a good fit. Suppose we have a Project and the ProjectSerializer.
I’ve noticed that the moment we annotate the Project to use a serializer, the compiler plugin stops generating the serialization code, but only plugs in the specified serializer, which prevents me from providing the correct input for the JsonTransformingSerializer. Please take a look below at the generated code.
I am able to see the workaround. That is that we don’t need to annotate the Project class itself to use the ProjectSerializer, but we can annotate all the referencing properties instead, e.g.
then assuming we use the ProjectWrapper, it works.
However I don’t like this approach of annotating the references instead of the main class. My particular case is that the Project class will always be deserialized in one specified way. It’s hard to ensure that all references to the Project class will be annotated. So this approach is error prone.
I don’t like the kotlinx serialization library being designed the way, that we can bind a class to a serializer, only if the serialize is built from scratch and not when it’s composed from the serializer generated by the compiler plugin. Overall I believe that the kotlinx serialization is a very interesting and inspiring library. Please allow me to share the idea how to extend it with new function.
My Proposition
I imagine that instead of providing KSerializer<T> object, we could extend the functionality of the library to be able to provide a function (KSerializer<T>) -> KSerializer<T> e.g.
that takes the compiler-generated serializer and produces a new one (e.g. based on the JsonTransformingSerializer transformation). So the compiler could still generate the original serializer, but on top of it would also apply the function above.
Could you please tell me what do you think about it?
In Example 19, Internal API is used to provide the correct descriptor. If you do not need a descriptor (it is not used in JSON serialization for now), you can leave the default one.