jackson work
This commit is contained in:
parent
ef39c99334
commit
524e05de83
|
@ -44,4 +44,11 @@ public class MyDto {
|
||||||
this.booleanValue = booleanValue;
|
this.booleanValue = booleanValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,19 @@ public class MyDtoAccessLevel {
|
||||||
|
|
||||||
private String stringValue;
|
private String stringValue;
|
||||||
int intValue;
|
int intValue;
|
||||||
|
protected float floatValue;
|
||||||
public boolean booleanValue;
|
public boolean booleanValue;
|
||||||
|
|
||||||
public MyDtoAccessLevel() {
|
public MyDtoAccessLevel() {
|
||||||
super();
|
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();
|
super();
|
||||||
|
|
||||||
this.stringValue = stringValue;
|
this.stringValue = stringValue;
|
||||||
this.intValue = intValue;
|
this.intValue = intValue;
|
||||||
|
this.floatValue = floatValue;
|
||||||
this.booleanValue = booleanValue;
|
this.booleanValue = booleanValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,15 +1,15 @@
|
||||||
package org.baeldung.jackson.field;
|
package org.baeldung.jackson.field;
|
||||||
|
|
||||||
public class MyDtoGetter {
|
public class MyDtoWithGetter {
|
||||||
|
|
||||||
private String stringValue;
|
private String stringValue;
|
||||||
private int intValue;
|
private int intValue;
|
||||||
|
|
||||||
public MyDtoGetter() {
|
public MyDtoWithGetter() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MyDtoGetter(final String stringValue, final int intValue) {
|
public MyDtoWithGetter(final String stringValue, final int intValue) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.stringValue = stringValue;
|
this.stringValue = stringValue;
|
|
@ -9,9 +9,8 @@ import static org.junit.Assert.assertThat;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.baeldung.jackson.field.MyDtoAccessLevel;
|
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.MyDtoSetter;
|
||||||
|
import org.baeldung.jackson.field.MyDtoWithGetter;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
|
@ -30,6 +29,7 @@ public class JacksonFieldUnitTest {
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
assertThat(dtoAsString, not(containsString("intValue")));
|
assertThat(dtoAsString, not(containsString("intValue")));
|
||||||
|
assertThat(dtoAsString, not(containsString("floatValue")));
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class JacksonFieldUnitTest {
|
||||||
public final void givenDifferentAccessLevels_whenGetterAdded_thenSerializable() throws JsonProcessingException {
|
public final void givenDifferentAccessLevels_whenGetterAdded_thenSerializable() throws JsonProcessingException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
final MyDtoGetter dtoObject = new MyDtoGetter();
|
final MyDtoWithGetter dtoObject = new MyDtoWithGetter();
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
assertThat(dtoAsString, containsString("stringValue"));
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
|
@ -51,7 +51,7 @@ public class JacksonFieldUnitTest {
|
||||||
final String jsonAsString = "{\"stringValue\":\"dtoString\"}";
|
final String jsonAsString = "{\"stringValue\":\"dtoString\"}";
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class);
|
final MyDtoWithGetter dtoObject = mapper.readValue(jsonAsString, MyDtoWithGetter.class);
|
||||||
|
|
||||||
assertNotNull(dtoObject);
|
assertNotNull(dtoObject);
|
||||||
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
|
assertThat(dtoObject.getStringValue(), equalTo("dtoString"));
|
||||||
|
|
|
@ -225,21 +225,25 @@ public class JacksonSerializationIgnoreUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenAllowingMapObjectWithNullKey_whenWritingMapObjectWithNullKey_thenAllowed() throws JsonProcessingException {
|
public final void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer());
|
mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer());
|
||||||
|
|
||||||
final MyDto dtoObject = new MyDto();
|
final MyDto dtoObject1 = new MyDto();
|
||||||
dtoObject.setStringValue("dtoObjectString");
|
dtoObject1.setStringValue("dtoObjectString1");
|
||||||
|
final MyDto dtoObject2 = new MyDto();
|
||||||
|
dtoObject2.setStringValue("dtoObjectString2");
|
||||||
|
|
||||||
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
|
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);
|
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
|
||||||
|
|
||||||
assertThat(dtoMapAsString, containsString(""));
|
|
||||||
assertThat(dtoMapAsString, containsString("dtoObjectString"));
|
|
||||||
System.out.println(dtoMapAsString);
|
System.out.println(dtoMapAsString);
|
||||||
|
assertThat(dtoMapAsString, containsString("\"\""));
|
||||||
|
assertThat(dtoMapAsString, containsString("dtoObjectString1"));
|
||||||
|
assertThat(dtoMapAsString, containsString("obj2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue