jackson work

This commit is contained in:
eugenp 2013-12-24 00:19:43 +02:00
parent 666f17e02c
commit 8118dd3c65
14 changed files with 87 additions and 25 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
public class MyDto { public class MyDto {

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.annotation.JsonFilter;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.ignore; package dtos;
import com.fasterxml.jackson.annotation.JsonIgnoreType; import com.fasterxml.jackson.annotation.JsonIgnoreType;

View File

@ -7,7 +7,6 @@ import java.io.IOException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.baeldung.jackson.ignore.MyDto;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; 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.fasterxml.jackson.databind.type.CollectionType;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import dtos.MyDto;
public class JacksonCollectionDeserializationUnitTest { public class JacksonCollectionDeserializationUnitTest {
// tests - json to multiple entity // tests - json to multiple entity

View File

@ -6,19 +6,21 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import org.baeldung.jackson.ignore.MyDto;
import org.baeldung.jackson.ignore.MyDtoIgnoreUnkown;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; 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 @Test
public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
@ -43,6 +45,8 @@ public class JacksonDeserializationUnitTest {
assertThat(readValue.isBooleanValue(), equalTo(true)); assertThat(readValue.isBooleanValue(), equalTo(true));
} }
// tests - json with unknown fields
@Test(expected = UnrecognizedPropertyException.class) @Test(expected = UnrecognizedPropertyException.class)
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException { public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
@ -91,4 +95,39 @@ public class JacksonDeserializationUnitTest {
assertThat(readValue.getIntValue(), equalTo(1)); 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"));
}
} }

View File

@ -7,14 +7,6 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.util.List; 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 org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include; 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.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.google.common.collect.Lists; 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 { public class JacksonSerializationUnitTest {
// tests - single entity to json // tests - single entity to json
@ -173,7 +174,7 @@ public class JacksonSerializationUnitTest {
} }
@Test @Test
public final void givenIgnoringNullFieldsOnClass_whenSerializingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException { public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull(); final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
@ -186,7 +187,7 @@ public class JacksonSerializationUnitTest {
} }
@Test @Test
public final void givenIgnoringNullFieldsGlobally_whenSerializingObjectWithNullField_thenFieldIsIgnroed() throws JsonProcessingException { public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL); mapper.setSerializationInclusion(Include.NON_NULL);
final MyDto dtoObject = new MyDto(); final MyDto dtoObject = new MyDto();