Merge remote-tracking branch 'upstream/master'

This commit is contained in:
tschiman 2017-02-11 21:51:44 -07:00
commit c7fa471064
6 changed files with 132 additions and 37 deletions

View File

@ -7,28 +7,32 @@ import java.util.stream.Stream;
public class JoinerSplitter {
public static String join ( String[] arrayOfString ) {
return Arrays.asList(arrayOfString)
public static String join(String[] arrayOfString) {
return Arrays
.asList(arrayOfString)
.stream()
.map(x -> x)
.collect(Collectors.joining(","));
}
public static String joinWithPrefixPostFix ( String[] arrayOfString ) {
return Arrays.asList(arrayOfString)
public static String joinWithPrefixPostFix(String[] arrayOfString) {
return Arrays
.asList(arrayOfString)
.stream()
.map(x -> x)
.collect(Collectors.joining(",","[","]"));
.collect(Collectors.joining(",", "[", "]"));
}
public static List<String> split ( String str ) {
return Stream.of(str.split(","))
.map (elem -> new String(elem))
public static List<String> split(String str) {
return Stream
.of(str.split(","))
.map(elem -> new String(elem))
.collect(Collectors.toList());
}
public static List<Character> splitToListOfChar ( String str ) {
return str.chars()
public static List<Character> splitToListOfChar(String str) {
return str
.chars()
.mapToObj(item -> (char) item)
.collect(Collectors.toList());
}

View File

@ -67,6 +67,12 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
@ -152,7 +158,7 @@
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependency>
</dependencies>
@ -189,7 +195,7 @@
<properties>
<!-- marshalling -->
<jackson.version>2.8.5</jackson.version>
<jackson.version>2.8.6</jackson.version>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>

View File

@ -4,6 +4,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
public class ActorJackson {
@ -53,7 +54,7 @@ public class ActorJackson {
}
private String formatDateOfBirth() {
final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return formatter.format(dateOfBirth);
}

View File

@ -0,0 +1,25 @@
package com.baeldung.jackson.miscellaneous.mixin;
import java.util.Optional;
public class Book {
private String title;
private Optional<String> subTitle;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Optional<String> getSubTitle() {
return subTitle;
}
public void setSubTitle(Optional<String> subTitle) {
this.subTitle = subTitle;
}
}

View File

@ -3,12 +3,10 @@ package com.baeldung.jackson.deserialization;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.baeldung.jackson.entities.Movie;
import org.junit.Assert;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.assertEquals;
public class JacksonDeserializeTest {
@ -20,7 +18,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class);
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);
assertEquals(expectedOutput, movie.toString());
}
@Test
@ -35,7 +33,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class);
final String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]";
Assert.assertEquals(movie.toString(), expectedOutput);
assertEquals(expectedOutput, movie.toString());
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.jackson.miscellaneous.mixin;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import static io.restassured.path.json.JsonPath.from;
import java.io.IOException;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class OptionalTypeTest {
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
@Test
public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
String subTitle = "The Parish Boy's Progress";
Book book = new Book();
book.setTitle("Oliver Twist");
book.setSubTitle(Optional.of(subTitle));
String result = mapper.writeValueAsString(book);
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
}
@Test
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
Book book = new Book();
book.setTitle("Oliver Twist");
book.setSubTitle(Optional.empty());
String result = mapper.writeValueAsString(book);
assertThat(from(result).getString("subTitle")).isNull();
}
@Test
public void givenField_whenDeserializingIntoOptional_thenIsPresentWithValue() throws IOException {
String subTitle = "The Parish Boy's Progress";
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }";
Book result = mapper.readValue(book, Book.class);
assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle));
}
@Test
public void givenNullField_whenDeserializingIntoOptional_thenIsEmpty() throws IOException {
String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }";
Book result = mapper.readValue(book, Book.class);
assertThat(result.getSubTitle()).isEmpty();
}
}