[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
@@ -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;
}
}
@@ -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();
}
@@ -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;
}
}
}
@@ -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;
}
}
@@ -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());
}
}