It's somehow difficult to find real examples showing how to do this. That's why I've decided to make this little tutorial to help you get the idea quickly with practical code examples.
Polymorphism is the ability to have different implementations represented by a single interface or abstract class. This article describes how to serialize and deserialize objects by their interface, as well as Polymorphic Tree Structured object instances.
Please note this example is written in Java 8 and uses Lombok. Lombok has numerous benefits like generating getters, toString, equals and hashcode methods.
In order to be able to serialize / deserialize the Vehicles instance, Jackson Json needs to know how to instantiate a Vehicle instance. It needs to know whenever it's a Truck or a Car instance. In order to do this, Jackson Json must be configured. There are several ways to do so.
This solution works well when the interface and the implementation are placed within the same package. However, it introduces a cyclic dependency between the Vehicle interface and its implementations.
Jackson has added a @type attribute to each vehicle json. This special attribute is used to identify the type of vehicle being serialized. Jackson then uses this information during deserialization to create the right class instance.
A CarTransporter is a Vehicle itself! But, it's also able to carry Vehicle instances. In fact, it could technically carry another CarTransporter although a real one wouldn't be able too... It's not the point here.
As you can see, I've added the CarTransporter type mapping. The unit-test above serializes and deserializes a CarTransporter instance. The produced json looks like the following:
I hope you see now that there is nothing difficult here! Jackson can serialize and deserialize polymorphic data structures very easily. The CarTransporter can itself carry another CarTransporter as a vehicle: that's where the tree structure is!
Now, you know how to configure Jackson to serialize and deserialize objects being represented by their interface.