BAEL-5849 Code for the improvement of the article Custom deserializer for a generic type jackson

This commit is contained in:
thibault.faure 2022-11-20 17:47:16 +01:00
parent 1c3569f2f3
commit 6746ddc7ea
4 changed files with 109 additions and 13 deletions

View File

@ -0,0 +1,31 @@
package com.baeldung.deserialization;
public class ItemWithWrappedUser {
public int id;
public String itemName;
public Wrapper<User> owner;
public ItemWithWrappedUser() {
super();
}
public ItemWithWrappedUser(final int id, final String itemName, final Wrapper<User> owner) {
this.id = id;
this.itemName = itemName;
this.owner = owner;
}
public int getId() {
return id;
}
public String getItemName() {
return itemName;
}
public Wrapper<User> getOwner() {
return owner;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.deserialization;
public class Wrapper<T> {
T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.deserialization;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
public class WrapperDeserializer extends JsonDeserializer<Wrapper<?>> implements ContextualDeserializer {
private JavaType type;
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
this.type = property.getType()
.containedType(0);
return this;
}
@Override
public Wrapper<?> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
Wrapper<?> wrapper = new Wrapper<>();
wrapper.setValue(deserializationContext.readValue(jsonParser, type));
return wrapper;
}
}

View File

@ -1,23 +1,24 @@
package com.baeldung.deserialization;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class CustomDeserializationUnitTest {
@ -78,4 +79,23 @@ public class CustomDeserializationUnitTest {
assertThat(restored, is(now));
}
@Test
public void whenDeserialisingItemWithWrappedUser_ThenWrappedUserIsCorrectlyParsed() throws JsonProcessingException {
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":{\"id\":2,\"name\":\"theUser\"}}";
SimpleModule module = new SimpleModule().addDeserializer(Wrapper.class, new WrapperDeserializer());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
final ItemWithWrappedUser readValue = objectMapper.readValue(json, ItemWithWrappedUser.class);
assertEquals(2, readValue.getOwner()
.getValue()
.getId());
assertEquals("theUser", readValue.getOwner()
.getValue()
.getName());
}
}