Skip to content

Commit 6cc8cd5

Browse files
committed
JAVA-6303: Update "Jackson Inheritance" article
1 parent 052d7e1 commit 6cc8cd5

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package com.baeldung.jackson.inheritance;
22

3+
import static org.hamcrest.CoreMatchers.instanceOf;
34
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
46

5-
import org.junit.Test;
6-
7-
import java.util.List;
8-
import java.util.ArrayList;
97
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
1010

11+
import org.junit.Test;
12+
13+
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Car;
14+
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Fleet;
15+
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Truck;
16+
import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Vehicle;
1117
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
19+
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
1220

1321
public class SubTypeHandlingUnitTest {
1422
@Test
@@ -23,21 +31,30 @@ public void givenSubTypes_whenConvertingObjects_thenDataValuesArePreserved() {
2331
}
2432

2533
@Test
26-
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
34+
public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException {
2735
ObjectMapper mapper = new ObjectMapper();
28-
mapper.enableDefaultTyping();
29-
30-
SubTypeConstructorStructure.Car car = new SubTypeConstructorStructure.Car("Mercedes-Benz", "S500", 5, 250.0);
31-
SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0);
32-
33-
List<SubTypeConstructorStructure.Vehicle> vehicles = new ArrayList<>();
36+
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
37+
.allowIfSubType("com.baeldung.jackson.inheritance")
38+
.allowIfSubType("java.util.ArrayList")
39+
.build();
40+
mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
41+
42+
Car car = new Car("Mercedes-Benz", "S500", 5, 250.0);
43+
Truck truck = new Truck("Isuzu", "NQR", 7500.0);
44+
45+
List<Vehicle> vehicles = new ArrayList<>();
3446
vehicles.add(car);
3547
vehicles.add(truck);
3648

37-
SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet();
49+
Fleet serializedFleet = new Fleet();
3850
serializedFleet.setVehicles(vehicles);
3951

4052
String jsonDataString = mapper.writeValueAsString(serializedFleet);
41-
mapper.readValue(jsonDataString, SubTypeConstructorStructure.Fleet.class);
53+
mapper.readValue(jsonDataString, Fleet.class);
54+
55+
Fleet deserializedFleet = mapper.readValue(jsonDataString, Fleet.class);
56+
57+
assertThat(deserializedFleet.getVehicles().get(0), instanceOf(Car.class));
58+
assertThat(deserializedFleet.getVehicles().get(1), instanceOf(Truck.class));
4259
}
4360
}

jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
import java.io.IOException;
1111

1212
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
14+
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
1315

1416
public class TypeInfoInclusionUnitTest {
1517
@Test
1618
public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException {
1719
ObjectMapper mapper = new ObjectMapper();
18-
mapper.enableDefaultTyping();
20+
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
21+
.allowIfSubType("com.baeldung.jackson.inheritance")
22+
.allowIfSubType("java.util.ArrayList")
23+
.build();
24+
mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
1925

2026
TypeInfoStructure.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0);
2127
TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0);

0 commit comments

Comments
 (0)