Search in sources :

Example 1 with MovieWithNullValue

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

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonParser (com.google.gson.JsonParser)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ActorGson (org.baeldung.gson.entities.ActorGson)1 MovieWithNullValue (org.baeldung.gson.entities.MovieWithNullValue)1 Test (org.junit.Test)1