/
ExampleTest.kt
140 lines (123 loc) · 3.73 KB
/
ExampleTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package arrow.integrations.jackson.module
import arrow.core.NonEmptyList
import arrow.core.Option
import arrow.core.bothIor
import arrow.core.nonEmptyListOf
import arrow.core.none
import arrow.core.right
import arrow.core.some
import arrow.core.valid
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.net.URI
class ExampleTest :
FunSpec({
data class Organization(
val name: String,
val address: Option<String>,
val websiteUrl: Option<URI>
)
data class ArrowUser(
val name: String,
val emails: NonEmptyList<String>,
val organization: Option<Organization>
)
val mapper =
ObjectMapper()
.registerKotlinModule()
.registerArrowModule()
.setSerializationInclusion(
JsonInclude.Include.NON_ABSENT
) // will not serialize None as nulls
val prettyPrinter = mapper.writerWithDefaultPrettyPrinter()
test("example #1: data structure serialization") {
val arrowKt = Organization("arrow-kt", none(), URI("https://arrow-kt.io").some())
val arrowUser =
ArrowUser(
"John Doe",
nonEmptyListOf("john@email.com", "john.doe@email.com.au"),
arrowKt.some()
)
val jsonString = prettyPrinter.writeValueAsString(arrowUser)
jsonString shouldBe
"""
{
"name" : "John Doe",
"emails" : [ "john@email.com", "john.doe@email.com.au" ],
"organization" : {
"name" : "arrow-kt",
"websiteUrl" : "https://arrow-kt.io"
}
}
""".trimIndent(
)
mapper.readValue(jsonString, ArrowUser::class.java) shouldBe arrowUser
}
test("example #2: validated") {
val arrowKt = Organization("arrow-kt", none(), URI("https://arrow-kt.io").some())
prettyPrinter.writeValueAsString(arrowKt.valid()) shouldBe
"""
{
"valid" : {
"name" : "arrow-kt",
"websiteUrl" : "https://arrow-kt.io"
}
}
""".trimIndent(
)
}
test("example #3: either") {
data class Fruit(@get:JsonValue val name: String)
val apricot = Fruit("apricot")
prettyPrinter.writeValueAsString(apricot.right()) shouldBe
"""
{
"right" : "apricot"
}
""".trimIndent()
}
test("example #4: ior") {
data class Fruit(@get:JsonValue val name: String)
data class Vegetable(val name: String, val kind: String)
val starfruit = Fruit("starfruit")
val spinach = Vegetable("spinach", "leafy greens")
prettyPrinter.writeValueAsString(Pair(starfruit, spinach).bothIor()) shouldBe
"""
{
"left" : "starfruit",
"right" : {
"name" : "spinach",
"kind" : "leafy greens"
}
}
""".trimIndent(
)
}
test("example #5: customizing field names") {
data class Fruit(@get:JsonValue val name: String)
data class Vegetable(val name: String, val kind: String)
val customMapper =
ObjectMapper()
.registerKotlinModule()
.registerArrowModule(iorModuleConfig = IorModuleConfig("l", "r"))
val starfruit = Fruit("starfruit")
val spinach = Vegetable("spinach", "leafy greens")
customMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(Pair(starfruit, spinach).bothIor()) shouldBe
"""
{
"l" : "starfruit",
"r" : {
"name" : "spinach",
"kind" : "leafy greens"
}
}
""".trimIndent(
)
}
})