Search in sources :

Example 1 with User

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);
}
Also used : User(com.baeldung.jackson.exception.User) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with User

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);
}
Also used : User(com.baeldung.jackson.exception.User) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with User

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);
}
Also used : User(com.baeldung.jackson.exception.User) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with User

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);
}
Also used : User(com.baeldung.jackson.exception.User) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

User (com.baeldung.jackson.exception.User)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1