Search in sources :

Example 1 with ActorGsonDeserializer

use of org.baeldung.gson.serialization.ActorGsonDeserializer in project tutorials by eugenp.

the class GsonDeserializeUnitTest method whenCustomDeserialize_thenCorrect.

@Test
public void whenCustomDeserialize_thenCorrect() throws ParseException {
    final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    final Gson gson = new GsonBuilder().registerTypeAdapter(ActorGson.class, new ActorGsonDeserializer()).create();
    final Movie outputMovie = gson.fromJson(jsonInput, Movie.class);
    final String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorGson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
    Assert.assertEquals(outputMovie.toString(), expectedOutput);
}
Also used : Movie(org.baeldung.gson.entities.Movie) GsonBuilder(com.google.gson.GsonBuilder) ActorGson(org.baeldung.gson.entities.ActorGson) Gson(com.google.gson.Gson) ActorGson(org.baeldung.gson.entities.ActorGson) ActorGsonDeserializer(org.baeldung.gson.serialization.ActorGsonDeserializer) Test(org.junit.Test)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ActorGson (org.baeldung.gson.entities.ActorGson)1 Movie (org.baeldung.gson.entities.Movie)1 ActorGsonDeserializer (org.baeldung.gson.serialization.ActorGsonDeserializer)1 Test (org.junit.Test)1