Reduce fields not necessary.

This commit is contained in:
Rachel Shu 2014-07-22 09:09:34 -04:00
parent c96bdead45
commit 09b4633741
4 changed files with 11 additions and 20 deletions

View File

@ -9,7 +9,7 @@ public class MyDtoGetter {
super(); super();
} }
public MyDtoGetter(final String stringValue, final int intValue, final boolean booleanValue) { public MyDtoGetter(final String stringValue, final int intValue) {
super(); super();
this.stringValue = stringValue; this.stringValue = stringValue;

View File

@ -3,17 +3,15 @@ package org.baeldung.jackson.field;
public class MyDtoGetterImplicitDeserialization { public class MyDtoGetterImplicitDeserialization {
private String stringValue; private String stringValue;
public boolean booleanValue;
public MyDtoGetterImplicitDeserialization() { public MyDtoGetterImplicitDeserialization() {
super(); super();
} }
public MyDtoGetterImplicitDeserialization(final String stringValue, final int intValue, final boolean booleanValue) { public MyDtoGetterImplicitDeserialization(final String stringValue) {
super(); super();
this.stringValue = stringValue; this.stringValue = stringValue;
this.booleanValue = booleanValue;
} }
// API // API

View File

@ -2,26 +2,22 @@ package org.baeldung.jackson.field;
public class MyDtoSetter { public class MyDtoSetter {
private String stringValue; private int intValue;
int intValue; public boolean booleanValue;
public MyDtoSetter() { public MyDtoSetter() {
super(); super();
} }
public MyDtoSetter(final String stringValue, final int intValue, final boolean booleanValue) { public MyDtoSetter(final int intValue, final boolean booleanValue) {
super(); super();
this.stringValue = stringValue;
this.intValue = intValue; this.intValue = intValue;
this.booleanValue = booleanValue;
} }
// API // API
public String getStringValue() {
return stringValue;
}
public void setIntValue(final int intValue) { public void setIntValue(final int intValue) {
this.intValue = intValue; this.intValue = intValue;
} }

View File

@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonFieldUnitTest { public class JacksonFieldUnitTest {
@Test @Test
public final void givenDifferentAccessLevels_whenPrivateOrPackage_thenNotSerializable_whenPublic_thenSerializable() throws JsonProcessingException { public final void givenDifferentAccessLevels_whenPublic_thenSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel(); final MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
@ -49,42 +49,39 @@ public class JacksonFieldUnitTest {
@Test @Test
public final void givenDifferentAccessLevels_whenGetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException { public final void givenDifferentAccessLevels_whenGetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"dtoString\",\"booleanValue\":\"true\"}"; final String jsonAsString = "{\"stringValue\":\"dtoString\"}";
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class); final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class);
assertNotNull(dtoObject); assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString")); assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
assertThat(dtoObject.booleanValue, equalTo(true));
} }
@Test @Test
public final void givenDifferentAccessLevels_whenSetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException { public final void givenDifferentAccessLevels_whenSetterAdded_thenDeserializable() throws JsonProcessingException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"dtoString\",\"intValue\":1}"; final String jsonAsString = "{\"intValue\":1}";
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final MyDtoSetter dtoObject = mapper.readValue(jsonAsString, MyDtoSetter.class); final MyDtoSetter dtoObject = mapper.readValue(jsonAsString, MyDtoSetter.class);
assertNotNull(dtoObject); assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
assertThat(dtoObject.anotherGetIntValue(), equalTo(1)); assertThat(dtoObject.anotherGetIntValue(), equalTo(1));
} }
@Test @Test
public final void givenDifferentAccessLevels_whenSetterAdded_thenStillNotSerializable() throws JsonProcessingException, JsonMappingException, IOException { public final void givenDifferentAccessLevels_whenSetterAdded_thenStillNotSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final MyDtoSetter dtoObject = new MyDtoSetter(); final MyDtoSetter dtoObject = new MyDtoSetter();
final String dtoAsString = mapper.writeValueAsString(dtoObject); final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
assertThat(dtoAsString, not(containsString("intValue"))); assertThat(dtoAsString, not(containsString("intValue")));
System.out.println(dtoAsString); System.out.println(dtoAsString);
} }
@Test @Test
public final void givenDifferentAccessLevels_whenSetVisibility_thenSerializable() throws JsonProcessingException, JsonMappingException, IOException { public final void givenDifferentAccessLevels_whenSetVisibility_thenSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);