Search in sources :

Example 1 with MovieWithNullValue

use of com.baeldung.jackson.entities.MovieWithNullValue in project tutorials by eugenp.

the class JacksonSerializeUnitTest method whenCustomSerialize_thenCorrect.

@Test
public void whenCustomSerialize_thenCorrect() throws ParseException, IOException {
    final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
    final MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
    final SimpleModule module = new SimpleModule();
    module.addSerializer(new ActorJacksonSerializer(ActorJackson.class));
    final ObjectMapper mapper = new ObjectMapper();
    final String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
    final Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
    final String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
    Assert.assertEquals(jsonResult, expectedOutput);
}
Also used : ActorJackson(com.baeldung.jackson.entities.ActorJackson) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MovieWithNullValue(com.baeldung.jackson.entities.MovieWithNullValue) SimpleDateFormat(java.text.SimpleDateFormat) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ActorJackson (com.baeldung.jackson.entities.ActorJackson)1 MovieWithNullValue (com.baeldung.jackson.entities.MovieWithNullValue)1 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Test (org.junit.Test)1