Search in sources :

Example 1 with Movie

use of com.baeldung.jackson.entities.Movie in project tutorials by eugenp.

the class JacksonDeserializeUnitTest method whenSimpleDeserialize_thenCorrect.

@Test
public void whenSimpleDeserialize_thenCorrect() throws IOException {
    final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    final ObjectMapper mapper = new ObjectMapper();
    final Movie movie = mapper.readValue(jsonInput, Movie.class);
    final String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
    assertEquals(expectedOutput, movie.toString());
}
Also used : Movie(com.baeldung.jackson.entities.Movie) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with Movie

use of com.baeldung.jackson.entities.Movie in project tutorials by eugenp.

the class JacksonDeserializeUnitTest method whenCustomDeserialize_thenCorrect.

@Test
public void whenCustomDeserialize_thenCorrect() throws IOException {
    final String jsonInput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    final ObjectMapper mapper = new ObjectMapper();
    final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    mapper.setDateFormat(df);
    final Movie movie = mapper.readValue(jsonInput, Movie.class);
    final String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
    assertEquals(expectedOutput, movie.toString());
}
Also used : Movie(com.baeldung.jackson.entities.Movie) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Movie

use of com.baeldung.jackson.entities.Movie in project tutorials by eugenp.

the class JacksonSerializeUnitTest method whenSimpleSerialize_thenCorrect.

@Test
public void whenSimpleSerialize_thenCorrect() throws JsonProcessingException, ParseException {
    final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
    final Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
    final ObjectMapper mapper = new ObjectMapper();
    final String jsonResult = mapper.writeValueAsString(movie);
    final String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401414400000,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    Assert.assertEquals(jsonResult, expectedOutput);
}
Also used : ActorJackson(com.baeldung.jackson.entities.ActorJackson) Movie(com.baeldung.jackson.entities.Movie) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

Movie (com.baeldung.jackson.entities.Movie)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Test (org.junit.Test)3 SimpleDateFormat (java.text.SimpleDateFormat)2 ActorJackson (com.baeldung.jackson.entities.ActorJackson)1 DateFormat (java.text.DateFormat)1