|
1 | 1 | package org.baeldung.gson.deserialization; |
2 | 2 |
|
3 | | -import com.google.gson.Gson; |
4 | | -import com.google.gson.GsonBuilder; |
| 3 | +import java.text.ParseException; |
| 4 | + |
5 | 5 | import org.baeldung.gson.entities.ActorGson; |
6 | 6 | import org.baeldung.gson.entities.Movie; |
7 | 7 | import org.baeldung.gson.serialization.ActorGsonDeserializer; |
8 | 8 | import org.junit.Assert; |
9 | 9 | import org.junit.Test; |
10 | 10 |
|
11 | | -import java.text.ParseException; |
| 11 | +import com.google.gson.Gson; |
| 12 | +import com.google.gson.GsonBuilder; |
12 | 13 |
|
13 | 14 | public class GsonDeserializeTest { |
14 | | - |
15 | 15 | @Test |
16 | 16 | public void whenSimpleDeserialize_thenCorrect() throws ParseException { |
17 | 17 |
|
18 | | - String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}"; |
| 18 | + final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"Tue Sep 21 11:00:00 GMT 1982\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}"; |
| 19 | + |
| 20 | + final Gson gson = new GsonBuilder().setDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").create(); |
19 | 21 |
|
20 | | - Movie outputMovie = new Gson().fromJson(jsonInput, Movie.class); |
| 22 | + final Movie outputMovie = gson.fromJson(jsonInput, Movie.class); |
21 | 23 |
|
22 | | - String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorGson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 04:00:00 PDT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]"; |
| 24 | + 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]]]]"; |
23 | 25 | Assert.assertEquals(outputMovie.toString(), expectedOutput); |
24 | 26 | } |
25 | 27 |
|
26 | 28 | @Test |
27 | 29 | public void whenCustomDeserialize_thenCorrect() throws ParseException { |
28 | 30 |
|
29 | | - String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}"; |
| 31 | + final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":" + "[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":" + "[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}"; |
30 | 32 |
|
31 | | - Gson gson = new GsonBuilder().registerTypeAdapter(ActorGson.class, new ActorGsonDeserializer()).create(); |
| 33 | + final Gson gson = new GsonBuilder().registerTypeAdapter(ActorGson.class, new ActorGsonDeserializer()).create(); |
32 | 34 |
|
33 | | - Movie outputMovie = gson.fromJson(jsonInput, Movie.class); |
| 35 | + final Movie outputMovie = gson.fromJson(jsonInput, Movie.class); |
34 | 36 |
|
35 | | - String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorGson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 12:00:00 PDT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]"; |
| 37 | + 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]]]]"; |
36 | 38 | Assert.assertEquals(outputMovie.toString(), expectedOutput); |
37 | 39 | } |
38 | 40 | } |
0 commit comments