Updated the tutorial examples: remove change name example, and

setVisibility example
This commit is contained in:
Rachel Shu 2014-07-16 11:28:51 -04:00
parent d597252bf1
commit 15123d8d5a
3 changed files with 7 additions and 149 deletions

View File

@ -1,50 +0,0 @@
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

@ -1,50 +0,0 @@
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

@ -9,14 +9,13 @@ 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.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -89,59 +88,18 @@ public class JacksonFieldUnitTest {
}
@Test
public final void givenCustomizedPropertyName_whenFieldAnnotated_thenPropertyNameCustomizedOnSerialization() throws JsonProcessingException, JsonMappingException, IOException {
public final void givenDifferentAccessLevels_whenSetVisibility_thenSerializable() throws JsonProcessingException, JsonMappingException, IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
final MyDtoCustomizedPropertyName dtoObject = new MyDtoCustomizedPropertyName();
final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
assertThat(dtoAsString, containsString("intValue"));
assertThat(dtoAsString, containsString("BOOLEANVALUE"));
assertThat(dtoAsString, not(containsString("booleanValue")));
assertThat(dtoAsString, 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"));
}
}