jackson work

This commit is contained in:
eugenp 2015-05-31 19:30:13 +01:00
parent ef39c99334
commit 524e05de83
6 changed files with 27 additions and 37 deletions

View File

@ -44,4 +44,11 @@ public class MyDto {
this.booleanValue = booleanValue;
}
//
@Override
public String toString() {
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
}
}

View File

@ -4,17 +4,19 @@ public class MyDtoAccessLevel {
private String stringValue;
int intValue;
protected float floatValue;
public boolean booleanValue;
public MyDtoAccessLevel() {
super();
}
public MyDtoAccessLevel(final String stringValue, final int intValue, final boolean booleanValue) {
public MyDtoAccessLevel(final String stringValue, final int intValue, final float floatValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.floatValue = floatValue;
this.booleanValue = booleanValue;
}

View File

@ -1,23 +0,0 @@
package org.baeldung.jackson.field;
public class MyDtoGetterImplicitDeserialization {
private String stringValue;
public MyDtoGetterImplicitDeserialization() {
super();
}
public MyDtoGetterImplicitDeserialization(final String stringValue) {
super();
this.stringValue = stringValue;
}
// API
public String getStringValue() {
return stringValue;
}
}

View File

@ -1,15 +1,15 @@
package org.baeldung.jackson.field;
public class MyDtoGetter {
public class MyDtoWithGetter {
private String stringValue;
private int intValue;
public MyDtoGetter() {
public MyDtoWithGetter() {
super();
}
public MyDtoGetter(final String stringValue, final int intValue) {
public MyDtoWithGetter(final String stringValue, final int intValue) {
super();
this.stringValue = stringValue;

View File

@ -9,9 +9,8 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.field.MyDtoAccessLevel;
import org.baeldung.jackson.field.MyDtoGetter;
import org.baeldung.jackson.field.MyDtoGetterImplicitDeserialization;
import org.baeldung.jackson.field.MyDtoSetter;
import org.baeldung.jackson.field.MyDtoWithGetter;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
@ -30,6 +29,7 @@ public class JacksonFieldUnitTest {
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("stringValue")));
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, not(containsString("floatValue")));
assertThat(dtoAsString, containsString("booleanValue"));
System.out.println(dtoAsString);
}
@ -38,7 +38,7 @@ public class JacksonFieldUnitTest {
public final void givenDifferentAccessLevels_whenGetterAdded_thenSerializable() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final MyDtoGetter dtoObject = new MyDtoGetter();
final MyDtoWithGetter dtoObject = new MyDtoWithGetter();
final String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("stringValue"));
@ -51,7 +51,7 @@ public class JacksonFieldUnitTest {
final String jsonAsString = "{\"stringValue\":\"dtoString\"}";
final ObjectMapper mapper = new ObjectMapper();
final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class);
final MyDtoWithGetter dtoObject = mapper.readValue(jsonAsString, MyDtoWithGetter.class);
assertNotNull(dtoObject);
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));

View File

@ -225,21 +225,25 @@ public class JacksonSerializationIgnoreUnitTest {
}
@Test
public final void givenAllowingMapObjectWithNullKey_whenWritingMapObjectWithNullKey_thenAllowed() throws JsonProcessingException {
public final void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer());
final MyDto dtoObject = new MyDto();
dtoObject.setStringValue("dtoObjectString");
final MyDto dtoObject1 = new MyDto();
dtoObject1.setStringValue("dtoObjectString1");
final MyDto dtoObject2 = new MyDto();
dtoObject2.setStringValue("dtoObjectString2");
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
dtoMap.put(null, dtoObject);
dtoMap.put(null, dtoObject1);
dtoMap.put("obj2", dtoObject2);
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
assertThat(dtoMapAsString, containsString(""));
assertThat(dtoMapAsString, containsString("dtoObjectString"));
System.out.println(dtoMapAsString);
assertThat(dtoMapAsString, containsString("\"\""));
assertThat(dtoMapAsString, containsString("dtoObjectString1"));
assertThat(dtoMapAsString, containsString("obj2"));
}
@Test