https://kotlinlang.org logo
#serialization
Title
# serialization
c

Chad Gregory

02/06/2023, 1:21 PM
I have a serializable data class that I am using with ktor client
Copy code
@Serializable
data class Job (
    val information: JobInformation = JobInformation(),
    val additional: AdditionalInformation = AdditionalInformation(),
    val bom: PersistentList<BOM> = persistentListOf()
)

@Serializable
data class BOM(
    @SerialName("comp_item")
    val compItem: String = "",
    val sequence: String = "",
    @SerialName("comp_qty")
    val compQty: String = "",
    @SerialName("comp_uom")
    val compUom: String = "",
    val operation: String = "",
    val type: String = ""
)
but when ktor client tries to decode the data from the request response I get this error
Copy code
kotlinx.serialization.json.internal.JsonDecodingException: Expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of kotlinx.serialization.Polymorphic<PersistentList>, but had class kotlinx.serialization.json.JsonArray (Kotlin reflection is not available)
I understand what the error is saying but I don't know how to fix it.
a

Adam S

02/06/2023, 2:53 PM
is
PersistentList
from kotlinx.collections? Have you set up custom serializers? https://github.com/Kotlin/kotlinx.collections.immutable/issues/63
c

Chad Gregory

02/06/2023, 4:06 PM
First I would like to know how a first party package is missing serialization support for 3 years but I'm just going to table that for now. I created this
Copy code
@Serializer(forClass = PersistentList::class)
class PersistentListSerializer<T>(private val dataSerializer: KSerializer<T>) : KSerializer<PersistentList<T>> {
    private class PersistentListDescriptor<T> : SerialDescriptor by serialDescriptor<List<T>>() {
        @ExperimentalSerializationApi
        override val serialName: String = "kotlinx.serialization.immutable.persistentList"
    }
    override val descriptor: SerialDescriptor = PersistentListDescriptor<T>()
    override fun serialize(encoder: Encoder, value: PersistentList<T>) {
        return ListSerializer(dataSerializer).serialize(encoder, value.toList())
    }
    override fun deserialize(decoder: Decoder): PersistentList<T> {
        return ListSerializer(dataSerializer).deserialize(decoder).toPersistentList()
    }
}
Switched with Kapt to KSP because of this stupid error https://github.com/Kotlin/kotlinx.serialization/issues/1908#issuecomment-1105187175 but now I am getting a new error
Copy code
E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.testapp, PID: 22633
                                                                                                    java.lang.IllegalStateException: Only KClass supported as classifier, got T
                                                                                                    	at kotlinx.serialization.internal.Platform_commonKt.kclass(Platform.common.kt:102)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:78)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
                                                                                                    	at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.builtinSerializer$SerializersKt__SerializersKt(Serializers.kt:96)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:84)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
                                                                                                    	at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
                                                                                                    	at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:41)
                                                                                                    	at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
                                                                                                    	at ca.on.listech.imm.util.PersistentListSerializer$PersistentListDescriptor.<init>(PersistentListSerializer.kt:31)
                                                                                                    	at ca.on.listech.imm.util.PersistentListSerializer.<init>(PersistentListSerializer.kt:22)
                                                                                                    	at ca.on.listech.imm.domain.models.Job$$serializer.childSerializers(Job.kt:10)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$childSerializers$2.invoke(PluginGeneratedSerialDescriptor.kt:36)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$childSerializers$2.invoke(PluginGeneratedSerialDescriptor.kt:36)
                                                                                                    	at kotlin.SafePublicationLazyImpl.getValue(LazyJVM.kt:107)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.getChildSerializers(PluginGeneratedSerialDescriptor.kt:36)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.getElementDescriptor(PluginGeneratedSerialDescriptor.kt:76)
                                                                                                    	at kotlinx.serialization.descriptors.SerialDescriptorKt$elementDescriptors$1$1.next(SerialDescriptor.kt:284)
                                                                                                    	at kotlinx.serialization.descriptors.SerialDescriptorKt$elementDescriptors$1$1.next(SerialDescriptor.kt:279)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptorKt.hashCodeImpl(PluginGeneratedSerialDescriptor.kt:137)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$_hashCode$2.invoke(PluginGeneratedSerialDescriptor.kt:44)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor$_hashCode$2.invoke(PluginGeneratedSerialDescriptor.kt:44)
                                                                                                    	at kotlin.SafePublicationLazyImpl.getValue(LazyJVM.kt:107)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.get_hashCode(PluginGeneratedSerialDescriptor.kt:44)
                                                                                                    	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.hashCode(PluginGeneratedSerialDescriptor.kt:97)
                                                                                                    	at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
                                                                                                    	at kotlinx.serialization.json.internal.DescriptorSchemaCache.get(SchemaCache.kt:35)
                                                                                                    	at kotlinx.serialization.json.internal.DescriptorSchemaCache.getOrPut(SchemaCache.kt:27)
                                                                                                    	at kotlinx.serialization.json.internal.JsonNamesMapKt.getJsonNameIndex(JsonNamesMap.kt:52)
                                                                                                    	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeObjectIndex(StreamingJsonDecoder.kt:139)
                                                                                                    	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeElementIndex(StreamingJsonDecoder.kt:92)
                                                                                                    	at ca.on.listech.imm.domain.models.Job$$serializer.deserialize(Job.kt:10)
                                                                                                    	at ca.on.listech.imm.domain.models.Job$$serializer.deserialize(Job.kt:10)
                                                                                                    	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
                                                                                                    	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
                                                                                                    	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
                                                                                                    	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
