further jackson work

This commit is contained in:
eugenp 2013-12-21 23:04:01 +02:00
parent 62e0386a9d
commit e088098c50
3 changed files with 59 additions and 4 deletions

View File

@ -0,0 +1,42 @@
package org.baeldung.jackson;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class MyDtoIgnoreField {
private String stringValue;
@JsonIgnore
private int intValue;
private boolean booleanValue;
public MyDtoIgnoreField() {
super();
}
// 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;
}
}

View File

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

View File

@ -7,8 +7,9 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import org.baeldung.jackson.MyBooleanMixIn;
import org.baeldung.jackson.MyMixInForString;
import org.baeldung.jackson.MyDto;
import org.baeldung.jackson.MyDtoIgnoreField;
import org.baeldung.jackson.MyDtoIgnoreFieldByName;
import org.baeldung.jackson.MyDtoIncludeNonDefault;
import org.junit.Test;
@ -43,7 +44,7 @@ public class JacksonSerializationUnitTest {
}
@Test
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
dtoObject.setBooleanValue(true);
@ -55,10 +56,22 @@ public class JacksonSerializationUnitTest {
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);
}
@Test
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(String.class, MyBooleanMixIn.class);
mapper.addMixInAnnotations(String.class, MyMixInForString.class);
final MyDto dtoObject = new MyDto();
dtoObject.setBooleanValue(true);