[BAEL-13511] - Create jackson-modules parent for all related modules
This commit is contained in:
-405
@@ -1,405 +0,0 @@
|
||||
package com.baeldung.jackson.annotation;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.annotation.bidirection.ItemWithIdentity;
|
||||
import com.baeldung.jackson.annotation.bidirection.ItemWithRef;
|
||||
import com.baeldung.jackson.annotation.bidirection.UserWithIdentity;
|
||||
import com.baeldung.jackson.annotation.bidirection.UserWithRef;
|
||||
import com.baeldung.jackson.annotation.date.EventWithFormat;
|
||||
import com.baeldung.jackson.annotation.date.EventWithSerializer;
|
||||
import com.baeldung.jackson.annotation.ignore.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.annotation.dtos.withEnum.DistanceEnumWithValue;
|
||||
import com.baeldung.jackson.annotation.exception.UserWithRoot;
|
||||
import com.baeldung.jackson.annotation.exception.UserWithRootNamespace;
|
||||
import com.baeldung.jackson.annotation.jsonview.Item;
|
||||
import com.baeldung.jackson.annotation.jsonview.Views;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
public class JacksonAnnotationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException {
|
||||
final ExtendableBean bean = new ExtendableBean("My bean");
|
||||
bean.add("attr1", "val1");
|
||||
bean.add("attr2", "val2");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("attr1"));
|
||||
assertThat(result, containsString("val1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithGetter bean = new BeanWithGetter(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, containsString("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonPropertyOrder_thenCorrect() throws JsonProcessingException {
|
||||
final MyBean bean = new MyBean(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, containsString("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonRawValue_thenCorrect() throws JsonProcessingException {
|
||||
final RawBean bean = new RawBean("My bean", "{\"attr\":false}");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, containsString("{\"attr\":false}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonRootName_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRoot user = new UserWithRoot(1, "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
|
||||
final String result = mapper.writeValueAsString(user);
|
||||
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonValue_thenCorrect() throws IOException {
|
||||
final String enumAsString = new ObjectMapper().writeValueAsString(DistanceEnumWithValue.MILE);
|
||||
|
||||
assertThat(enumAsString, is("1609.34"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
final String toParse = "20-12-2014 02:30:00";
|
||||
final Date date = df.parse(toParse);
|
||||
final EventWithSerializer event = new EventWithSerializer("party", date);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(event);
|
||||
assertThat(result, containsString(toParse));
|
||||
}
|
||||
|
||||
// ========================= Deserializing annotations ============================
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonCreator_thenCorrect() throws IOException {
|
||||
final String json = "{\"id\":1,\"theName\":\"My bean\"}";
|
||||
|
||||
final BeanWithCreator bean = new ObjectMapper().readerFor(BeanWithCreator.class)
|
||||
.readValue(json);
|
||||
assertEquals("My bean", bean.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonInject_thenCorrect() throws IOException {
|
||||
final String json = "{\"name\":\"My bean\"}";
|
||||
final InjectableValues inject = new InjectableValues.Std().addValue(int.class, 1);
|
||||
|
||||
final BeanWithInject bean = new ObjectMapper().reader(inject)
|
||||
.forType(BeanWithInject.class)
|
||||
.readValue(json);
|
||||
assertEquals("My bean", bean.name);
|
||||
assertEquals(1, bean.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws IOException {
|
||||
final String json = "{\"name\":\"My bean\",\"attr2\":\"val2\",\"attr1\":\"val1\"}";
|
||||
|
||||
final ExtendableBean bean = new ObjectMapper().readerFor(ExtendableBean.class)
|
||||
.readValue(json);
|
||||
assertEquals("My bean", bean.name);
|
||||
assertEquals("val2", bean.getProperties()
|
||||
.get("attr2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonSetter_thenCorrect() throws IOException {
|
||||
final String json = "{\"id\":1,\"name\":\"My bean\"}";
|
||||
|
||||
final BeanWithGetter bean = new ObjectMapper().readerFor(BeanWithGetter.class)
|
||||
.readValue(json);
|
||||
assertEquals("My bean", bean.getTheName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws IOException {
|
||||
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}";
|
||||
|
||||
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
final EventWithSerializer event = new ObjectMapper().readerFor(EventWithSerializer.class)
|
||||
.readValue(json);
|
||||
assertEquals("20-12-2014 02:30:00", df.format(event.eventDate));
|
||||
}
|
||||
|
||||
// ========================= Inclusion annotations ============================
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, not(containsString("id")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, not(containsString("id")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnoreType_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
final UserWithIgnoreType.Name name = new UserWithIgnoreType.Name("John", "Doe");
|
||||
final UserWithIgnoreType user = new UserWithIgnoreType(1, name);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
assertThat(result, containsString("1"));
|
||||
assertThat(result, not(containsString("name")));
|
||||
assertThat(result, not(containsString("John")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonInclude_thenCorrect() throws JsonProcessingException {
|
||||
final MyBean bean = new MyBean(1, null);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("1"));
|
||||
assertThat(result, not(containsString("name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonAutoDetect_thenCorrect() throws JsonProcessingException {
|
||||
final PrivateBean bean = new PrivateBean(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("1"));
|
||||
assertThat(result, containsString("My bean"));
|
||||
}
|
||||
|
||||
// ========================= Polymorphic annotations ============================
|
||||
|
||||
@Test
|
||||
public void whenSerializingPolymorphic_thenCorrect() throws JsonProcessingException {
|
||||
final Zoo.Dog dog = new Zoo.Dog("lacy");
|
||||
final Zoo zoo = new Zoo(dog);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(zoo);
|
||||
|
||||
assertThat(result, containsString("type"));
|
||||
assertThat(result, containsString("dog"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingPolymorphic_thenCorrect() throws IOException {
|
||||
final String json = "{\"animal\":{\"name\":\"lacy\",\"type\":\"cat\"}}";
|
||||
|
||||
final Zoo zoo = new ObjectMapper().readerFor(Zoo.class)
|
||||
.readValue(json);
|
||||
|
||||
assertEquals("lacy", zoo.animal.name);
|
||||
assertEquals(Zoo.Cat.class, zoo.animal.getClass());
|
||||
}
|
||||
// ========================= General annotations ============================
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonProperty_thenCorrect() throws IOException {
|
||||
final BeanWithGetter bean = new BeanWithGetter(1, "My bean");
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, containsString("1"));
|
||||
|
||||
final BeanWithGetter resultBean = new ObjectMapper().readerFor(BeanWithGetter.class)
|
||||
.readValue(result);
|
||||
assertEquals("My bean", resultBean.getTheName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonFormat_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
final String toParse = "20-12-2014 02:30:00";
|
||||
final Date date = df.parse(toParse);
|
||||
final EventWithFormat event = new EventWithFormat("party", date);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(event);
|
||||
assertThat(result, containsString(toParse));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonUnwrapped_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
final UnwrappedUser.Name name = new UnwrappedUser.Name("John", "Doe");
|
||||
final UnwrappedUser user = new UnwrappedUser(1, name);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(user);
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonView_thenCorrect() throws JsonProcessingException, JsonProcessingException {
|
||||
final Item item = new Item(2, "book", "John");
|
||||
|
||||
final String result = new ObjectMapper().writerWithView(Views.Public.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("2"));
|
||||
assertThat(result, not(containsString("John")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithIdentity user = new UserWithIdentity(1, "John");
|
||||
final ItemWithIdentity item = new ItemWithIdentity(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("userItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithFilter bean = new BeanWithFilter(1, "My bean");
|
||||
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
|
||||
|
||||
final String result = new ObjectMapper().writer(filters)
|
||||
.writeValueAsString(bean);
|
||||
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, not(containsString("id")));
|
||||
}
|
||||
|
||||
// =========================
|
||||
@Test
|
||||
public void whenSerializingUsingCustomAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithCustomAnnotation bean = new BeanWithCustomAnnotation(1, "My bean", null);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, containsString("1"));
|
||||
assertThat(result, not(containsString("dateCreated")));
|
||||
}
|
||||
|
||||
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
|
||||
@Test
|
||||
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
final com.baeldung.jackson.annotation.dtos.Item item = new com.baeldung.jackson.annotation.dtos.Item(1, "book", null);
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(item);
|
||||
assertThat(result, containsString("owner"));
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.addMixIn(com.baeldung.jackson.annotation.dtos.User.class, MyMixInForIgnoreType.class);
|
||||
|
||||
result = mapper.writeValueAsString(item);
|
||||
assertThat(result, not(containsString("owner")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDisablingAllAnnotations_thenAllDisabled() throws JsonProcessingException {
|
||||
final MyBean bean = new MyBean(1, null);
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(MapperFeature.USE_ANNOTATIONS);
|
||||
|
||||
final String result = mapper.writeValueAsString(bean);
|
||||
assertThat(result, containsString("1"));
|
||||
assertThat(result, containsString("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String json = "{\"fName\": \"John\", \"lastName\": \"Green\"}";
|
||||
|
||||
// act
|
||||
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
|
||||
|
||||
// assert
|
||||
assertThat(aliasBean.getFirstName(), is("John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
UserWithRootNamespace author = new UserWithRootNamespace(1, "John");
|
||||
|
||||
// act
|
||||
ObjectMapper mapper = new XmlMapper();
|
||||
mapper = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).enable(SerializationFeature.INDENT_OUTPUT);
|
||||
String result = mapper.writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(result, containsString("<user xmlns=\"users\">"));
|
||||
|
||||
/*
|
||||
<user xmlns="users">
|
||||
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
|
||||
<id xmlns="">1</id>
|
||||
<name xmlns="">John</name>
|
||||
<items xmlns=""/>
|
||||
</user>
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class IgnoreFieldsWithFilterUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||
|
||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||
dtoObject.setIntValue(12);
|
||||
|
||||
final String dtoAsString = mapper.writer(filters)
|
||||
.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
|
||||
@Override
|
||||
public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
|
||||
if (include(writer)) {
|
||||
if (!writer.getName()
|
||||
.equals("intValue")) {
|
||||
writer.serializeAsField(pojo, jgen, provider);
|
||||
return;
|
||||
}
|
||||
|
||||
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
|
||||
if (intValue >= 0) {
|
||||
writer.serializeAsField(pojo, jgen, provider);
|
||||
}
|
||||
} else if (!jgen.canOmitFields()) { // since 2.3
|
||||
writer.serializeAsOmittedField(pojo, jgen, provider);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean include(final BeanPropertyWriter writer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean include(final PropertyWriter writer) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||
|
||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||
dtoObject.setIntValue(-1);
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtoAsString = mapper.writer(filters)
|
||||
.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
}
|
||||
-119
@@ -1,119 +0,0 @@
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
|
||||
public class JacksonSerializationIgnoreUnitTest {
|
||||
|
||||
// tests - single entity to json
|
||||
|
||||
// ignore
|
||||
|
||||
@Test
|
||||
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault());
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
|
||||
dtoObject.setBooleanValue(true);
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
|
||||
dtoObject.setBooleanValue(true);
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField();
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
// @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, MyMixInForIgnoreType.class);
|
||||
final MyDtoWithSpecialField dtoObject = new MyDtoWithSpecialField();
|
||||
dtoObject.setBooleanValue(true);
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNullsIgnoredOnClass_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNullsIgnoredGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
final MyDto dtoObject = new MyDto();
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.jackson.ignorenullfields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class IgnoreNullFieldsUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenNullsIgnoredOnClass_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNullsIgnoredGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
final MyDto dtoObject = new MyDto();
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.jackson.jsonproperty;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
public class JsonPropertyUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtoAsString = mapper.writeValueAsString(new MyDto());
|
||||
|
||||
assertThat(dtoAsString, containsString("intValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNameOfFieldIsChangedViaAnnotationOnGetter_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged();
|
||||
dtoObject.setStringValue("a");
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||
assertThat(dtoAsString, containsString("strVal"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
}
|
||||
-114
@@ -1,114 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JavaReadWriteJsonExampleUnitTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||
final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]";
|
||||
|
||||
@Test
|
||||
public void whenWriteJavaToJson_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final String carAsString = objectMapper.writeValueAsString(car);
|
||||
assertThat(carAsString, containsString("yellow"));
|
||||
assertThat(carAsString, containsString("renault"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWriteToFile_thanCorrect() throws Exception {
|
||||
File resultFile = folder.newFile("car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car car = new Car("yellow", "renault");
|
||||
objectMapper.writeValue(resultFile, car);
|
||||
|
||||
Car fromFile = objectMapper.readValue(resultFile, Car.class);
|
||||
assertEquals(car.getType(), fromFile.getType());
|
||||
assertEquals(car.getColor(), fromFile.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToJava_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class);
|
||||
assertNotNull(car);
|
||||
assertThat(car.getColor(), containsString("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToJsonNode_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON);
|
||||
assertNotNull(jsonNode);
|
||||
assertThat(jsonNode.get("color")
|
||||
.asText(), containsString("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToList_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final List<Car> listCar = objectMapper.readValue(LOCAL_JSON, new TypeReference<List<Car>>() {
|
||||
|
||||
});
|
||||
for (final Car car : listCar) {
|
||||
assertNotNull(car);
|
||||
assertThat(car.getType(), equalTo("BMW"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToMap_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Map<String, Object> map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
assertNotNull(map);
|
||||
for (final String key : map.keySet()) {
|
||||
assertNotNull(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wheReadFromFile_thanCorrect() throws Exception {
|
||||
File resource = new File("src/test/resources/json_car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car fromFile = objectMapper.readValue(resource, Car.class);
|
||||
|
||||
assertEquals("BMW", fromFile.getType());
|
||||
assertEquals("Black", fromFile.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wheReadFromUrl_thanCorrect() throws Exception {
|
||||
URL resource = new URL("file:src/test/resources/json_car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car fromFile = objectMapper.readValue(resource, Car.class);
|
||||
|
||||
assertEquals("BMW", fromFile.getType());
|
||||
assertEquals("Black", fromFile.getColor());
|
||||
}
|
||||
}
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.baeldung.jackson.objectmapper.dto.Request;
|
||||
import com.fasterxml.jackson.core.Version;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class SerializationDeserializationFeatureUnitTest {
|
||||
|
||||
final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||
final String JSON_CAR = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
|
||||
final String JSON_ARRAY = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]";
|
||||
|
||||
@Test
|
||||
public void whenFailOnUnkownPropertiesFalse_thanJsonReadCorrectly() throws Exception {
|
||||
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
final Car car = objectMapper.readValue(JSON_CAR, Car.class);
|
||||
final JsonNode jsonNodeRoot = objectMapper.readTree(JSON_CAR);
|
||||
final JsonNode jsonNodeYear = jsonNodeRoot.get("year");
|
||||
final String year = jsonNodeYear.asText();
|
||||
|
||||
assertNotNull(car);
|
||||
assertThat(car.getColor(), equalTo("Black"));
|
||||
assertThat(year, containsString("1970"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCustomSerializerDeserializer_thanReadWriteCorrect() throws Exception {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null));
|
||||
serializerModule.addSerializer(Car.class, new CustomCarSerializer());
|
||||
mapper.registerModule(serializerModule);
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final String carJson = mapper.writeValueAsString(car);
|
||||
assertThat(carJson, containsString("renault"));
|
||||
assertThat(carJson, containsString("model"));
|
||||
|
||||
final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
|
||||
deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer());
|
||||
mapper.registerModule(deserializerModule);
|
||||
final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class);
|
||||
assertNotNull(carResult);
|
||||
assertThat(carResult.getColor(), equalTo("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDateFormatSet_thanSerializedAsExpected() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final Request request = new Request();
|
||||
request.setCar(car);
|
||||
request.setDatePurchased(new Date());
|
||||
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
|
||||
objectMapper.setDateFormat(df);
|
||||
final String carAsString = objectMapper.writeValueAsString(request);
|
||||
assertNotNull(carAsString);
|
||||
assertThat(carAsString, containsString("datePurchased"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseJavaArrayForJsonArrayTrue_thanJsonReadAsArray() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
||||
final Car[] cars = objectMapper.readValue(JSON_ARRAY, Car[].class);
|
||||
for (final Car car : cars) {
|
||||
assertNotNull(car);
|
||||
assertThat(car.getType(), equalTo("BMW"));
|
||||
}
|
||||
}
|
||||
}
|
||||
-80
@@ -1,80 +0,0 @@
|
||||
package com.baeldung.jackson.unknownproperties;
|
||||
|
||||
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.exc.UnrecognizedPropertyException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class UnknownPropertiesUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"booleanValue\":true}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
}
|
||||
|
||||
// tests - json with unknown fields
|
||||
|
||||
@Test(expected = UnrecognizedPropertyException.class)
|
||||
public final void givenJsonHasUnknownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenJsonHasUnknownValuesButJacksonIsIgnoringUnknownFields_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = // @formatter:off
|
||||
"{\"stringValue\":\"a\"," +
|
||||
"\"intValue\":1," +
|
||||
"\"booleanValue\":true," +
|
||||
"\"stringValue2\":\"something\"}"; // @formatter:on
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenJsonHasUnknownValuesButUnknownFieldsAreIgnoredOnClass_whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = // @formatter:off
|
||||
"{\"stringValue\":\"a\"," +
|
||||
"\"intValue\":1," +
|
||||
"\"booleanValue\":true," +
|
||||
"\"stringValue2\":\"something\"}"; // @formatter:on
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDtoIgnoreUnknown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnknown.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user