Serializable field in Jackson

This commit is contained in:
Rachel Shu 2014-07-11 11:12:15 -04:00
parent a1ff4c6900
commit d597252bf1
9 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package org.baeldung.jackson.field;
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
public MyDto(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 getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
}

View File

@ -0,0 +1,21 @@
package org.baeldung.jackson.field;
public class MyDtoAccessLevel {
private String stringValue;
int intValue;
public boolean booleanValue;
public MyDtoAccessLevel() {
super();
}
public MyDtoAccessLevel(final String stringValue, final int intValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
}
}

View File

@ -0,0 +1,50 @@
package org.baeldung.jackson.field;
import com.fasterxml.jackson.annotation.JsonProperty;
public class MyDtoCustomizedPropertyName {
private String stringValue;
private int intValue;
@JsonProperty("BOOLEANVALUE")
private boolean booleanValue;
public MyDtoCustomizedPropertyName() {
super();
}
public MyDtoCustomizedPropertyName(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 getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.jackson.field;
public class MyDtoGetter {
private String stringValue;
int intValue;
public boolean booleanValue;
public MyDtoGetter() {
super();
}
public MyDtoGetter(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;
}
}

View File

@ -0,0 +1,31 @@
package org.baeldung.jackson.field;
public class MyDtoGetterImplicitDeserialization {
private String stringValue;
int intValue;
public boolean booleanValue;
public MyDtoGetterImplicitDeserialization() {
super();
}
public MyDtoGetterImplicitDeserialization(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 int getIntValue() {
return intValue;
}
}

View File

@ -0,0 +1,35 @@
package org.baeldung.jackson.field;
public class MyDtoSetter {
private String stringValue;
int intValue;
public boolean booleanValue;
public MyDtoSetter() {
super();
}
public MyDtoSetter(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 setIntValue(final int intValue) {
this.intValue = intValue;
}
public int anotherGetIntValue() {
return intValue;
}
}

View File

@ -0,0 +1,50 @@
package org.baeldung.jackson.field;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private int id;
private String name;
@JsonIgnore
private String password;
public User() {
super();
}
public User(final int id, final String name, final String password) {
this.id = id;
this.name = name;
this.password = password;
}
// API
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(final String password) {
this.password = password;
}
}

View File

@ -0,0 +1,147 @@
package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.field.MyDtoAccessLevel;
import org.baeldung.jackson.field.MyDtoCustomizedPropertyName;
import org.baeldung.jackson.field.MyDtoGetter;
import org.baeldung.jackson.field.MyDtoGetterImplicitDeserialization;
import org.baeldung.jackson.field.MyDtoSetter;
import org.baeldung.jackson.field.User;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonFieldUnitTest {
@Test
public final void givenDifferentAccessLevels_whenPrivateOrPackage_thenNotSerializable_whenPublic_thenSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("stringValue")));
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@Test
public final void givenDifferentAccessLevels_whenGetterAdded_thenSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoGetter dtoObject = new MyDtoGetter();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@Test
public final void givenDifferentAccessLevels_whenGetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"dtoString\",\"intValue\":1,\"booleanValue\":\"true\"}";
final ObjectMapper mapper = new ObjectMapper();
final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class);
assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
assertThat(dtoObject.getIntValue(), equalTo(1));
assertThat(dtoObject.booleanValue, equalTo(true));
}
@Test
public final void givenDifferentAccessLevels_whenSetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"dtoString\",\"intValue\":1,\"booleanValue\":\"true\"}";
final ObjectMapper mapper = new ObjectMapper();
final MyDtoSetter dtoObject = mapper.readValue(jsonAsString, MyDtoSetter.class);
assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
assertThat(dtoObject.anotherGetIntValue(), equalTo(1));
assertThat(dtoObject.booleanValue, equalTo(true));
}
@Test
public final void givenDifferentAccessLevels_whenSetterAdded_thenStillNotSerializable() throws JsonProcessingException, JsonMappingException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoSetter dtoObject = new MyDtoSetter();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@Test
public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnSerialization() throws JsonProcessingException, JsonMappingException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoCustomizedPropertyName dtoObject = new MyDtoCustomizedPropertyName();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
assertThat(dtoAsString, containsString("intValue"));
assertThat(dtoAsString, containsString("BOOLEANVALUE"));
assertThat(dtoAsString, not(containsString("booleanValue")));
System.out.println(dtoAsString);
}
@Test
public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnDeserialization() throws JsonProcessingException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"dtoString\",\"intValue\":1,\"BOOLEANVALUE\":\"true\"}";
final ObjectMapper mapper = new ObjectMapper();
final MyDtoCustomizedPropertyName dtoObject = mapper.readValue(jsonAsString, MyDtoCustomizedPropertyName.class);
assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
assertThat(dtoObject.getIntValue(), equalTo(1));
assertThat(dtoObject.getBooleanValue(), equalTo(true));
}
@Test
public final void givenFieldIsIgnoredOnlyAtSerialization_whenUserIsSerialized_thenIgnored() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final User userObject = new User();
userObject.setId(1);
userObject.setName("theUser");
userObject.setPassword("thePassword");
final String userAsString = mapper.writeValueAsString(userObject);
assertThat(userAsString, containsString("id"));
assertThat(userAsString, containsString("name"));
assertThat(userAsString, not(containsString("password")));
System.out.println(userAsString);
}
@Test
public final void givenFieldIsIgnoredOnlyAtSerialization_whenUserIsDeserialized_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "{\"id\":1,\"name\":\"theUser\",\"password\":\"thePassword\"}";
final ObjectMapper mapper = new ObjectMapper();
final User userObject = mapper.readValue(jsonAsString, User.class);
assertNotNull(userObject);
assertThat(userObject.getId(), equalTo(1));
assertThat(userObject.getName(), equalTo("theUser"));
assertThat(userObject.getPassword(), equalTo("thePassword"));
}
}

View File

@ -15,6 +15,7 @@ import org.junit.runners.Suite;
,JacksonSerializationIgnoreUnitTest.class
,JacksonSerializationUnitTest.class
,SandboxTest.class
,JacksonFieldUnitTest.class
}) // @formatter:on
public class UnitTestSuite {
}