use of com.baeldung.jackson.exception.User in project tutorials by eugenp.
the class JacksonExceptionsUnitTest method givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect.
@Test
public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{'id':1,'name':'John'}";
final JsonFactory factory = new JsonFactory();
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
final ObjectMapper mapper = new ObjectMapper(factory);
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}
use of com.baeldung.jackson.exception.User in project tutorials by eugenp.
the class JacksonExceptionsUnitTest method givenWrappedJsonString_whenDeserializing_thenException.
// JsonMappingException: Root name does not match expected
@Test(expected = JsonMappingException.class)
public void givenWrappedJsonString_whenDeserializing_thenException() throws IOException {
final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}";
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
}
use of com.baeldung.jackson.exception.User in project tutorials by eugenp.
the class JacksonExceptionsUnitTest method givenDefaultConstructor_whenDeserializing_thenCorrect.
@Test
public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\"}";
final ObjectMapper mapper = new ObjectMapper();
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}
use of com.baeldung.jackson.exception.User in project tutorials by eugenp.
the class JacksonExceptionsUnitTest method givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect.
@Test
public void givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}";
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
assertEquals("John", user.name);
}
Aggregations