|
| 1 | +package org.baeldung.jackson.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.instanceOf; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.baeldung.jackson.ignore.MyDto; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.core.JsonParseException; |
| 14 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 15 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.fasterxml.jackson.databind.type.CollectionType; |
| 18 | +import com.google.common.collect.Lists; |
| 19 | + |
| 20 | +public class JacksonCollectionDeserializationUnitTest { |
| 21 | + |
| 22 | + // tests - json to multiple entity |
| 23 | + |
| 24 | + @Test |
| 25 | + public final void givenJsonArray_whenDeserializingAsArray_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 26 | + final ObjectMapper mapper = new ObjectMapper(); |
| 27 | + |
| 28 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 29 | + final String jsonArray = mapper.writeValueAsString(listOfDtos); |
| 30 | + // [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}] |
| 31 | + |
| 32 | + final MyDto[] asArray = mapper.readValue(jsonArray, MyDto[].class); |
| 33 | + assertThat(asArray[0], instanceOf(MyDto.class)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public final void givenJsonArray_whenDeserializingAsListWithNoTypeInfo_thenNotCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 38 | + final ObjectMapper mapper = new ObjectMapper(); |
| 39 | + |
| 40 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 41 | + final String jsonArray = mapper.writeValueAsString(listOfDtos); |
| 42 | + // [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}] |
| 43 | + |
| 44 | + final List<MyDto> asList = mapper.readValue(jsonArray, List.class); |
| 45 | + assertThat((Object) asList.get(0), instanceOf(LinkedHashMap.class)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public final void givenJsonArray_whenDeserializingAsListWithTypeReferenceHelp_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 50 | + final ObjectMapper mapper = new ObjectMapper(); |
| 51 | + |
| 52 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 53 | + final String jsonArray = mapper.writeValueAsString(listOfDtos); |
| 54 | + // [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}] |
| 55 | + |
| 56 | + final List<MyDto> asList = mapper.readValue(jsonArray, new TypeReference<List<MyDto>>() { |
| 57 | + }); |
| 58 | + assertThat(asList.get(0), instanceOf(MyDto.class)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public final void givenJsonArray_whenDeserializingAsListWithJavaTypeHelp_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 63 | + final ObjectMapper mapper = new ObjectMapper(); |
| 64 | + |
| 65 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 66 | + final String jsonArray = mapper.writeValueAsString(listOfDtos); |
| 67 | + // [{"stringValue":"a","intValue":1,"booleanValue":true},{"stringValue":"bc","intValue":3,"booleanValue":false}] |
| 68 | + |
| 69 | + final CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, MyDto.class); |
| 70 | + final List<MyDto> asList = mapper.readValue(jsonArray, javaType); |
| 71 | + assertThat(asList.get(0), instanceOf(MyDto.class)); |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | +// a (private) no-args constructor is required (simulate without) |
0 commit comments