diff --git a/kotlin-libraries-2/.gitignore b/kotlin-libraries-2/.gitignore new file mode 100644 index 0000000000..0c017e8f8c --- /dev/null +++ b/kotlin-libraries-2/.gitignore @@ -0,0 +1,14 @@ +/bin/ + +#ignore gradle +.gradle/ + + +#ignore build and generated files +build/ +node/ +out/ + +#ignore installed node modules and package lock file +node_modules/ +package-lock.json diff --git a/kotlin-libraries-2/README.md b/kotlin-libraries-2/README.md new file mode 100644 index 0000000000..ff12555376 --- /dev/null +++ b/kotlin-libraries-2/README.md @@ -0,0 +1 @@ +## Relevant articles: diff --git a/kotlin-libraries-2/pom.xml b/kotlin-libraries-2/pom.xml new file mode 100644 index 0000000000..e2e261cb1b --- /dev/null +++ b/kotlin-libraries-2/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + kotlin-libraries-2 + kotlin-libraries-2 + jar + + + com.baeldung + parent-kotlin + 1.0.0-SNAPSHOT + ../parent-kotlin + + + + + com.fasterxml.jackson.module + jackson-module-kotlin + + + junit + junit + test + + + + + + + + + + diff --git a/kotlin-libraries-2/resources/logback.xml b/kotlin-libraries-2/resources/logback.xml new file mode 100644 index 0000000000..9452207268 --- /dev/null +++ b/kotlin-libraries-2/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + \ No newline at end of file diff --git a/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt new file mode 100644 index 0000000000..4ff47ea987 --- /dev/null +++ b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Book.kt @@ -0,0 +1,8 @@ +package com.baeldung.kotlin.jackson + +import com.fasterxml.jackson.annotation.* + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +data class Book(var title: String, @JsonProperty("author") var authorName: String) { + var genres: List? = emptyList() +} \ No newline at end of file diff --git a/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt new file mode 100644 index 0000000000..445b6013d5 --- /dev/null +++ b/kotlin-libraries-2/src/main/kotlin/com/baeldung/kotlin/jackson/Movie.kt @@ -0,0 +1,3 @@ +package com.baeldung.kotlin.jackson + +data class Movie(var name: String, var studio: String, var rating: Float? = 1f) \ No newline at end of file diff --git a/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt b/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt new file mode 100644 index 0000000000..84171d9019 --- /dev/null +++ b/kotlin-libraries-2/src/test/kotlin/com/baeldung/kotlin/jackson/JacksonUnitTest.kt @@ -0,0 +1,114 @@ +package com.baeldung.kotlin.jackson + +import org.junit.Test +import kotlin.test.assertTrue +import kotlin.test.assertFalse +import kotlin.test.assertEquals +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.KotlinModule + +class JacksonUnitTest { + //val mapper = jacksonObjectMapper() + val mapper = ObjectMapper().registerModule(KotlinModule()) + + + @Test + fun whenSerializeMovie_thenSuccess() { + val movie = Movie("Endgame", "Marvel", 9.2f) + val serialized = mapper.writeValueAsString(movie) + + val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeMovie_thenSuccess() { + val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}""" + // val movie: Movie = mapper.readValue(json) + val movie = mapper.readValue(json) + + assertEquals(movie.name, "Endgame") + assertEquals(movie.studio, "Marvel") + assertEquals(movie.rating, 9.2f) + } + + @Test + fun whenDeserializeMovieWithMissingValue_thenUseDefaultValue() { + val json = """{"name":"Endgame","studio":"Marvel"}""" + val movie: Movie = mapper.readValue(json) + + assertEquals(movie.name, "Endgame") + assertEquals(movie.studio, "Marvel") + assertEquals(movie.rating, 1f) + } + + @Test + fun whenSerializeMap_thenSuccess() { + val map = mapOf(1 to "one", 2 to "two") + val serialized = mapper.writeValueAsString(map) + + val json = """{"1":"one","2":"two"}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeMap_thenSuccess() { + val json = """{"1":"one","2":"two"}""" + val aMap: Map = mapper.readValue(json) + + assertEquals(aMap[1], "one") + assertEquals(aMap[2], "two") + } + + @Test + fun whenSerializeList_thenSuccess() { + val movie1 = Movie("Endgame", "Marvel", 9.2f) + val movie2 = Movie("Shazam", "Warner Bros", 7.6f) + val movieList = listOf(movie1, movie2) + val serialized = mapper.writeValueAsString(movieList) + + val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeList_thenSuccess() { + val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]""" + val movieList: List = mapper.readValue(json) + + val movie1 = Movie("Endgame", "Marvel", 9.2f) + val movie2 = Movie("Shazam", "Warner Bros", 7.6f) + assertTrue(movieList.contains(movie1)) + assertTrue(movieList.contains(movie2)) + } + + @Test + fun whenSerializeBook_thenSuccess() { + val book = Book("Oliver Twist", "Charles Dickens") + val serialized = mapper.writeValueAsString(book) + + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + assertEquals(serialized, json) + } + + @Test + fun whenDeserializeBook_thenSuccess() { + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + val book: Book = mapper.readValue(json) + + assertEquals(book.title, "Oliver Twist") + assertEquals(book.authorName, "Charles Dickens") + } + + @Test + fun givenJsonInclude_whenSerializeBook_thenEmptyFieldExcluded() { + val book = Book("Oliver Twist", "Charles Dickens") + val serialized = mapper.writeValueAsString(book) + + val json = """{"title":"Oliver Twist","author":"Charles Dickens"}""" + assertEquals(serialized, json) + } + +} \ No newline at end of file