further jackson examples

This commit is contained in:
eugenp 2013-12-21 22:28:51 +02:00
parent 134248ac94
commit 62e0386a9d
6 changed files with 88 additions and 9 deletions

View File

@ -0,0 +1,8 @@
package org.baeldung.jackson;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class MyBooleanMixIn {
//
}

View File

@ -3,13 +3,13 @@ package org.baeldung.jackson;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = { "intValue" }) @JsonIgnoreProperties(value = { "intValue" })
public class BarDto { public class MyDtoIgnoreFieldByName {
private String stringValue; private String stringValue;
private int intValue; private int intValue;
private boolean booleanValue; private boolean booleanValue;
public BarDto() { public MyDtoIgnoreFieldByName() {
super(); super();
} }

View File

@ -0,0 +1,50 @@
package org.baeldung.jackson;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDtoIgnoreType {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDtoIgnoreType() {
super();
}
public MyDtoIgnoreType(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;
}
}

View File

@ -4,13 +4,13 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonInclude(Include.NON_DEFAULT) @JsonInclude(Include.NON_DEFAULT)
public class FooDto { public class MyDtoIncludeNonDefault {
private String stringValue; private String stringValue;
private int intValue; private int intValue;
private boolean booleanValue; private boolean booleanValue;
public FooDto() { public MyDtoIncludeNonDefault() {
super(); super();
} }

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson; package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -6,6 +6,8 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import org.baeldung.jackson.MyDto;
import org.baeldung.jackson.MyDtoIgnoreUnkown;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson; package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
@ -7,6 +7,10 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.baeldung.jackson.MyBooleanMixIn;
import org.baeldung.jackson.MyDto;
import org.baeldung.jackson.MyDtoIgnoreFieldByName;
import org.baeldung.jackson.MyDtoIncludeNonDefault;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
@ -20,7 +24,7 @@ public class JacksonSerializationUnitTest {
@Test @Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException { public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new FooDto()); final String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault());
assertThat(dtoAsString, not(containsString("intValue"))); assertThat(dtoAsString, not(containsString("intValue")));
System.out.println(dtoAsString); System.out.println(dtoAsString);
@ -29,7 +33,7 @@ public class JacksonSerializationUnitTest {
@Test @Test
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException { public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final FooDto dtoObject = new FooDto(); final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
dtoObject.setBooleanValue(true); dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject); final String dtoAsString = mapper.writeValueAsString(dtoObject);
@ -41,7 +45,7 @@ public class JacksonSerializationUnitTest {
@Test @Test
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final BarDto dtoObject = new BarDto(); final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
dtoObject.setBooleanValue(true); dtoObject.setBooleanValue(true);
final String dtoAsString = mapper.writeValueAsString(dtoObject); final String dtoAsString = mapper.writeValueAsString(dtoObject);
@ -51,6 +55,21 @@ public class JacksonSerializationUnitTest {
System.out.println(dtoAsString); 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);
final MyDto dtoObject = new MyDto();
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);
}
// tests - multiple entities to json // tests - multiple entities to json
@Test @Test