1111import org .junit .Test ;
1212
1313import com .fasterxml .jackson .core .JsonParseException ;
14- import com .fasterxml .jackson .core .JsonParser ;
15- import com .fasterxml .jackson .core .JsonProcessingException ;
16- import com .fasterxml .jackson .databind .DeserializationContext ;
1714import com .fasterxml .jackson .databind .DeserializationFeature ;
18- import com .fasterxml .jackson .databind .JsonDeserializer ;
1915import com .fasterxml .jackson .databind .JsonMappingException ;
2016import com .fasterxml .jackson .databind .ObjectMapper ;
21- import com .fasterxml .jackson .databind .deser .DeserializationProblemHandler ;
2217import com .fasterxml .jackson .databind .exc .UnrecognizedPropertyException ;
2318
2419public class JacksonDeserializationUnitTest {
@@ -45,6 +40,7 @@ public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToACla
4540
4641 assertNotNull (readValue );
4742 assertThat (readValue .getStringValue (), equalTo ("a" ));
43+ assertThat (readValue .isBooleanValue (), equalTo (true ));
4844 }
4945
5046 @ Test (expected = UnrecognizedPropertyException .class )
@@ -61,8 +57,12 @@ public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenEx
6157 }
6258
6359 @ Test
64- public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect () throws JsonParseException , JsonMappingException , IOException {
65- final String jsonAsString = "{\" stringValue\" :\" a\" ,\" intValue\" :1,\" booleanValue\" :true,\" stringValue2\" :\" something\" }" ;
60+ public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializing_thenCorrect () throws JsonParseException , JsonMappingException , IOException {
61+ final String jsonAsString =// @formatter:off
62+ "{\" stringValue\" :\" a\" ," +
63+ "\" intValue\" :1," +
64+ "\" booleanValue\" :true," +
65+ "\" stringValue2\" :\" something\" }" ; // @formatter:on
6666 final ObjectMapper mapper = new ObjectMapper ();
6767 mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
6868
@@ -75,24 +75,20 @@ public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenD
7575 }
7676
7777 @ Test
78- public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect () throws JsonParseException , JsonMappingException , IOException {
79- final String jsonAsString = "{\" stringValue\" :\" a\" ,\" intValue\" :1,\" booleanValue\" :true,\" stringValue2\" :\" something\" }" ;
78+ public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect () throws JsonParseException , JsonMappingException , IOException {
79+ final String jsonAsString =// @formatter:off
80+ "{\" stringValue\" :\" a\" ," +
81+ "\" intValue\" :1," +
82+ "\" booleanValue\" :true," +
83+ "\" stringValue2\" :\" something\" }" ; // @formatter:on
8084 final ObjectMapper mapper = new ObjectMapper ();
81- mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
8285
8386 final MyDtoIgnoreUnkown readValue = mapper .readValue (jsonAsString , MyDtoIgnoreUnkown .class );
8487
8588 assertNotNull (readValue );
8689 assertThat (readValue .getStringValue (), equalTo ("a" ));
8790 assertThat (readValue .isBooleanValue (), equalTo (true ));
8891 assertThat (readValue .getIntValue (), equalTo (1 ));
89-
90- mapper .addHandler (new DeserializationProblemHandler () {
91- @ Override
92- public boolean handleUnknownProperty (final DeserializationContext ctxt , final JsonParser jp , final JsonDeserializer <?> deserializer , final Object beanOrClass , final String propertyName ) throws IOException , JsonProcessingException {
93- return super .handleUnknownProperty (ctxt , jp , deserializer , beanOrClass , propertyName );
94- }
95- });
9692 }
9793
9894}
0 commit comments