minor fix
This commit is contained in:
parent
3bbb7c8945
commit
4906adb2ad
@ -1,7 +1,10 @@
|
|||||||
package org.baeldung.jackson.entities;
|
package org.baeldung.jackson.entities;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public class ActorJackson {
|
public class ActorJackson {
|
||||||
|
|
||||||
@ -22,7 +25,7 @@ public class ActorJackson {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ActorJackson [imdbId=" + imdbId + ", dateOfBirth=" + dateOfBirth + ", filmography=" + filmography + "]";
|
return "ActorJackson [imdbId=" + imdbId + ", dateOfBirth=" + formatDateOfBirth() + ", filmography=" + filmography + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getImdbId() {
|
public String getImdbId() {
|
||||||
@ -49,4 +52,9 @@ public class ActorJackson {
|
|||||||
this.filmography = filmography;
|
this.filmography = filmography;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String formatDateOfBirth() {
|
||||||
|
final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
|
||||||
|
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
|
return formatter.format(dateOfBirth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,40 @@
|
|||||||
package org.baeldung.jackson.deserialization;
|
package org.baeldung.jackson.deserialization;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.baeldung.jackson.entities.Movie;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
import org.baeldung.jackson.entities.Movie;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class JacksonDeserializeTest {
|
public class JacksonDeserializeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSimpleDeserialize_thenCorrect() throws IOException {
|
public void whenSimpleDeserialize_thenCorrect() throws IOException {
|
||||||
|
|
||||||
String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
final String jsonInput = "{\"imdbId\":\"tt0472043\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
Movie movie = mapper.readValue(jsonInput, Movie.class);
|
final Movie movie = mapper.readValue(jsonInput, Movie.class);
|
||||||
|
|
||||||
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]]]]";
|
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]]]]";
|
||||||
Assert.assertEquals(movie.toString(), expectedOutput);
|
Assert.assertEquals(movie.toString(), expectedOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCustomDeserialize_thenCorrect() throws IOException {
|
public void whenCustomDeserialize_thenCorrect() throws IOException {
|
||||||
|
|
||||||
String jsonInput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
final String jsonInput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"1982-09-21T12:00:00+01:00\",\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||||
mapper.setDateFormat(df);
|
mapper.setDateFormat(df);
|
||||||
|
|
||||||
Movie movie = mapper.readValue(jsonInput, Movie.class);
|
final Movie movie = mapper.readValue(jsonInput, Movie.class);
|
||||||
|
|
||||||
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]]]]";
|
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]]]]";
|
||||||
Assert.assertEquals(movie.toString(), expectedOutput);
|
Assert.assertEquals(movie.toString(), expectedOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package org.baeldung.jackson.serialization;
|
package org.baeldung.jackson.serialization;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -9,14 +8,11 @@ import java.util.Arrays;
|
|||||||
import org.baeldung.jackson.entities.ActorJackson;
|
import org.baeldung.jackson.entities.ActorJackson;
|
||||||
import org.baeldung.jackson.entities.Movie;
|
import org.baeldung.jackson.entities.Movie;
|
||||||
import org.baeldung.jackson.entities.MovieWithNullValue;
|
import org.baeldung.jackson.entities.MovieWithNullValue;
|
||||||
import org.baeldung.jackson.serialization.ActorJacksonSerializer;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
@ -26,34 +22,34 @@ public class JacksonSerializeTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenSimpleSerialize_thenCorrect() throws JsonProcessingException, ParseException {
|
public void whenSimpleSerialize_thenCorrect() throws JsonProcessingException, ParseException {
|
||||||
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
||||||
|
|
||||||
ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
|
final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
|
||||||
Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
|
final Movie movie = new Movie("tt0472043", "Mel Gibson", Arrays.asList(rudyYoungblood));
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
String jsonResult = mapper.writeValueAsString(movie);
|
final String jsonResult = mapper.writeValueAsString(movie);
|
||||||
|
|
||||||
String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401439600000,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
final String expectedOutput = "{\"imdbId\":\"tt0472043\",\"director\":\"Mel Gibson\",\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":401403600000,\"filmography\":[\"Apocalypto\",\"Beatdown\",\"Wind Walkers\"]}]}";
|
||||||
Assert.assertEquals(jsonResult, expectedOutput);
|
Assert.assertEquals(jsonResult, expectedOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCustomSerialize_thenCorrect() throws ParseException, IOException {
|
public void whenCustomSerialize_thenCorrect() throws ParseException, IOException {
|
||||||
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
||||||
|
|
||||||
ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
|
final ActorJackson rudyYoungblood = new ActorJackson("nm2199632", sdf.parse("21-09-1982"), Arrays.asList("Apocalypto", "Beatdown", "Wind Walkers"));
|
||||||
MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
|
final MovieWithNullValue movieWithNullValue = new MovieWithNullValue(null, "Mel Gibson", Arrays.asList(rudyYoungblood));
|
||||||
|
|
||||||
SimpleModule module = new SimpleModule();
|
final SimpleModule module = new SimpleModule();
|
||||||
module.addSerializer(new ActorJacksonSerializer(ActorJackson.class));
|
module.addSerializer(new ActorJacksonSerializer(ActorJackson.class));
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
|
final String jsonResult = mapper.registerModule(module).writer(new DefaultPrettyPrinter()).writeValueAsString(movieWithNullValue);
|
||||||
|
|
||||||
Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
|
final Object json = mapper.readValue("{\"actors\":[{\"imdbId\":\"nm2199632\",\"dateOfBirth\":\"21-09-1982\",\"N° Film: \":3,\"filmography\":\"Apocalypto-Beatdown-Wind Walkers\"}],\"imdbID\":null}", Object.class);
|
||||||
String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
|
final String expectedOutput = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(json);
|
||||||
|
|
||||||
Assert.assertEquals(jsonResult, expectedOutput);
|
Assert.assertEquals(jsonResult, expectedOutput);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user