-1

I'm trying to create a Serializer for the List<Pair<String, Any>> type, I need this type for a project where I have to manipulate keys and values but user can change key names at any time and using a List of Pair is much better for what I want (and doesn't really work with a Map).

I have this code, but it produces a compiler error

class SnapshotListPairSerializer<K, V>(private val keySerializer: KSerializer<K>, private val valueSerializer: KSerializer<V>) :
    KSerializer<SnapshotStateList<Pair<K, V>>> {
    override val descriptor: SerialDescriptor = ListSerializer(PairSerializer(keySerializer, valueSerializer)).descriptor
    
    override fun serialize(encoder: Encoder, value: SnapshotStateList<Pair<K, V>>) {
        encoder.encodeSerializableValue(ListSerializer(PairSerializer(keySerializer, valueSerializer)), value as List<Pair<K, V>>)
    }
    
    override fun deserialize(decoder: Decoder): SnapshotStateList<Pair<K, V>> {
        val list = mutableStateListOf<Pair<K, V>>()
        val items = decoder.decodeSerializableValue(ListSerializer(PairSerializer(keySerializer, valueSerializer)))
        list.addAll(items)
        return list
    }
}

Also, SnapshotStateList is a class that comes from Jetpack Compose and extends List.

Ayfri
  • 570
  • 1
  • 4
  • 24
  • 2
    (The PasteBin link gives `Error, this is a private paste or is pending moderation.` — Which is of course one reason why it's often better to include stuff like that directly in the question where possible.) – gidds Dec 29 '21 at 00:08
  • 1
    Your code compiles fine to me. Please don't use links to your code/errors on SO questions, instead use formatted significant part of the code/stack trace. – Phil Dukhov Dec 29 '21 at 04:07
  • Sorry the link was working some days ago, I'll change it, but my stacktrace is like 3000 lines long with a lot of compiler code and compiler saying "there and there and there I'm not working". – Ayfri Dec 29 '21 at 14:16
  • The error is now public on pastebin. – Ayfri Dec 29 '21 at 14:20
  • Thank you for the full stack trace. But still, could you please include the _main_ points of the exception you get in the question, i.e., the exception, as been suggested twice now? "Backend Internal error: Exception during IR lowering" It not only prevents problems like this, it is friendlier to those trying to help you, _and_ ensures the essence of your question can more easily end in Google results in return helping others. ;) – Steven Jeuris Jan 15 '22 at 23:11

1 Answers1

1

The exception you get is:

Backend Internal error: Exception during IR lowering

Given that this is not providing you with meaningful information, but mentions compiler internals, this is not an error of your doing, but a bug: a cue to search for known bugs.

This seems very similar to the issue I filed on GitHub.

If it is the same cause, it should be fixed in version 1.6.10. This may explain why Philip can't repro.

P.s. the next problem you will run into is likely that Any is not registered for polymorphic serialization. Serializing Any is dodgy. If you are stuck and the documentation does not help you out, I suggest you post a new question with more information on the exact use case/expected types, and I will gladly help out.

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • Okay, I do not know that, thanks ! But for my side I just used a basic List serializer instead of one for List of pairs, and it worked gently ^^ – Ayfri Jan 16 '22 at 11:42