|
| 1 | +package org.baeldung.jackson.test; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.containsString; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.baeldung.jackson.dtos.User; |
| 11 | +import org.baeldung.jackson.exception.UserWithPrivateFields; |
| 12 | +import org.baeldung.jackson.exception.UserWithRoot; |
| 13 | +import org.baeldung.jackson.exception.Zoo; |
| 14 | +import org.baeldung.jackson.exception.ZooConfigured; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
| 18 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 19 | +import com.fasterxml.jackson.core.JsonFactory; |
| 20 | +import com.fasterxml.jackson.core.JsonParseException; |
| 21 | +import com.fasterxml.jackson.core.JsonParser; |
| 22 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 24 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 25 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 28 | + |
| 29 | +public class JacksonExceptionsTest { |
| 30 | + |
| 31 | + // JsonMappingException: Can not construct instance of |
| 32 | + @Test(expected = JsonMappingException.class) |
| 33 | + public void givenAbstractClass_whenDeserializing_thenException() throws IOException { |
| 34 | + final String json = "{\"animal\":{\"name\":\"lacy\"}}"; |
| 35 | + final ObjectMapper mapper = new ObjectMapper(); |
| 36 | + |
| 37 | + mapper.reader().withType(Zoo.class).readValue(json); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenAbstractClassConfigured_whenDeserializing_thenCorrect() throws IOException { |
| 42 | + final String json = "{\"animal\":{\"name\":\"lacy\"}}"; |
| 43 | + final ObjectMapper mapper = new ObjectMapper(); |
| 44 | + |
| 45 | + mapper.reader().withType(ZooConfigured.class).readValue(json); |
| 46 | + } |
| 47 | + |
| 48 | + // JsonMappingException: No serializer found for class |
| 49 | + @Test(expected = JsonMappingException.class) |
| 50 | + public void givenClassWithPrivateFields_whenSerializing_thenException() throws IOException { |
| 51 | + final UserWithPrivateFields user = new UserWithPrivateFields(1, "John"); |
| 52 | + |
| 53 | + final ObjectMapper mapper = new ObjectMapper(); |
| 54 | + mapper.writer().writeValueAsString(user); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenClassWithPrivateFields_whenConfigureSerializing_thenCorrect() throws IOException { |
| 59 | + final UserWithPrivateFields user = new UserWithPrivateFields(1, "John"); |
| 60 | + |
| 61 | + final ObjectMapper mapper = new ObjectMapper(); |
| 62 | + mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); |
| 63 | + |
| 64 | + final String result = mapper.writer().writeValueAsString(user); |
| 65 | + assertThat(result, containsString("John")); |
| 66 | + } |
| 67 | + |
| 68 | + // JsonMappingException: No suitable constructor found |
| 69 | + @Test(expected = JsonMappingException.class) |
| 70 | + public void givenNoDefaultConstructor_whenDeserializing_thenException() throws IOException { |
| 71 | + final String json = "{\"id\":1,\"name\":\"John\"}"; |
| 72 | + final ObjectMapper mapper = new ObjectMapper(); |
| 73 | + |
| 74 | + mapper.reader().withType(org.baeldung.jackson.exception.User.class).readValue(json); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException { |
| 79 | + final String json = "{\"id\":1,\"name\":\"John\"}"; |
| 80 | + final ObjectMapper mapper = new ObjectMapper(); |
| 81 | + |
| 82 | + final User user = mapper.reader().withType(User.class).readValue(json); |
| 83 | + assertEquals("John", user.name); |
| 84 | + } |
| 85 | + |
| 86 | + // JsonMappingException: Root name does not match expected |
| 87 | + @Test(expected = JsonMappingException.class) |
| 88 | + public void givenWrappedJsonString_whenDeserializing_thenException() throws IOException { |
| 89 | + final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}"; |
| 90 | + |
| 91 | + final ObjectMapper mapper = new ObjectMapper(); |
| 92 | + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); |
| 93 | + |
| 94 | + mapper.reader().withType(User.class).readValue(json); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenWrappedJsonStringAndConfigureClass_whenDeserializing_thenCorrect() throws IOException { |
| 99 | + final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}"; |
| 100 | + |
| 101 | + final ObjectMapper mapper = new ObjectMapper(); |
| 102 | + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); |
| 103 | + |
| 104 | + final UserWithRoot user = mapper.reader().withType(UserWithRoot.class).readValue(json); |
| 105 | + assertEquals("John", user.name); |
| 106 | + } |
| 107 | + |
| 108 | + // JsonMappingException: Can not deserialize instance of |
| 109 | + @Test(expected = JsonMappingException.class) |
| 110 | + public void givenJsonOfArray_whenDeserializing_thenException() throws JsonProcessingException, IOException { |
| 111 | + final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; |
| 112 | + final ObjectMapper mapper = new ObjectMapper(); |
| 113 | + |
| 114 | + mapper.reader().withType(User.class).readValue(json); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void givenJsonOfArray_whenDeserializing_thenCorrect() throws JsonProcessingException, IOException { |
| 119 | + final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; |
| 120 | + final ObjectMapper mapper = new ObjectMapper(); |
| 121 | + |
| 122 | + final List<User> users = mapper.reader().withType(new TypeReference<List<User>>() { |
| 123 | + }).readValue(json); |
| 124 | + |
| 125 | + assertEquals(2, users.size()); |
| 126 | + } |
| 127 | + |
| 128 | + // UnrecognizedPropertyException |
| 129 | + @Test(expected = UnrecognizedPropertyException.class) |
| 130 | + public void givenJsonStringWithExtra_whenDeserializing_thenException() throws IOException { |
| 131 | + final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}"; |
| 132 | + |
| 133 | + final ObjectMapper mapper = new ObjectMapper(); |
| 134 | + mapper.reader().withType(User.class).readValue(json); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect() throws IOException { |
| 139 | + final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}"; |
| 140 | + |
| 141 | + final ObjectMapper mapper = new ObjectMapper(); |
| 142 | + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 143 | + |
| 144 | + final User user = mapper.reader().withType(User.class).readValue(json); |
| 145 | + assertEquals("John", user.name); |
| 146 | + } |
| 147 | + |
| 148 | + // JsonParseException: Unexpected character (''' (code 39)) |
| 149 | + @Test(expected = JsonParseException.class) |
| 150 | + public void givenStringWithSingleQuotes_whenDeserializing_thenException() throws JsonProcessingException, IOException { |
| 151 | + final String json = "{'id':1,'name':'John'}"; |
| 152 | + final ObjectMapper mapper = new ObjectMapper(); |
| 153 | + |
| 154 | + mapper.reader().withType(User.class).readValue(json); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException { |
| 159 | + final String json = "{'id':1,'name':'John'}"; |
| 160 | + |
| 161 | + final JsonFactory factory = new JsonFactory(); |
| 162 | + factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); |
| 163 | + final ObjectMapper mapper = new ObjectMapper(factory); |
| 164 | + |
| 165 | + final User user = mapper.reader().withType(User.class).readValue(json); |
| 166 | + assertEquals("John", user.name); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +} |
0 commit comments