This repository was archived by the owner on Apr 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExampleTest.kt
160 lines (138 loc) · 4.12 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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 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.matchers.shouldBe
import java.net.URI
import kotlin.test.Ignore
import kotlin.test.Test
class ExampleTest {
data class Organization(
val name: String,
val address: Option<String>,
val websiteUrl: Option<URI>,
)
data class ArrowUser(
val name: String,
val emails: List<String>,
val organization: Option<Organization>,
)
data class ArrowUserNel(
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
fun `example #1 - data structure serialization`() {
val arrowKt = Organization("arrow-kt", none(), URI("https://arrow-kt.io").some())
val arrowUser =
ArrowUser("John Doe", listOf("[email protected]", "[email protected]"), arrowKt.some())
val jsonString = prettyPrinter.writeValueAsString(arrowUser)
jsonString shouldBe
"""
{
"name" : "John Doe",
"emails" : [ "[email protected]", "[email protected]" ],
"organization" : {
"name" : "arrow-kt",
"websiteUrl" : "https://arrow-kt.io"
}
}
"""
.trimIndent()
mapper.readValue(jsonString, ArrowUser::class.java) shouldBe arrowUser
}
@Ignore
@Test
fun `example #2 - data structure serialization - non-empty list`() {
val arrowKt = Organization("arrow-kt", none(), URI("https://arrow-kt.io").some())
val arrowUser =
ArrowUserNel(
"John Doe",
nonEmptyListOf("[email protected]", "[email protected]"),
arrowKt.some(),
)
val jsonString = prettyPrinter.writeValueAsString(arrowUser)
jsonString shouldBe
"""
{
"name" : "John Doe",
"emails" : [ "[email protected]", "[email protected]" ],
"organization" : {
"name" : "arrow-kt",
"websiteUrl" : "https://arrow-kt.io"
}
}
"""
.trimIndent()
mapper.readValue(jsonString, ArrowUserNel::class.java) shouldBe arrowUser
}
@Test
fun `example #3 - either`() {
data class Fruit(@get:JsonValue val name: String)
val apricot = Fruit("apricot")
prettyPrinter.writeValueAsString(apricot.right()) shouldBe
"""
{
"right" : "apricot"
}
"""
.trimIndent()
}
@Test
fun `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
fun `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()
}
}