jackson work
This commit is contained in:
parent
666f17e02c
commit
8118dd3c65
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
public class MyDto {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
@ -0,0 +1,21 @@
|
|||
package dtos;
|
||||
|
||||
public class MyDtoNoAccessors {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoNoAccessors() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoNoAccessors(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.jackson.ignore;
|
||||
package dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
|
@ -7,7 +7,6 @@ import java.io.IOException;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.jackson.ignore.MyDto;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
|
@ -17,6 +16,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import dtos.MyDto;
|
||||
|
||||
public class JacksonCollectionDeserializationUnitTest {
|
||||
|
||||
// tests - json to multiple entity
|
||||
|
|
|
@ -6,19 +6,21 @@ import static org.junit.Assert.assertThat;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.baeldung.jackson.ignore.MyDto;
|
||||
import org.baeldung.jackson.ignore.MyDtoIgnoreUnkown;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
|
||||
public class JacksonDeserializationUnitTest {
|
||||
import dtos.MyDto;
|
||||
import dtos.MyDtoIgnoreUnkown;
|
||||
|
||||
// tests - json to single entity
|
||||
public class JacksonDeserializationUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
|
@ -43,6 +45,8 @@ public class JacksonDeserializationUnitTest {
|
|||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
}
|
||||
|
||||
// tests - json with unknown fields
|
||||
|
||||
@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\"}";
|
||||
|
@ -91,4 +95,39 @@ public class JacksonDeserializationUnitTest {
|
|||
assertThat(readValue.getIntValue(), equalTo(1));
|
||||
}
|
||||
|
||||
// to JsonNode
|
||||
|
||||
@Test
|
||||
public final void whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException {
|
||||
final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final JsonNode actualObj = mapper.readTree(jsonString);
|
||||
|
||||
assertNotNull(actualObj);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingLowLevelDetails_whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException {
|
||||
final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final JsonFactory factory = mapper.getFactory();
|
||||
final JsonParser parser = factory.createParser(jsonString);
|
||||
final JsonNode actualObj = mapper.readTree(parser);
|
||||
|
||||
assertNotNull(actualObj);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenTheJsonNode_whenRetrievingDataFromId_thenCorrect() throws JsonParseException, IOException {
|
||||
final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final JsonNode actualObj = mapper.readTree(jsonString);
|
||||
|
||||
// When
|
||||
final JsonNode jsonNode1 = actualObj.get("k1");
|
||||
assertThat(jsonNode1.textValue(), equalTo("v1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,14 +7,6 @@ import static org.junit.Assert.assertThat;
|
|||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.jackson.ignore.MyDto;
|
||||
import org.baeldung.jackson.ignore.MyDtoFieldNameChanged;
|
||||
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
|
||||
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
|
||||
import org.baeldung.jackson.ignore.MyDtoIgnoreNull;
|
||||
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
|
||||
import org.baeldung.jackson.ignore.MyDtoWithFilter;
|
||||
import org.baeldung.jackson.ignore.MyMixInForString;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
@ -31,6 +23,15 @@ import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
|||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import dtos.MyDto;
|
||||
import dtos.MyDtoFieldNameChanged;
|
||||
import dtos.MyDtoIgnoreField;
|
||||
import dtos.MyDtoIgnoreFieldByName;
|
||||
import dtos.MyDtoIgnoreNull;
|
||||
import dtos.MyDtoIncludeNonDefault;
|
||||
import dtos.MyDtoWithFilter;
|
||||
import dtos.MyMixInForString;
|
||||
|
||||
public class JacksonSerializationUnitTest {
|
||||
|
||||
// tests - single entity to json
|
||||
|
@ -173,7 +174,7 @@ public class JacksonSerializationUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public final void givenIgnoringNullFieldsOnClass_whenSerializingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
|
||||
public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
||||
|
||||
|
@ -186,7 +187,7 @@ public class JacksonSerializationUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public final void givenIgnoringNullFieldsGlobally_whenSerializingObjectWithNullField_thenFieldIsIgnroed() throws JsonProcessingException {
|
||||
public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
final MyDto dtoObject = new MyDto();
|
||||
|
|
Loading…
Reference in New Issue