Search in sources :

Example 1 with ActorGson

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

the class ActorGsonDeserializer method deserialize.

@Override
public ActorGson deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    final JsonElement jsonImdbId = jsonObject.get("imdbId");
    final JsonElement jsonDateOfBirth = jsonObject.get("dateOfBirth");
    final JsonArray jsonFilmography = jsonObject.getAsJsonArray("filmography");
    final ArrayList<String> filmList = new ArrayList<String>();
    if (jsonFilmography != null) {
        for (int i = 0; i < jsonFilmography.size(); i++) {
            filmList.add(jsonFilmography.get(i).getAsString());
        }
    }
    ActorGson actorGson = null;
    try {
        actorGson = new ActorGson(jsonImdbId.getAsString(), sdf.parse(jsonDateOfBirth.getAsString()), filmList);
    } catch (final ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return actorGson;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ActorGson(org.baeldung.gson.entities.ActorGson) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException)

Example 2 with ActorGson

use of org.baeldung.gson.entities.ActorGson 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 3 with ActorGson

use of org.baeldung.gson.entities.ActorGson 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 4 with ActorGson

use of org.baeldung.gson.entities.ActorGson 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)

Example 5 with ActorGson

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

the class GsonSerializeUnitTest method whenCustomSerialize_thenCorrect.

@Test
public void whenCustomSerialize_thenCorrect() throws ParseException {
    Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().serializeNulls().disableHtmlEscaping().registerTypeAdapter(ActorGson.class, new ActorGsonSerializer()).create();
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    ActorGson rudyYoungblood = new ActorGson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
    MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
    String expectedOutput = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create().toJson(new JsonParser().parse("{\"imdbId\":null,\"actors\":[{\"<strong>IMDB Code</strong>\":\"nm2199632\",\"<strong>Date Of Birth</strong>\":\"21-09-1982\",\"<strong>N° Film:</strong> \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}]}"));
    Assert.assertEquals(gson.toJson(movieWithNullValue), expectedOutput);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ActorGson(org.baeldung.gson.entities.ActorGson) Gson(com.google.gson.Gson) ActorGson(org.baeldung.gson.entities.ActorGson) MovieWithNullValue(org.baeldung.gson.entities.MovieWithNullValue) SimpleDateFormat(java.text.SimpleDateFormat) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Aggregations

ActorGson (org.baeldung.gson.entities.ActorGson)5 Gson (com.google.gson.Gson)4 Test (org.junit.Test)4 GsonBuilder (com.google.gson.GsonBuilder)3 Movie (org.baeldung.gson.entities.Movie)3 SimpleDateFormat (java.text.SimpleDateFormat)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonParser (com.google.gson.JsonParser)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 MovieWithNullValue (org.baeldung.gson.entities.MovieWithNullValue)1 ActorGsonDeserializer (org.baeldung.gson.serialization.ActorGsonDeserializer)1