Merge pull request #11194 from freelansam/JAVA-6303
JAVA-6303: Update "Jackson Inheritance" article
This commit is contained in:
commit
e9d5975e3c
|
@ -1,14 +1,22 @@
|
||||||
package com.baeldung.jackson.inheritance;
|
package com.baeldung.jackson.inheritance;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
import static org.junit.Assert.assertEquals;
|
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 org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Car;
|
||||||
import java.util.ArrayList;
|
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Fleet;
|
||||||
import java.io.IOException;
|
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.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
||||||
|
|
||||||
public class SubTypeHandlingUnitTest {
|
public class SubTypeHandlingUnitTest {
|
||||||
@Test
|
@Test
|
||||||
|
@ -23,21 +31,30 @@ public class SubTypeHandlingUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
|
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
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);
|
List<Vehicle> vehicles = new ArrayList<>();
|
||||||
SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0);
|
|
||||||
|
|
||||||
List<SubTypeConstructorStructure.Vehicle> vehicles = new ArrayList<>();
|
|
||||||
vehicles.add(car);
|
vehicles.add(car);
|
||||||
vehicles.add(truck);
|
vehicles.add(truck);
|
||||||
|
|
||||||
SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet();
|
Fleet serializedFleet = new Fleet();
|
||||||
serializedFleet.setVehicles(vehicles);
|
serializedFleet.setVehicles(vehicles);
|
||||||
|
|
||||||
String jsonDataString = mapper.writeValueAsString(serializedFleet);
|
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 java.io.IOException;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
||||||
|
|
||||||
public class TypeInfoInclusionUnitTest {
|
public class TypeInfoInclusionUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException {
|
public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
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.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0);
|
||||||
TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0);
|
TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0);
|
||||||
|
|
Loading…
Reference in New Issue