Skip to content

Commit f8d8ef6

Browse files
committed
jackson support for kotlin
1 parent febda47 commit f8d8ef6

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

kotlin-libraries-2/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/bin/
2+
3+
#ignore gradle
4+
.gradle/
5+
6+
7+
#ignore build and generated files
8+
build/
9+
node/
10+
out/
11+
12+
#ignore installed node modules and package lock file
13+
node_modules/
14+
package-lock.json

kotlin-libraries-2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Relevant articles:

kotlin-libraries-2/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>kotlin-libraries-2</artifactId>
6+
<name>kotlin-libraries-2</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-kotlin</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<relativePath>../parent-kotlin</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.fasterxml.jackson.module</groupId>
19+
<artifactId>jackson-module-kotlin</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
28+
29+
</dependencies>
30+
31+
<properties>
32+
</properties>
33+
34+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
</configuration>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.kotlin.jackson
2+
3+
import com.fasterxml.jackson.annotation.*
4+
5+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
6+
data class Book(var title: String, @JsonProperty("author") var authorName: String) {
7+
var genres: List<String>? = emptyList()
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.jackson
2+
3+
data class Movie(var name: String, var studio: String, var rating: Float? = 1f)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.baeldung.kotlin.jackson
2+
3+
import org.junit.Test
4+
import kotlin.test.assertTrue
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertEquals
7+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
8+
import com.fasterxml.jackson.module.kotlin.readValue
9+
import com.fasterxml.jackson.databind.ObjectMapper
10+
import com.fasterxml.jackson.module.kotlin.KotlinModule
11+
12+
class JacksonUnitTest {
13+
//val mapper = jacksonObjectMapper()
14+
val mapper = ObjectMapper().registerModule(KotlinModule())
15+
16+
17+
@Test
18+
fun whenSerializeMovie_thenSuccess() {
19+
val movie = Movie("Endgame", "Marvel", 9.2f)
20+
val serialized = mapper.writeValueAsString(movie)
21+
22+
val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}"""
23+
assertEquals(serialized, json)
24+
}
25+
26+
@Test
27+
fun whenDeserializeMovie_thenSuccess() {
28+
val json = """{"name":"Endgame","studio":"Marvel","rating":9.2}"""
29+
// val movie: Movie = mapper.readValue(json)
30+
val movie = mapper.readValue<Movie>(json)
31+
32+
assertEquals(movie.name, "Endgame")
33+
assertEquals(movie.studio, "Marvel")
34+
assertEquals(movie.rating, 9.2f)
35+
}
36+
37+
@Test
38+
fun whenDeserializeMovieWithMissingValue_thenUseDefaultValue() {
39+
val json = """{"name":"Endgame","studio":"Marvel"}"""
40+
val movie: Movie = mapper.readValue(json)
41+
42+
assertEquals(movie.name, "Endgame")
43+
assertEquals(movie.studio, "Marvel")
44+
assertEquals(movie.rating, 1f)
45+
}
46+
47+
@Test
48+
fun whenSerializeMap_thenSuccess() {
49+
val map = mapOf(1 to "one", 2 to "two")
50+
val serialized = mapper.writeValueAsString(map)
51+
52+
val json = """{"1":"one","2":"two"}"""
53+
assertEquals(serialized, json)
54+
}
55+
56+
@Test
57+
fun whenDeserializeMap_thenSuccess() {
58+
val json = """{"1":"one","2":"two"}"""
59+
val aMap: Map<Int,String> = mapper.readValue(json)
60+
61+
assertEquals(aMap[1], "one")
62+
assertEquals(aMap[2], "two")
63+
}
64+
65+
@Test
66+
fun whenSerializeList_thenSuccess() {
67+
val movie1 = Movie("Endgame", "Marvel", 9.2f)
68+
val movie2 = Movie("Shazam", "Warner Bros", 7.6f)
69+
val movieList = listOf(movie1, movie2)
70+
val serialized = mapper.writeValueAsString(movieList)
71+
72+
val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]"""
73+
assertEquals(serialized, json)
74+
}
75+
76+
@Test
77+
fun whenDeserializeList_thenSuccess() {
78+
val json = """[{"name":"Endgame","studio":"Marvel","rating":9.2},{"name":"Shazam","studio":"Warner Bros","rating":7.6}]"""
79+
val movieList: List<Movie> = mapper.readValue(json)
80+
81+
val movie1 = Movie("Endgame", "Marvel", 9.2f)
82+
val movie2 = Movie("Shazam", "Warner Bros", 7.6f)
83+
assertTrue(movieList.contains(movie1))
84+
assertTrue(movieList.contains(movie2))
85+
}
86+
87+
@Test
88+
fun whenSerializeBook_thenSuccess() {
89+
val book = Book("Oliver Twist", "Charles Dickens")
90+
val serialized = mapper.writeValueAsString(book)
91+
92+
val json = """{"title":"Oliver Twist","author":"Charles Dickens"}"""
93+
assertEquals(serialized, json)
94+
}
95+
96+
@Test
97+
fun whenDeserializeBook_thenSuccess() {
98+
val json = """{"title":"Oliver Twist","author":"Charles Dickens"}"""
99+
val book: Book = mapper.readValue(json)
100+
101+
assertEquals(book.title, "Oliver Twist")
102+
assertEquals(book.authorName, "Charles Dickens")
103+
}
104+
105+
@Test
106+
fun givenJsonInclude_whenSerializeBook_thenEmptyFieldExcluded() {
107+
val book = Book("Oliver Twist", "Charles Dickens")
108+
val serialized = mapper.writeValueAsString(book)
109+
110+
val json = """{"title":"Oliver Twist","author":"Charles Dickens"}"""
111+
assertEquals(serialized, json)
112+
}
113+
114+
}

0 commit comments

Comments
 (0)