把 Jackson 的所有有关文章移动到类库模块中

This commit is contained in:
2024-05-05 12:57:54 -04:00
parent 26215fd7af
commit f3f528cf1c
393 changed files with 144 additions and 144 deletions
@@ -0,0 +1,2 @@
## 相关文章
- [Jackson 中的 @JsonSubTypes 对比多态反射反序列化](https://track.ossez.com/articles/PL-A-37491939/JsonSubTypes-vs.-Reflections-for-Polymorphic-Deserialization-in-Jackson)
@@ -0,0 +1,42 @@
<?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.ossez</groupId>
<artifactId>libraries-jackson</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.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>
<reflections.version>0.9.11</reflections.version>
</properties>
</project>
@@ -0,0 +1,47 @@
package com.ossez.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.ossez.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.ossez.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.ossez.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.ossez.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());
}
}