[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:
+47
@@ -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;
|
||||
}
|
||||
}
|
||||
+13
@@ -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();
|
||||
}
|
||||
+66
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
@@ -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;
|
||||
}
|
||||
}
|
||||
+20
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user