JAVA-6303: Update "Jackson Inheritance" article
This commit is contained in:
parent
56a9917613
commit
0595a35062
|
@ -1,14 +1,22 @@
|
|||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Car;
|
||||
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Fleet;
|
||||
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Truck;
|
||||
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Vehicle;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
||||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
||||
|
||||
public class SubTypeHandlingUnitTest {
|
||||
@Test
|
||||
|
@ -23,21 +31,30 @@ public class SubTypeHandlingUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
|
||||
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enableDefaultTyping();
|
||||
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
|
||||
.allowIfSubType("com.baeldung.jackson.inheritance")
|
||||
.allowIfSubType("java.util.ArrayList")
|
||||
.build();
|
||||
mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
Car car = new Car("Mercedes-Benz", "S500", 5, 250.0);
|
||||
Truck truck = new Truck("Isuzu", "NQR", 7500.0);
|
||||
|
||||
SubTypeConstructorStructure.Car car = new SubTypeConstructorStructure.Car("Mercedes-Benz", "S500", 5, 250.0);
|
||||
SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0);
|
||||
|
||||
List<SubTypeConstructorStructure.Vehicle> vehicles = new ArrayList<>();
|
||||
List<Vehicle> vehicles = new ArrayList<>();
|
||||
vehicles.add(car);
|
||||
vehicles.add(truck);
|
||||
|
||||
SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet();
|
||||
Fleet serializedFleet = new Fleet();
|
||||
serializedFleet.setVehicles(vehicles);
|
||||
|
||||
String jsonDataString = mapper.writeValueAsString(serializedFleet);
|
||||
mapper.readValue(jsonDataString, SubTypeConstructorStructure.Fleet.class);
|
||||
mapper.readValue(jsonDataString, Fleet.class);
|
||||
|
||||
Fleet deserializedFleet = mapper.readValue(jsonDataString, Fleet.class);
|
||||
|
||||
assertThat(deserializedFleet.getVehicles().get(0), instanceOf(Car.class));
|
||||
assertThat(deserializedFleet.getVehicles().get(1), instanceOf(Truck.class));
|
||||
}
|
||||
}
|
|
@ -10,12 +10,18 @@ import java.util.ArrayList;
|
|||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
||||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
||||
|
||||
public class TypeInfoInclusionUnitTest {
|
||||
@Test
|
||||
public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enableDefaultTyping();
|
||||
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
|
||||
.allowIfSubType("com.baeldung.jackson.inheritance")
|
||||
.allowIfSubType("java.util.ArrayList")
|
||||
.build();
|
||||
mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
TypeInfoStructure.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0);
|
||||
TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0);
|
||||
|
|
Loading…
Reference in New Issue