diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java deleted file mode 100644 index 8fbfb41cd8..0000000000 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java +++ /dev/null @@ -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; - } - -} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/User.java b/jackson/src/test/java/org/baeldung/jackson/field/User.java deleted file mode 100644 index 1210fd992e..0000000000 --- a/jackson/src/test/java/org/baeldung/jackson/field/User.java +++ /dev/null @@ -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; - } - -} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java index 7cb14c4151..a7394d9129 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java @@ -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")); - } - }