From 1d71f63693cfad50cf672a0762de1b8193149ce6 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 19 Jan 2017 08:48:48 +0100 Subject: [PATCH] Bael 19 jackson inheritance (#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 --- .../baeldung/jackson/inheritance/Event.java | 25 +++++++++++ .../inheritance/ItemIdAddedToUser.java | 31 ++++++++++++++ .../inheritance/ItemIdRemovedFromUser.java | 29 +++++++++++++ .../ItemIdRemovedFromUserTest.java | 41 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 jackson/src/main/java/com/baeldung/jackson/inheritance/Event.java create mode 100644 jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdAddedToUser.java create mode 100644 jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUser.java create mode 100644 jackson/src/test/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUserTest.java diff --git a/jackson/src/main/java/com/baeldung/jackson/inheritance/Event.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/Event.java new file mode 100644 index 0000000000..797bde98b2 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/inheritance/Event.java @@ -0,0 +1,25 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeInfo; + +@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "eventType") +abstract public class Event { + private final String id; + private final Long timestamp; + + @JsonCreator + public Event(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp) { + this.id = id; + this.timestamp = timestamp; + } + + public Long getTimestamp() { + return timestamp; + } + + public String getId() { + return id; + } +} \ No newline at end of file diff --git a/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdAddedToUser.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdAddedToUser.java new file mode 100644 index 0000000000..11aa8ddb1a --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdAddedToUser.java @@ -0,0 +1,31 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; + +@JsonTypeName("itemIdAddedToUser") +@JsonIgnoreProperties("id") +public class ItemIdAddedToUser extends Event { + private final String itemId; + private final Long quantity; + + @JsonCreator + public ItemIdAddedToUser(@JsonProperty("id") String id, + @JsonProperty("timestamp") Long timestamp, + @JsonProperty("itemId") String itemId, + @JsonProperty("quantity") Long quantity) { + super(id, timestamp); + this.itemId = itemId; + this.quantity = quantity; + } + + public String getItemId() { + return itemId; + } + + public Long getQuantity() { + return quantity; + } +} \ No newline at end of file diff --git a/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUser.java b/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUser.java new file mode 100644 index 0000000000..682394ddf3 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUser.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; + +@JsonTypeName("itemIdRemovedFromUser") +public class ItemIdRemovedFromUser extends Event { + private final String itemId; + private final Long quantity; + + @JsonCreator + public ItemIdRemovedFromUser(@JsonProperty("id") String id, + @JsonProperty("timestamp") Long timestamp, + @JsonProperty("itemId") String itemId, + @JsonProperty("quantity") Long quantity) { + super(id, timestamp); + this.itemId = itemId; + this.quantity = quantity; + } + + public String getItemId() { + return itemId; + } + + public Long getQuantity() { + return quantity; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUserTest.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUserTest.java new file mode 100644 index 0000000000..5aee03555e --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/ItemIdRemovedFromUserTest.java @@ -0,0 +1,41 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ItemIdRemovedFromUserTest { + @Test + public void givenRemoveItemJson_whenDeserialize_shouldHaveProperClassType() throws IOException { + //given + Event event = new ItemIdRemovedFromUser("1", 12345567L, "item_1", 2L); + ObjectMapper objectMapper = new ObjectMapper(); + String eventJson = objectMapper.writeValueAsString(event); + + //when + Event result = new ObjectMapper().readValue(eventJson, Event.class); + + //then + assertTrue(result instanceof ItemIdRemovedFromUser); + assertEquals("item_1", ((ItemIdRemovedFromUser) result).getItemId()); + } + + @Test + public void givenAdddItemJson_whenSerialize_shouldIgnoreIdPropertyFromSuperclass() throws IOException { + //given + Event event = new ItemIdAddedToUser("1", 12345567L, "item_1", 2L); + ObjectMapper objectMapper = new ObjectMapper(); + + //when + String eventJson = objectMapper.writeValueAsString(event); + + //then + assertFalse(eventJson.contains("id")); + } + +} \ No newline at end of file