From d597252bf16a6578900d0c64d3360054029d260f Mon Sep 17 00:00:00 2001 From: Rachel Shu Date: Fri, 11 Jul 2014 11:12:15 -0400 Subject: [PATCH] Serializable field in Jackson --- .../org/baeldung/jackson/field/MyDto.java | 47 ++++++ .../jackson/field/MyDtoAccessLevel.java | 21 +++ .../field/MyDtoCustomizedPropertyName.java | 50 ++++++ .../baeldung/jackson/field/MyDtoGetter.java | 27 ++++ .../MyDtoGetterImplicitDeserialization.java | 31 ++++ .../baeldung/jackson/field/MyDtoSetter.java | 35 +++++ .../java/org/baeldung/jackson/field/User.java | 50 ++++++ .../jackson/test/JacksonFieldUnitTest.java | 147 ++++++++++++++++++ .../baeldung/jackson/test/UnitTestSuite.java | 1 + 9 files changed, 409 insertions(+) create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDto.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/MyDtoSetter.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/field/User.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDto.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDto.java new file mode 100644 index 0000000000..edfc9d2f91 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDto.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java new file mode 100644 index 0000000000..d1761b30ed --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java new file mode 100644 index 0000000000..8fbfb41cd8 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoCustomizedPropertyName.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java new file mode 100644 index 0000000000..12feb98628 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java new file mode 100644 index 0000000000..ee9d346a0a --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoSetter.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoSetter.java new file mode 100644 index 0000000000..391b1959ae --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoSetter.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/User.java b/jackson/src/test/java/org/baeldung/jackson/field/User.java new file mode 100644 index 0000000000..1210fd992e --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/field/User.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java new file mode 100644 index 0000000000..7cb14c4151 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java @@ -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")); + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java b/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java index 2eb24928d6..886b43d05d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java @@ -15,6 +15,7 @@ import org.junit.runners.Suite; ,JacksonSerializationIgnoreUnitTest.class ,JacksonSerializationUnitTest.class ,SandboxTest.class + ,JacksonFieldUnitTest.class }) // @formatter:on public class UnitTestSuite { } \ No newline at end of file