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.

Deserialization of a polymorphic type based on presence of a property

See original GitHub issue

I’d like a feature on jackson for deserialization of polymorphic type where type selection strategy should be based on presence of a property. I know this was mentioned before but there was no consensus about how it should be solved.

Example: For jsons like: {'foo':''} {'bar':''}

and type definitions like:

@JsonSubTypes({
        @JsonSubTypes.Type(value=Foo.class, name="foo"),
        @JsonSubTypes.Type(value=Bar.class, name="bar"),
})
interface FooBar {
}
@Value
class Foo {
    private final String foo;
}
@Value
class Bar {
    private final String bar;
}

When I deserialise FooBar with {'foo':''} I should get an instance of a Foo and similar for {'bar':''} I should get an instance of a Bar.

I have a working prototype where you have to specify a new custom deserialiser like:

public class FooBarDeserialiser extends PresentPropertyPolymorphicDeserialiser<FooBar> {

    public FooBarDeserialiser() {
        super(FooBar.class);
    }
}

and this is a valid approach, but a better one, in my opinion, would be if this had first class support for example with JsonTypeInfo:

@JsonTypeInfo(use = JsonTypeInfo.Id.PROPERTY_NAME) // or PROPERTY_NAME_PRESENCE
@JsonSubTypes({
        @JsonSubTypes.Type(value=Foo.class, name="foo"),
        @JsonSubTypes.Type(value=Bar.class, name="bar"),
})
interface FooBar {
}

Thoughts?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:36
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

14reactions
RobertDiebelscommented, Jun 13, 2019

Upvoted. This would be extremely useful when you:

  1. Don’t want to include type information in your JSON.
  2. Don’t want to write a deserializer to handle all this.
7reactions
lpandziccommented, Apr 25, 2018

Yes I agree, but the functionality should be added in some form to the jackson itself, something like @JsonTypeInfo(use=Id.PRESENCE, include=As.PROPERTY) or something like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing polymorphic types with Jackson based on the ...
Here's a solution I've come up with that expands a bit on Erik Gillespie's. It does exactly what you asked for and it...
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson to figure out the right type during deserialization.
Read more >
Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >
How to serialize properties of derived classes with System ...
For polymorphic serialization to work, the type of the serialized value should be that of the polymorphic base type. This includes using the ......
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 have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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