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,30 +7,34 @@ import java.util.stream.Stream;
public class JoinerSplitter { public class JoinerSplitter {
public static String join ( String[] arrayOfString ) { public static String join(String[] arrayOfString) {
return Arrays.asList(arrayOfString) return Arrays
.stream() .asList(arrayOfString)
.map(x -> x) .stream()
.collect(Collectors.joining(",")); .map(x -> x)
} .collect(Collectors.joining(","));
}
public static String joinWithPrefixPostFix ( String[] arrayOfString ) {
return Arrays.asList(arrayOfString) public static String joinWithPrefixPostFix(String[] arrayOfString) {
.stream() return Arrays
.map(x -> x) .asList(arrayOfString)
.collect(Collectors.joining(",","[","]")); .stream()
} .map(x -> x)
.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) {
.collect(Collectors.toList()); return Stream
} .of(str.split(","))
.map(elem -> new String(elem))
public static List<Character> splitToListOfChar ( String str ) { .collect(Collectors.toList());
return str.chars() }
.mapToObj(item -> (char) item)
.collect(Collectors.toList()); 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> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
@ -128,8 +134,8 @@
<version>${mockito.version}</version> <version>${mockito.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- logging --> <!-- logging -->
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
@ -152,7 +158,7 @@
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -189,7 +195,7 @@
<properties> <properties>
<!-- marshalling --> <!-- marshalling -->
<jackson.version>2.8.5</jackson.version> <jackson.version>2.8.6</jackson.version>
<!-- logging --> <!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version> <org.slf4j.version>1.7.21</org.slf4j.version>
@ -198,7 +204,7 @@
<!-- util --> <!-- util -->
<guava.version>19.0</guava.version> <guava.version>19.0</guava.version>
<commons-lang3.version>3.5</commons-lang3.version> <commons-lang3.version>3.5</commons-lang3.version>
<commons-io.version>2.5</commons-io.version> <commons-io.version>2.5</commons-io.version>
<joda-time.version>2.9.6</joda-time.version> <joda-time.version>2.9.6</joda-time.version>
<gson.version>2.8.0</gson.version> <gson.version>2.8.0</gson.version>
<commons-collections4.version>4.1</commons-collections4.version> <commons-collections4.version>4.1</commons-collections4.version>

View File

@ -4,6 +4,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
public class ActorJackson { public class ActorJackson {
@ -53,7 +54,7 @@ public class ActorJackson {
} }
private String formatDateOfBirth() { 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")); formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return formatter.format(dateOfBirth); 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.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import com.baeldung.jackson.entities.Movie; import com.baeldung.jackson.entities.Movie;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.assertEquals;
public class JacksonDeserializeTest { public class JacksonDeserializeTest {
@ -20,7 +18,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class); 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]]]]"; 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 @Test
@ -35,7 +33,7 @@ public class JacksonDeserializeTest {
final Movie movie = mapper.readValue(jsonInput, Movie.class); 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]]]]"; 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();
}
}