[BAEL-6750] - Jackson Polymorphic Deserialization and SubTypes registration using Reflection API (#14503)

* [BAEL-6750] - Jackson Polymorphic Deserialization and SubTypes registration using Reflection API

* Renamed test files

---------

Co-authored-by: ovidiu-mihai98 <57233636+ovidiu-mihai98@users.noreply.github.com>
This commit is contained in:
ovidiumihaitacu 2023-07-31 20:19:16 +03:00 committed by GitHub
parent bb967e0836
commit c8bca3abc1
7 changed files with 233 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jackson-polymorphic-deserialization</artifactId>
<name>jackson-polymorphic-deserialization</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>jackson-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-core.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
</dependencies>
<build>
<finalName>jackson-polymorphic-deserialization</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<jackson-core.version>2.15.2</jackson-core.version>
<reflections.version>0.9.11</reflections.version>
</properties>
</project>

View File

@ -0,0 +1,47 @@
package com.baeldung.jackson.polymorphic.deserialization.reflection;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
public class Vehicle {
public String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@VehicleSubType("ELECTRIC_VEHICLE")
public static class ElectricVehicle extends Vehicle {
String autonomy;
String chargingTime;
public String getAutonomy() {
return autonomy;
}
public void setAutonomy(String autonomy) {
this.autonomy = autonomy;
}
public String getChargingTime() {
return chargingTime;
}
public void setChargingTime(String chargingTime) {
this.chargingTime = chargingTime;
}
}
@VehicleSubType("FUEL_VEHICLE")
public static class FuelVehicle extends Vehicle {
String fuelType;
String transmissionType;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.jackson.polymorphic.deserialization.reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface VehicleSubType {
String value();
}

View File

@ -0,0 +1,66 @@
package com.baeldung.jackson.polymorphic.deserialization.typeHandlingAnnotations;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Vehicle.ElectricVehicle.class, name = "ELECTRIC_VEHICLE"),
@JsonSubTypes.Type(value = Vehicle.FuelVehicle.class, name = "FUEL_VEHICLE")
})
public class Vehicle {
public String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public static class ElectricVehicle extends Vehicle {
String autonomy;
String chargingTime;
public String getAutonomy() {
return autonomy;
}
public void setAutonomy(String autonomy) {
this.autonomy = autonomy;
}
public String getChargingTime() {
return chargingTime;
}
public void setChargingTime(String chargingTime) {
this.chargingTime = chargingTime;
}
}
public static class FuelVehicle extends Vehicle {
String fuelType;
String transmissionType;
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public String getTransmissionType() {
return transmissionType;
}
public void setTransmissionType(String transmissionType) {
this.transmissionType = transmissionType;
}
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.jackson.polymorphic.deserialization.reflection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
public class ReflectionUnitTest {
@Test
public void whenDeserializingPolymorphic_thenCorrect() throws JsonProcessingException {
String json = "{\"type\":\"ELECTRIC_VEHICLE\",\"autonomy\":\"500\",\"chargingTime\":\"200\"}";
ObjectMapper objectMapper = getCustomObjectMapper();
Vehicle vehicle = objectMapper.readValue(json, Vehicle.class);
assertEquals(Vehicle.ElectricVehicle.class, vehicle.getClass());
}
private ObjectMapper getCustomObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
Reflections reflections = new Reflections("com.baeldung.jackson.polymorphic.deserialization.reflection");
Set<Class<?>> subtypes = reflections.getTypesAnnotatedWith(VehicleSubType.class);
for (Class<?> subType : subtypes) {
VehicleSubType annotation = subType.getAnnotation(VehicleSubType.class);
if (annotation != null) {
String typeName = annotation.value();
objectMapper.registerSubtypes(new NamedType(subType, typeName));
}
}
return objectMapper;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.jackson.polymorphic.deserialization.typeHandlingAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TypeHandlingAnnotationsUnitTest {
@Test
public void whenDeserializingPolymorphic_thenCorrect() throws JsonProcessingException {
String json = "{\"type\":\"ELECTRIC_VEHICLE\",\"autonomy\":\"500\",\"chargingTime\":\"200\"}";
Vehicle vehicle = new ObjectMapper().readerFor(Vehicle.class).readValue(json);
assertEquals(Vehicle.ElectricVehicle.class, vehicle.getClass());
}
}

View File

@ -22,6 +22,7 @@
<module>jackson-custom-conversions</module>
<module>jackson-exceptions</module>
<module>jackson-jr</module>
<module>jackson-polymorphic-deserialization</module>
</modules>
<dependencies>