Use Jackson Core framework to deal with polymorphism type

Use Jackson Core framework to deal with polymorphism type Jackson is a popular Java library for conversion between serialization and desertified Java objects and JSON data.When processing polymorphism, the Jackson Core framework provides some useful functions and technologies. In Java, polymorphism refers to a variable that can quote multiple different types of objects.Consider the following example: abstract class Animal { private String name; // omit other attributes and methods } class Dog extends Animal { private String breed; // omit other attributes and methods } class Cat extends Animal { private boolean isIndoor; // omit other attributes and methods } Now, suppose we have a list containing different types of animal objects: List<Animal> animals = new ArrayList<>(); animals.add(new Dog("Labrador")); animals.add(new Cat(true)); We hope to convert this list into JSON string so that they can transmit or store on the network.To achieve this goal, we need to use the Jackson Core framework. First, we need to introduce the dependency item of the Jackson library in the project.You can add the following code fragments to the pom.xml file of the Maven project: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.4</version> </dependency> Then we can use Jackson's `ObjectMapper` class to perform serialized operations.The following is an example code: import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) { List<Animal> animals = new ArrayList<>(); animals.add(new Dog("Labrador")); animals.add(new Cat(true)); ObjectMapper mapper = new ObjectMapper(); try { String json = mapper.writeValueAsString(animals); System.out.println(json); } catch (JsonProcessingException e) { e.printStackTrace(); } } } The above code serializes the `Animals` list to the JSON string and print it to the console.The output result may be similar to: json [{"breed":"Labrador","name":null},{"isIndoor":true,"name":null}] It can be seen that polymorphic animal objects are correctly converted to corresponding JSON representations.The Jackson Core framework automatically handles the polymorphism field. In the case of deepertization, the Jackson Core framework also provides a convenient method.We can use the `Readvalue` method of the` ObjectMapper` class to convert JSON string back to the Java object.The following is an example code: String json = "[{\"breed\":\"Labrador\",\"name\":null},{\"isIndoor\":true,\"name\":null}]"; try { List<Animal> deserializedAnimals = mapper.readValue(json, new TypeReference<List<Animal>>(){}); for (Animal animal : deserializedAnimals) { System.out.println(animal.getClass().getSimpleName()); } } catch (JsonProcessingException e) { e.printStackTrace(); } The above code turns the JSON fonts to the `List <nimal> object, and print out the name of each animal object.The output result may be similar to: Dog Cat To sum up, using the Jackson Core framework can easily handle polymorphism.It sequences the polymorphic object into the corresponding JSON representation by automatically identifying the actual type of the object, and can correctly serve the JSON string back order to the original Java object.This makes the serialization and device of data more simple and efficient when processing polymorphic types.