11package 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 )
193sealed 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}
0 commit comments