minor jackson testing and cleanup work

This commit is contained in:
eugenp 2013-12-22 14:44:43 +02:00
parent 502e405fc7
commit 9d954b0f42
2 changed files with 14 additions and 18 deletions

View File

@ -14,7 +14,7 @@
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>15.0</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -11,14 +11,9 @@ import org.baeldung.jackson.ignore.MyDtoIgnoreUnkown;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
public class JacksonDeserializationUnitTest { public class JacksonDeserializationUnitTest {
@ -45,6 +40,7 @@ public class JacksonDeserializationUnitTest {
assertNotNull(readValue); assertNotNull(readValue);
assertThat(readValue.getStringValue(), equalTo("a")); assertThat(readValue.getStringValue(), equalTo("a"));
assertThat(readValue.isBooleanValue(), equalTo(true));
} }
@Test(expected = UnrecognizedPropertyException.class) @Test(expected = UnrecognizedPropertyException.class)
@ -61,8 +57,12 @@ public class JacksonDeserializationUnitTest {
} }
@Test @Test
public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; final String jsonAsString =// @formatter:off
"{\"stringValue\":\"a\"," +
"\"intValue\":1," +
"\"booleanValue\":true," +
"\"stringValue2\":\"something\"}"; // @formatter:on
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@ -75,10 +75,13 @@ public class JacksonDeserializationUnitTest {
} }
@Test @Test
public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; final String jsonAsString =// @formatter:off
"{\"stringValue\":\"a\"," +
"\"intValue\":1," +
"\"booleanValue\":true," +
"\"stringValue2\":\"something\"}"; // @formatter:on
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class); final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class);
@ -86,13 +89,6 @@ public class JacksonDeserializationUnitTest {
assertThat(readValue.getStringValue(), equalTo("a")); assertThat(readValue.getStringValue(), equalTo("a"));
assertThat(readValue.isBooleanValue(), equalTo(true)); assertThat(readValue.isBooleanValue(), equalTo(true));
assertThat(readValue.getIntValue(), equalTo(1)); assertThat(readValue.getIntValue(), equalTo(1));
mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException, JsonProcessingException {
return super.handleUnknownProperty(ctxt, jp, deserializer, beanOrClass, propertyName);
}
});
} }
} }