Skip to content

Commit 3145d6c

Browse files
authored
refactor!: remove kotlinx.serialization dependency
Release-As: 0.3.0 Signed-off-by: Nicklas Lundin <[email protected]>
1 parent 700e16b commit 3145d6c

File tree

3 files changed

+1
-145
lines changed

3 files changed

+1
-145
lines changed

android/build.gradle.kts

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ plugins {
55
id("maven-publish")
66
id("signing")
77
id("org.jlleitschuh.gradle.ktlint")
8-
kotlin("plugin.serialization") version "1.8.10"
98
}
109

1110
val releaseVersion = project.extra["version"].toString()
@@ -98,7 +97,6 @@ publishing {
9897

9998
dependencies {
10099
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
101-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
102100
testImplementation("junit:junit:4.13.2")
103101
testImplementation("org.mockito.kotlin:mockito-kotlin:5.1.0")
104102
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
package dev.openfeature.sdk
22

3-
import android.annotation.SuppressLint
4-
import dev.openfeature.sdk.exceptions.OpenFeatureError
5-
import kotlinx.serialization.KSerializer
6-
import kotlinx.serialization.Serializable
7-
import kotlinx.serialization.descriptors.PrimitiveKind
8-
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
9-
import kotlinx.serialization.encoding.Decoder
10-
import kotlinx.serialization.encoding.Encoder
11-
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
12-
import kotlinx.serialization.json.JsonElement
13-
import kotlinx.serialization.json.jsonObject
14-
import java.text.SimpleDateFormat
15-
import java.util.Date
16-
import java.util.TimeZone
17-
18-
@Serializable(with = ValueSerializer::class)
193
sealed interface Value {
204

215
fun asString(): kotlin.String? = if (this is String) string else null
@@ -27,28 +11,20 @@ sealed interface Value {
2711
fun asStructure(): Map<kotlin.String, Value>? = if (this is Structure) structure else null
2812
fun isNull(): kotlin.Boolean = this is Null
2913

30-
@Serializable
3114
data class String(val string: kotlin.String) : Value
3215

33-
@Serializable
3416
data class Boolean(val boolean: kotlin.Boolean) : Value
3517

36-
@Serializable
3718
data class Integer(val integer: Int) : Value
3819

39-
@Serializable
4020
data class Double(val double: kotlin.Double) : Value
4121

42-
@Serializable
43-
data class Date(@Serializable(DateSerializer::class) val date: java.util.Date) : Value
22+
data class Date(val date: java.util.Date) : Value
4423

45-
@Serializable
4624
data class Structure(val structure: Map<kotlin.String, Value>) : Value
4725

48-
@Serializable
4926
data class List(val list: kotlin.collections.List<Value>) : Value
5027

51-
@Serializable
5228
object Null : Value {
5329
override fun equals(other: Any?): kotlin.Boolean {
5430
return other is Null
@@ -58,37 +34,4 @@ sealed interface Value {
5834
return javaClass.hashCode()
5935
}
6036
}
61-
}
62-
63-
object ValueSerializer : JsonContentPolymorphicSerializer<Value>(Value::class) {
64-
override fun selectDeserializer(element: JsonElement) = when (element.jsonObject.keys) {
65-
emptySet<String>() -> Value.Null.serializer()
66-
setOf("string") -> Value.String.serializer()
67-
setOf("boolean") -> Value.Boolean.serializer()
68-
setOf("integer") -> Value.Integer.serializer()
69-
setOf("double") -> Value.Double.serializer()
70-
setOf("date") -> Value.Date.serializer()
71-
setOf("list") -> Value.List.serializer()
72-
setOf("structure") -> Value.Structure.serializer()
73-
else -> throw OpenFeatureError.ParseError("couldn't find deserialization key for Value")
74-
}
75-
}
76-
77-
@SuppressLint("SimpleDateFormat")
78-
object DateSerializer : KSerializer<Date> {
79-
private val dateFormatter =
80-
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").apply { timeZone = TimeZone.getTimeZone("UTC") }
81-
private val fallbackDateFormatter =
82-
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { timeZone = TimeZone.getTimeZone("UTC") }
83-
override val descriptor = PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING)
84-
override fun serialize(encoder: Encoder, value: Date) = encoder.encodeString(dateFormatter.format(value))
85-
override fun deserialize(decoder: Decoder): Date = with(decoder.decodeString()) {
86-
try {
87-
dateFormatter.parse(this)
88-
?: throw IllegalArgumentException("unable to parse $this")
89-
} catch (e: Exception) {
90-
fallbackDateFormatter.parse(this)
91-
?: throw IllegalArgumentException("unable to parse $this")
92-
}
93-
}
9437
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package dev.openfeature.sdk
22

3-
import kotlinx.serialization.json.Json
4-
import kotlinx.serialization.json.decodeFromJsonElement
5-
import kotlinx.serialization.json.encodeToJsonElement
63
import org.junit.Assert
74
import org.junit.Test
8-
import java.time.Instant
9-
import java.util.Date
105

116
class ValueTests {
127

@@ -61,84 +56,4 @@ class ValueTests {
6156
val value = Value.List(listOf())
6257
Assert.assertEquals(listOf<Value>(), value.asList())
6358
}
64-
65-
@Test
66-
fun testEncodeDecode() {
67-
val date = Date.from(Instant.parse("2023-03-01T14:01:46Z"))
68-
val value = Value.Structure(
69-
mapOf(
70-
"null" to Value.Null,
71-
"text" to Value.String("test"),
72-
"bool" to Value.Boolean(true),
73-
"int" to Value.Integer(3),
74-
"double" to Value.Double(4.5),
75-
"date" to Value.Date(date),
76-
"list" to Value.List(listOf(Value.Boolean(false), Value.Integer(4))),
77-
"structure" to Value.Structure(mapOf("int" to Value.Integer(5)))
78-
)
79-
)
80-
81-
val encodedValue = Json.encodeToJsonElement(value)
82-
val decodedValue = Json.decodeFromJsonElement<Value>(encodedValue)
83-
84-
Assert.assertEquals(value, decodedValue)
85-
}
86-
87-
@Test
88-
fun testJsonDecode() {
89-
val stringDateTime = "2023-03-01T14:01:46Z"
90-
val json = "{" +
91-
" \"structure\": {" +
92-
" \"null\": {}," +
93-
" \"text\": {" +
94-
" \"string\": \"test\"" +
95-
" }," +
96-
" \"bool\": {" +
97-
" \"boolean\": true" +
98-
" }," +
99-
" \"int\": {" +
100-
" \"integer\": 3" +
101-
" }," +
102-
" \"double\": {" +
103-
" \"double\": 4.5" +
104-
" }," +
105-
" \"date\": {" +
106-
" \"date\": \"$stringDateTime\"" +
107-
" }," +
108-
" \"list\": {" +
109-
" \"list\": [" +
110-
" {" +
111-
" \"boolean\": false" +
112-
" }," +
113-
" {" +
114-
" \"integer\": 4" +
115-
" }" +
116-
" ]" +
117-
" }," +
118-
" \"structure\": {" +
119-
" \"structure\": {" +
120-
" \"int\": {" +
121-
" \"integer\": 5" +
122-
" }" +
123-
" }" +
124-
" }" +
125-
" }" +
126-
"}"
127-
128-
val expectedValue = Value.Structure(
129-
mapOf(
130-
"null" to Value.Null,
131-
"text" to Value.String("test"),
132-
"bool" to Value.Boolean(true),
133-
"int" to Value.Integer(3),
134-
"double" to Value.Double(4.5),
135-
"date" to Value.Date(Date.from(Instant.parse(stringDateTime))),
136-
"list" to Value.List(listOf(Value.Boolean(false), Value.Integer(4))),
137-
"structure" to Value.Structure(mapOf("int" to Value.Integer(5)))
138-
)
139-
)
140-
141-
val decodedValue = Json.decodeFromString(Value.serializer(), json)
142-
Assert.assertEquals(expectedValue, decodedValue)
143-
}
14459
}

0 commit comments

Comments
 (0)