Search in sources :

Example 1 with Movie

use of org.baeldung.gson.entities.Movie in project tutorials by eugenp.

the class GsonSerializeUnitTest method whenSimpleSerialize_thenCorrect.

@Test
public void whenSimpleSerialize_thenCorrect() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    ActorGson rudyYoungblood = new ActorGson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
    Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
    String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"Sep 21, 1982 12:00:00 AM\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    Assert.assertEquals(new Gson().toJson(movie), expectedOutput);
}
Also used : Movie(org.baeldung.gson.entities.Movie) ActorGson(org.baeldung.gson.entities.ActorGson) ActorGson(org.baeldung.gson.entities.ActorGson) Gson(com.google.gson.Gson) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 2 with Movie

use of org.baeldung.gson.entities.Movie in project tutorials by eugenp.

the class GsonDeserializeUnitTest method whenSimpleDeserialize_thenCorrect.

@Test
public void whenSimpleDeserialize_thenCorrect() throws ParseException {
    final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"Tue Sep 21 11:00:00 GMT 1982\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
    final Gson gson = new GsonBuilder().setDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").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) Test(org.junit.Test)

Example 3 with Movie

use of org.baeldung.gson.entities.Movie 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)3 ActorGson (org.baeldung.gson.entities.ActorGson)3 Movie (org.baeldung.gson.entities.Movie)3 Test (org.junit.Test)3 GsonBuilder (com.google.gson.GsonBuilder)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ActorGsonDeserializer (org.baeldung.gson.serialization.ActorGsonDeserializer)1