jackson work
This commit is contained in:
parent
88fe235abc
commit
6f9026a183
|
@ -0,0 +1,42 @@
|
|||
package org.baeldung.jackson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(value = { "intValue" })
|
||||
public class BarDto {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public BarDto() {
|
||||
super();
|
||||
}
|
||||
|
||||
// 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 isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +1,133 @@
|
|||
package org.baeldung.jackson;
|
||||
|
||||
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 java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class JacksonUnitTest {
|
||||
|
||||
// tests
|
||||
// tests - single entity to json
|
||||
|
||||
@Test
|
||||
public final void whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
|
||||
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String fooDtoAsString = mapper.writeValueAsString(new FooDto());
|
||||
System.out.println(fooDtoAsString);
|
||||
final String dtoAsString = mapper.writeValueAsString(new FooDto());
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
|
||||
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final FooDto dtoObject = new FooDto();
|
||||
dtoObject.setBooleanValue(true);
|
||||
|
||||
final String fooDtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
System.out.println(fooDtoAsString);
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final BarDto dtoObject = new BarDto();
|
||||
dtoObject.setBooleanValue(true);
|
||||
|
||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
// tests - multiple entities to json
|
||||
|
||||
@Test
|
||||
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
|
||||
|
||||
System.out.println(dtosAsString);
|
||||
}
|
||||
|
||||
// tests - json to single entity
|
||||
|
||||
@Test
|
||||
public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
}
|
||||
|
||||
@Test(expected = UnrecognizedPropertyException.class)
|
||||
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
// tests - json to multiple entities
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Article Ideas:
|
||||
- Deserializing with a custom JsonParser
|
||||
|
||||
- Jackson Ignore: ignore specific fields at field level, at class level,
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package org.baeldung.jackson;
|
||||
|
||||
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 isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.baeldung.jackson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class MyDtoIgnoreUnkown {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoIgnoreUnkown() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoIgnoreUnkown(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 isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue