Can Jackson polymorphic deserialization be used to serialize to a subtype if a specific field is present?

As of Jackson 2.12.2, the following accomplishes the goal using the “deduction-based polymorphism” feature. If properties distinct to the Bird subtype (i.e. wingspan) are present, the deserialized type will be Bird; else it will be Animal: @JsonTypeInfo(use=Id.DEDUCTION, defaultImpl = Animal.class) @JsonSubTypes({@Type(Bird.class)}) public class Animal { public String name; public int age; } Deduction-based polymorphism The …

Read more

Whats the use of saying

List<SomeObject> l; In this you cannot say List<SomeObject> l = new ArrayList<SubClassOfSomeObjectClass>;(not allowed) wheres for List<? extends SomeObject> l; you can say List<? extends SomeObject> l = new ArrayList<SubClassOfSomeObject>;(allowed) But note that in List<? extends SomeObject> l = new ArrayList<SubClassOfSomeObject>; you cannot add anything to your list l because ? represents unknown class (Except null …

Read more

Possible to have “polymorphic has_one” relationship in rails?

I don’t believe you’re in need of a has_one association here, the belongs_to should be what you’re looking for. In this case, you’d want a target_id and target_type column on your Campaign table, you can create these in a rake with a t.references :target call (where t is the table variable). class Campaign < ActiveRecord::Base …

Read more

Method accepting two different types as parameter

How about this: interface ICrushable { void crush(); } utterlyDestroy(ICrushable parameter) { // Very long crushing process goes here parameter.crush() } utterlyDestroy(Dreams parameter) { utterlyDestroy(new ICrushable() { crush() {parameter.crush();}); } utterlyDestroy(Garlic parameter) { utterlyDestroy(new ICrushable() { crush() {parameter.crush();}); } New development should implement the ICrushable interface, but for the existing Classes, the parameter is wrapped …

Read more

Why to use Polymorphism?

In your example, the use of polymorphism isn’t incredibly helpful since you only have one subclass of FlyingMachine. Polymorphism becomes helpful if you have multiple kinds of FlyingMachine. Then you could have a method that accepts any kind of FlyingMachine and uses its fly() method. An example might be testMaxAltitude(FlyingMachine). Another feature that is only …

Read more