Skip to content

Commit 4906adb

Browse files
committed
minor fix
1 parent 3bbb7c8 commit 4906adb

3 files changed

Lines changed: 36 additions & 31 deletions

File tree

‎jackson/src/main/java/org/baeldung/jackson/entities/ActorJackson.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.baeldung.jackson.entities;
22

3+
import java.text.DateFormat;
4+
import java.text.SimpleDateFormat;
35
import java.util.Date;
46
import java.util.List;
7+
import java.util.TimeZone;
58

69
public class ActorJackson {
710

@@ -22,7 +25,7 @@ public ActorJackson(String imdbId, Date dateOfBirth, List<String> filmography) {
2225

2326
@Override
2427
public String toString() {
25-
return "ActorJackson [imdbId=" + imdbId + ", dateOfBirth=" + dateOfBirth + ", filmography=" + filmography + "]";
28+
return "ActorJackson [imdbId=" + imdbId + ", dateOfBirth=" + formatDateOfBirth() + ", filmography=" + filmography + "]";
2629
}
2730

2831
public String getImdbId() {
@@ -49,4 +52,9 @@ public void setFilmography(List<String> filmography) {
4952
this.filmography = filmography;
5053
}
5154

55+
private String formatDateOfBirth() {
56+
final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
57+
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
58+
return formatter.format(dateOfBirth);
59+
}
5260
}

‎jackson/src/test/java/org/baeldung/jackson/deserialization/JacksonDeserializeTest.java‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
package org.baeldung.jackson.deserialization;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import java.io.IOException;
4+
import java.text.DateFormat;
5+
import java.text.SimpleDateFormat;
6+
47
import org.baeldung.jackson.entities.Movie;
58
import org.junit.Assert;
69
import org.junit.Test;
710

8-
import java.io.IOException;
9-
import java.text.DateFormat;
10-
import java.text.SimpleDateFormat;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
1112

1213
public class JacksonDeserializeTest {
1314

1415
@Test
1516
public void whenSimpleDeserialize_thenCorrect() throws IOException {
1617

17-
String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
18-
ObjectMapper mapper = new ObjectMapper();
19-
Movie movie = mapper.readValue(jsonInput, Movie.class);
18+
final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
19+
final ObjectMapper mapper = new ObjectMapper();
20+
final Movie movie = mapper.readValue(jsonInput, Movie.class);
2021

21-
String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 04:00:00 PDT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
22+
final String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
2223
Assert.assertEquals(movie.toString(), expectedOutput);
2324
}
2425

2526
@Test
2627
public void whenCustomDeserialize_thenCorrect() throws IOException {
2728

28-
String jsonInput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
29+
final String jsonInput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
2930

30-
ObjectMapper mapper = new ObjectMapper();
31+
final ObjectMapper mapper = new ObjectMapper();
3132
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
3233
mapper.setDateFormat(df);
3334

34-
Movie movie = mapper.readValue(jsonInput, Movie.class);
35+
final Movie movie = mapper.readValue(jsonInput, Movie.class);
3536

36-
String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 12:00:00 PDT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
37+
final String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 09:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
3738
Assert.assertEquals(movie.toString(), expectedOutput);
3839
}
3940

‎jackson/src/test/java/org/baeldung/jackson/serialization/JacksonSerializeTest.java‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package org.baeldung.jackson.serialization;
22

33
import java.io.IOException;
4-
import java.text.DateFormat;
54
import java.text.ParseException;
65
import java.text.SimpleDateFormat;
76
import java.util.Arrays;
87

98
import org.baeldung.jackson.entities.ActorJackson;
109
import org.baeldung.jackson.entities.Movie;
1110
import org.baeldung.jackson.entities.MovieWithNullValue;
12-
import org.baeldung.jackson.serialization.ActorJacksonSerializer;
1311
import org.junit.Assert;
1412
import org.junit.Test;
1513

16-
import com.fasterxml.jackson.core.JsonParseException;
1714
import com.fasterxml.jackson.core.JsonProcessingException;
1815
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
19-
import com.fasterxml.jackson.databind.JsonMappingException;
2016
import com.fasterxml.jackson.databind.ObjectMapper;
2117
import com.fasterxml.jackson.databind.SerializationFeature;
2218
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -26,34 +22,34 @@ public class JacksonSerializeTest {
2622
@Test
2723
public void whenSimpleSerialize_thenCorrect() throws JsonProcessingException, ParseException {
2824

29-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
25+
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
3026

31-
ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
32-
Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
27+
final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
28+
final Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
3329

34-
ObjectMapper mapper = new ObjectMapper();
35-
String jsonResult = mapper.writeValueAsString(movie);
30+
final ObjectMapper mapper = new ObjectMapper();
31+
final String jsonResult = mapper.writeValueAsString(movie);
3632

37-
String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401439600000,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
33+
final String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401403600000,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
3834
Assert.assertEquals(jsonResult, expectedOutput);
3935
}
4036

4137
@Test
4238
public void whenCustomSerialize_thenCorrect() throws ParseException, IOException {
4339

44-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
40+
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
4541

46-
ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
47-
MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
42+
final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
43+
final MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
4844

49-
SimpleModule module = new SimpleModule();
45+
final SimpleModule module = new SimpleModule();
5046
module.addSerializer(new ActorJacksonSerializer(ActorJackson.class));
51-
ObjectMapper mapper = new ObjectMapper();
47+
final ObjectMapper mapper = new ObjectMapper();
5248

53-
String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
49+
final String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
5450

55-
Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
56-
String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
51+
final Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
52+
final String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
5753

5854
Assert.assertEquals(jsonResult, expectedOutput);
5955
}

0 commit comments

Comments
 (0)