-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptionJson.kt
39 lines (32 loc) · 1.16 KB
/
OptionJson.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
package net.orandja.test
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.orandja.either.None
import net.orandja.either.Option
import net.orandja.either.Some
import kotlin.test.Test
import kotlin.test.assertEquals
class OptionJson {
@Serializable
data class Data(val value: Option<String?> = None)
private val codec = Json { encodeDefaults = false }
private val noneData = Data(None)
private val nullData = Data(Some(null))
private val someData = Data(Some("value"))
private val noneJson = """{}"""
private val nullJson = """{"value":null}"""
private val someJson = """{"value":"value"}"""
@Test
fun serialize() {
assertEquals(noneJson, codec.encodeToString(noneData))
assertEquals(nullJson, codec.encodeToString(nullData))
assertEquals(someJson, codec.encodeToString(someData))
}
@Test
fun deserialize() {
assertEquals(noneData, codec.decodeFromString(noneJson))
assertEquals(nullData, codec.decodeFromString(nullJson))
assertEquals(someData, codec.decodeFromString(someJson))
}
}