upgrade jackson

This commit is contained in:
DOHA 2016-07-11 12:23:02 +02:00
parent 73a89d9400
commit da58ba479d
8 changed files with 121 additions and 65 deletions

View File

@ -0,0 +1,54 @@
package com.baeldung.jackson.dtos;
public class MyDtoWithSpecialField {
private String[] stringValue;
private int intValue;
private boolean booleanValue;
public MyDtoWithSpecialField() {
super();
}
public MyDtoWithSpecialField(final String[] stringValue, final int intValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
}
// API
public String[] getStringValue() {
return stringValue;
}
public void setStringValue(final String[] stringValue) {
this.stringValue = stringValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(final int intValue) {
this.intValue = intValue;
}
public boolean isBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
//
@Override
public String toString() {
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
}
}

View File

@ -3,6 +3,6 @@ package com.baeldung.jackson.dtos;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class MyMixInForString {
public class MyMixInForIgnoreType {
//
}

View File

@ -12,11 +12,8 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.baeldung.jackson.bidirection.ItemWithIdentity;
import com.baeldung.jackson.bidirection.ItemWithRef;
import com.baeldung.jackson.bidirection.UserWithRef;
import com.baeldung.jackson.dtos.User;
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
import org.junit.Test;
import com.baeldung.jackson.annotation.BeanWithCreator;
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
import com.baeldung.jackson.annotation.BeanWithFilter;
@ -30,16 +27,17 @@ import com.baeldung.jackson.annotation.RawBean;
import com.baeldung.jackson.annotation.UnwrappedUser;
import com.baeldung.jackson.annotation.UserWithIgnoreType;
import com.baeldung.jackson.annotation.Zoo;
import com.baeldung.jackson.bidirection.ItemWithIdentity;
import com.baeldung.jackson.bidirection.ItemWithRef;
import com.baeldung.jackson.bidirection.UserWithIdentity;
import com.baeldung.jackson.bidirection.UserWithRef;
import com.baeldung.jackson.date.EventWithFormat;
import com.baeldung.jackson.date.EventWithSerializer;
import com.baeldung.jackson.dtos.MyMixInForString;
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
import com.baeldung.jackson.exception.UserWithRoot;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.Views;
import org.junit.Ignore;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
@ -127,7 +125,7 @@ public class JacksonAnnotationTest {
public void whenDeserializingUsingJsonCreator_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":1,\"theName\":\"My bean\"}";
final BeanWithCreator bean = new ObjectMapper().reader(BeanWithCreator.class).readValue(json);
final BeanWithCreator bean = new ObjectMapper().readerFor(BeanWithCreator.class).readValue(json);
assertEquals("My bean", bean.name);
}
@ -136,7 +134,7 @@ public class JacksonAnnotationTest {
final String json = "{\"name\":\"My bean\"}";
final InjectableValues inject = new InjectableValues.Std().addValue(int.class, 1);
final BeanWithInject bean = new ObjectMapper().reader(inject).withType(BeanWithInject.class).readValue(json);
final BeanWithInject bean = new ObjectMapper().reader(inject).forType(BeanWithInject.class).readValue(json);
assertEquals("My bean", bean.name);
assertEquals(1, bean.id);
}
@ -145,7 +143,7 @@ public class JacksonAnnotationTest {
public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"name\":\"My bean\",\"attr2\":\"val2\",\"attr1\":\"val1\"}";
final ExtendableBean bean = new ObjectMapper().reader(ExtendableBean.class).readValue(json);
final ExtendableBean bean = new ObjectMapper().readerFor(ExtendableBean.class).readValue(json);
assertEquals("My bean", bean.name);
assertEquals("val2", bean.getProperties().get("attr2"));
}
@ -154,7 +152,7 @@ public class JacksonAnnotationTest {
public void whenDeserializingUsingJsonSetter_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":1,\"name\":\"My bean\"}";
final BeanWithGetter bean = new ObjectMapper().reader(BeanWithGetter.class).readValue(json);
final BeanWithGetter bean = new ObjectMapper().readerFor(BeanWithGetter.class).readValue(json);
assertEquals("My bean", bean.getTheName());
}
@ -164,7 +162,7 @@ public class JacksonAnnotationTest {
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
final EventWithSerializer event = new ObjectMapper().reader(EventWithSerializer.class).readValue(json);
final EventWithSerializer event = new ObjectMapper().readerFor(EventWithSerializer.class).readValue(json);
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
}
@ -235,7 +233,7 @@ public class JacksonAnnotationTest {
public void whenDeserializingPolymorphic_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"animal\":{\"name\":\"lacy\",\"type\":\"cat\"}}";
final Zoo zoo = new ObjectMapper().reader().withType(Zoo.class).readValue(json);
final Zoo zoo = new ObjectMapper().readerFor(Zoo.class).readValue(json);
assertEquals("lacy", zoo.animal.name);
assertEquals(Zoo.Cat.class, zoo.animal.getClass());
@ -250,7 +248,7 @@ public class JacksonAnnotationTest {
assertThat(result, containsString("My bean"));
assertThat(result, containsString("1"));
final BeanWithGetter resultBean = new ObjectMapper().reader(BeanWithGetter.class).readValue(result);
final BeanWithGetter resultBean = new ObjectMapper().readerFor(BeanWithGetter.class).readValue(result);
assertEquals("My bean", resultBean.getTheName());
}
@ -338,19 +336,19 @@ public class JacksonAnnotationTest {
assertThat(result, not(containsString("dateCreated")));
}
@Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
@Test
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
final User user = new User(1, "John");
final com.baeldung.jackson.dtos.Item item = new com.baeldung.jackson.dtos.Item(1, "book", null);
String result = new ObjectMapper().writeValueAsString(user);
assertThat(result, containsString("John"));
String result = new ObjectMapper().writeValueAsString(item);
assertThat(result, containsString("owner"));
final ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(String.class, MyMixInForString.class);
mapper.addMixIn(com.baeldung.jackson.dtos.User.class, MyMixInForIgnoreType.class);
result = mapper.writeValueAsString(user);
assertThat(result, not(containsString("John")));
result = mapper.writeValueAsString(item);
assertThat(result, not(containsString("owner")));
}
@Test

View File

@ -7,6 +7,8 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.junit.Test;
import com.baeldung.jackson.bidirection.Item;
import com.baeldung.jackson.bidirection.ItemWithIdentity;
import com.baeldung.jackson.bidirection.ItemWithIgnore;
@ -20,8 +22,6 @@ import com.baeldung.jackson.bidirection.UserWithRef;
import com.baeldung.jackson.bidirection.UserWithSerializer;
import com.baeldung.jackson.bidirection.UserWithView;
import com.baeldung.jackson.jsonview.Views;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -93,7 +93,7 @@ public class JacksonBidirectionRelationTest {
public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
final ItemWithIdentity item = new ObjectMapper().reader(ItemWithIdentity.class).readValue(json);
final ItemWithIdentity item = new ObjectMapper().readerFor(ItemWithIdentity.class).readValue(json);
assertEquals(2, item.id);
assertEquals("book", item.itemName);
@ -104,7 +104,7 @@ public class JacksonBidirectionRelationTest {
public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
final ItemWithSerializer item = new ObjectMapper().reader(ItemWithSerializer.class).readValue(json);
final ItemWithSerializer item = new ObjectMapper().readerFor(ItemWithSerializer.class).readValue(json);
assertEquals(2, item.id);
assertEquals("book", item.itemName);
assertEquals("John", item.owner.name);

View File

@ -11,15 +11,15 @@ import java.time.LocalDateTime;
import java.util.Date;
import java.util.TimeZone;
import com.baeldung.jackson.date.EventWithLocalDateTime;
import com.baeldung.jackson.date.Event;
import com.baeldung.jackson.date.EventWithFormat;
import com.baeldung.jackson.date.EventWithJodaTime;
import com.baeldung.jackson.date.EventWithSerializer;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import com.baeldung.jackson.date.Event;
import com.baeldung.jackson.date.EventWithFormat;
import com.baeldung.jackson.date.EventWithJodaTime;
import com.baeldung.jackson.date.EventWithLocalDateTime;
import com.baeldung.jackson.date.EventWithSerializer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@ -128,7 +128,7 @@ public class JacksonDateTest {
final ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(df);
final Event event = mapper.reader(Event.class).readValue(json);
final Event event = mapper.readerFor(Event.class).readValue(json);
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
}
@ -139,7 +139,7 @@ public class JacksonDateTest {
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
final ObjectMapper mapper = new ObjectMapper();
final EventWithSerializer event = mapper.reader(EventWithSerializer.class).readValue(json);
final EventWithSerializer event = mapper.readerFor(EventWithSerializer.class).readValue(json);
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
}

View File

@ -7,9 +7,13 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import com.baeldung.jackson.exception.*;
import org.junit.Test;
import com.baeldung.jackson.exception.User;
import com.baeldung.jackson.exception.UserWithPrivateFields;
import com.baeldung.jackson.exception.UserWithRoot;
import com.baeldung.jackson.exception.Zoo;
import com.baeldung.jackson.exception.ZooConfigured;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonFactory;
@ -30,7 +34,7 @@ public class JacksonExceptionsTest {
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(Zoo.class).readValue(json);
mapper.reader().forType(Zoo.class).readValue(json);
}
@Test
@ -38,7 +42,7 @@ public class JacksonExceptionsTest {
final String json = "{\"animal\":{\"name\":\"lacy\"}}";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(ZooConfigured.class).readValue(json);
mapper.reader().forType(ZooConfigured.class).readValue(json);
}
// JsonMappingException: No serializer found for class
@ -67,7 +71,7 @@ public class JacksonExceptionsTest {
final String json = "{\"id\":1,\"name\":\"John\"}";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(User.class).readValue(json);
mapper.reader().forType(User.class).readValue(json);
}
@Test
@ -75,7 +79,7 @@ public class JacksonExceptionsTest {
final String json = "{\"id\":1,\"name\":\"John\"}";
final ObjectMapper mapper = new ObjectMapper();
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}
@ -87,7 +91,7 @@ public class JacksonExceptionsTest {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
}
@Test
@ -97,7 +101,7 @@ public class JacksonExceptionsTest {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
final UserWithRoot user = mapper.reader().withType(UserWithRoot.class).readValue(json);
final UserWithRoot user = mapper.reader().forType(UserWithRoot.class).readValue(json);
assertEquals("John", user.name);
}
@ -107,7 +111,7 @@ public class JacksonExceptionsTest {
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
}
@Test
@ -115,7 +119,7 @@ public class JacksonExceptionsTest {
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
final ObjectMapper mapper = new ObjectMapper();
final List<com.baeldung.jackson.dtos.User> users = mapper.reader().withType(new TypeReference<List<com.baeldung.jackson.dtos.User>>() {
final List<com.baeldung.jackson.dtos.User> users = mapper.reader().forType(new TypeReference<List<com.baeldung.jackson.dtos.User>>() {
}).readValue(json);
assertEquals(2, users.size());
@ -127,7 +131,7 @@ public class JacksonExceptionsTest {
final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
}
@Test
@ -137,7 +141,7 @@ public class JacksonExceptionsTest {
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}
@ -147,7 +151,7 @@ public class JacksonExceptionsTest {
final String json = "{'id':1,'name':'John'}";
final ObjectMapper mapper = new ObjectMapper();
mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
}
@Test
@ -158,7 +162,7 @@ public class JacksonExceptionsTest {
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
final ObjectMapper mapper = new ObjectMapper(factory);
final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json);
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}

View File

@ -7,12 +7,12 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.User;
import com.baeldung.jackson.jsonview.MyBeanSerializerModifier;
import com.baeldung.jackson.jsonview.Views;
import org.junit.Test;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.MyBeanSerializerModifier;
import com.baeldung.jackson.jsonview.User;
import com.baeldung.jackson.jsonview.Views;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -66,7 +66,7 @@ public class JacksonJsonViewTest {
final ObjectMapper mapper = new ObjectMapper();
final User user = mapper.readerWithView(Views.Public.class).withType(User.class).readValue(json);
final User user = mapper.readerWithView(Views.Public.class).forType(User.class).readValue(json);
assertEquals(1, user.getId());
assertEquals("John", user.getName());
}

View File

@ -8,17 +8,17 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
import com.baeldung.jackson.dtos.MyDtoWithFilter;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull;
import com.baeldung.jackson.dtos.MyDto;
import com.baeldung.jackson.dtos.MyMixInForString;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreField;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName;
import com.baeldung.jackson.serialization.MyDtoNullKeySerializer;
import org.junit.Ignore;
import org.junit.Test;
import com.baeldung.jackson.dtos.MyDto;
import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
import com.baeldung.jackson.dtos.MyDtoWithFilter;
import com.baeldung.jackson.dtos.MyDtoWithSpecialField;
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreField;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName;
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull;
import com.baeldung.jackson.serialization.MyDtoNullKeySerializer;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
@ -85,12 +85,12 @@ public class JacksonSerializationIgnoreUnitTest {
System.out.println(dtoAsString);
}
@Ignore("Jackson 2.7.1-1 seems to have changed the API for this case")
// @Ignore("Jackson 2.7.1-1 seems to have changed the API for this case")
@Test
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(String.class, MyMixInForString.class);
final MyDto dtoObject = new MyDto();
mapper.addMixIn(String[].class, MyMixInForIgnoreType.class);
final MyDtoWithSpecialField dtoObject = new MyDtoWithSpecialField();
dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject);