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
This commit is contained in:
Tomasz Lelek 2017-01-19 08:48:48 +01:00 committed by Grzegorz Piwowarek
parent 66449db339
commit 1d71f63693
4 changed files with 126 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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"));
}
}