a

Adam S

02/06/2023, 4:08 PM
Copy code
@Serializer(forClass = PersistentList::class)
class PersistentListSerializer<T>(private val dataSerializer: KSerializer<T>)
annotating it with
@Serializer
probably isn’t right
normally
@Serializer(forClass ...)
is used on independent objects
are you sure you need to serialize directly to/from
PersistentList
? What if you used a regular list, and then converted it via a property?
Copy code
@Serializable
data class X(
   private val boms: List<BOM>
) {
  val bomsPersistent = boms.toPersistentList()
}
(tweak it to make it more accurate/a bit prettier)
c

Chad Gregory

02/06/2023, 4:13 PM
that might be the a better idea, this is getting kind of ridiculous lol
a

Adam S

02/06/2023, 4:16 PM
yeah KxS can be complicated - it’s usually easier to have a little bit of extra Kotlin code, because it’s easier to understand and debug
c

Chad Gregory

02/06/2023, 4:46 PM
I am using this with Datastore, would there be any issue with retrieving bom from a DataStore as a Flow<Job>?
Copy code
val getJob: Flow<Job> = cxt.job.data

suspend fun updateActiveJob(info: JobInformation, additional: AdditionalInformation, bom: List<BOM>) {
        cxt.job.updateData {job ->
            job.copy(
                information = info,
                additional = additional,
                bom = job.bomList.mutate {
                    for (item in bom) {
                        it.add(item)
                    }
                }
            )
        }
    }
a

Adam S

02/06/2023, 4:50 PM
sorry I don’t really understand the context. Are you asking if you can use KxS to encode/decode data on the fly? Because that’s fine
c

Chad Gregory

02/06/2023, 4:52 PM
my question is since I made this change
Copy code
@Serializable
data class X(
   private val bom: List<BOM>
) {
  val bomList = bom.toPersistentList()
}
will there be an issue with getting the data out of the datastore via a Flow when the serializable data class does not have a PersistentList property?
a

Adam S

02/06/2023, 4:56 PM
that depends on a lot of things! All KxS cares about is that 1. everything has a KSerializer and SerialDescriptor, and 2. the property
bom
is structured like a list
for example, if it’s JSON data then KxS doesn’t care if it’s deserializing to a List or an Array or ArrayList or MutableList
c

Chad Gregory

02/06/2023, 4:59 PM
Well the DataStore contains a PersistentList<BOM> which then needs to become a List<BOM> so my guess is that would be an issue since I had issues going from List<BOM> to PersistentList<BOM>\
if so there basically no point to even use PersistentList at all and just store it as json data for something
a

Adam S

02/06/2023, 5:01 PM
that could well be. I’ve never used the kotlinx.collections so I wouldn’t know
c

Chad Gregory

02/06/2023, 5:01 PM
well with the context of this thread that would be true
279 Views