Skip to content

Commit 1d71f63

Browse files
tomekl007pivovarit
authored andcommitted
Bael 19 jackson inheritance (eugenp#954)
* BAEL-19 event inheritance * BAEL-19 test for json inheritance * BAEL-19 example event processor * BAEL-19 do not need json ignore * BAEL-19 simpler event, example of abstract * BAEL-19 ignoring superclass property * BAEL-586 change class event names
1 parent 66449db commit 1d71f63

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jackson.inheritance;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
7+
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "eventType")
8+
abstract public class Event {
9+
private final String id;
10+
private final Long timestamp;
11+
12+
@JsonCreator
13+
public Event(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp) {
14+
this.id = id;
15+
this.timestamp = timestamp;
16+
}
17+
18+
public Long getTimestamp() {
19+
return timestamp;
20+
}
21+
22+
public String getId() {
23+
return id;
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.jackson.inheritance;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonTypeName;
7+
8+
@JsonTypeName("itemIdAddedToUser")
9+
@JsonIgnoreProperties("id")
10+
public class ItemIdAddedToUser extends Event {
11+
private final String itemId;
12+
private final Long quantity;
13+
14+
@JsonCreator
15+
public ItemIdAddedToUser(@JsonProperty("id") String id,
16+
@JsonProperty("timestamp") Long timestamp,
17+
@JsonProperty("itemId") String itemId,
18+
@JsonProperty("quantity") Long quantity) {
19+
super(id, timestamp);
20+
this.itemId = itemId;
21+
this.quantity = quantity;
22+
}
23+
24+
public String getItemId() {
25+
return itemId;
26+
}
27+
28+
public Long getQuantity() {
29+
return quantity;
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.jackson.inheritance;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonTypeName;
6+
7+
@JsonTypeName("itemIdRemovedFromUser")
8+
public class ItemIdRemovedFromUser extends Event {
9+
private final String itemId;
10+
private final Long quantity;
11+
12+
@JsonCreator
13+
public ItemIdRemovedFromUser(@JsonProperty("id") String id,
14+
@JsonProperty("timestamp") Long timestamp,
15+
@JsonProperty("itemId") String itemId,
16+
@JsonProperty("quantity") Long quantity) {
17+
super(id, timestamp);
18+
this.itemId = itemId;
19+
this.quantity = quantity;
20+
}
21+
22+
public String getItemId() {
23+
return itemId;
24+
}
25+
26+
public Long getQuantity() {
27+
return quantity;
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.jackson.inheritance;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class ItemIdRemovedFromUserTest {
13+
@Test
14+
public void givenRemoveItemJson_whenDeserialize_shouldHaveProperClassType() throws IOException {
15+
//given
16+
Event event = new ItemIdRemovedFromUser("1", 12345567L, "item_1", 2L);
17+
ObjectMapper objectMapper = new ObjectMapper();
18+
String eventJson = objectMapper.writeValueAsString(event);
19+
20+
//when
21+
Event result = new ObjectMapper().readValue(eventJson, Event.class);
22+
23+
//then
24+
assertTrue(result instanceof ItemIdRemovedFromUser);
25+
assertEquals("item_1", ((ItemIdRemovedFromUser) result).getItemId());
26+
}
27+
28+
@Test
29+
public void givenAdddItemJson_whenSerialize_shouldIgnoreIdPropertyFromSuperclass() throws IOException {
30+
//given
31+
Event event = new ItemIdAddedToUser("1", 12345567L, "item_1", 2L);
32+
ObjectMapper objectMapper = new ObjectMapper();
33+
34+
//when
35+
String eventJson = objectMapper.writeValueAsString(event);
36+
37+
//then
38+
assertFalse(eventJson.contains("id"));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)