1
1
package dev.openfeature.sdk
2
2
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 )
19
3
sealed interface Value {
20
4
21
5
fun asString (): kotlin.String? = if (this is String ) string else null
@@ -27,28 +11,20 @@ sealed interface Value {
27
11
fun asStructure (): Map <kotlin.String , Value>? = if (this is Structure ) structure else null
28
12
fun isNull (): kotlin.Boolean = this is Null
29
13
30
- @Serializable
31
14
data class String (val string : kotlin.String ) : Value
32
15
33
- @Serializable
34
16
data class Boolean (val boolean : kotlin.Boolean ) : Value
35
17
36
- @Serializable
37
18
data class Integer (val integer : Int ) : Value
38
19
39
- @Serializable
40
20
data class Double (val double : kotlin.Double ) : Value
41
21
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
44
23
45
- @Serializable
46
24
data class Structure (val structure : Map <kotlin.String , Value >) : Value
47
25
48
- @Serializable
49
26
data class List (val list : kotlin.collections.List <Value >) : Value
50
27
51
- @Serializable
52
28
object Null : Value {
53
29
override fun equals (other : Any? ): kotlin.Boolean {
54
30
return other is Null
@@ -58,37 +34,4 @@ sealed interface Value {
58
34
return javaClass.hashCode()
59
35
}
60
36
}
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
- }
94
37
}
0 commit comments