I created a serializer that would allow me to cach...
# serialization
j
I created a serializer that would allow me to cache a map containing lazy values that are expensive to compute. That way I can persist them to disk between invocations of the program:
Copy code
@Serializer(forClass = Map::class)
class  MapWithLazyValue<K,V>(private val keySerializer : KSerializer<K>, private val valueSerializer : KSerializer<V>) : KSerializer<Map<K, Lazy<V>>> {
    override val descriptor: SerialDescriptor = PrimitiveDescriptor("map", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): Map<K, Lazy<V>> {
        return decoder.decodeSerializableValue(MapSerializer(keySerializer, valueSerializer)).mapValues { lazy { it.value } }
    }

    override fun serialize(encoder: Encoder, value: Map<K, Lazy<V>>) {
        encoder.encodeSerializableValue(MapSerializer(keySerializer, valueSerializer),
            value.filterValues { it.isInitialized() }.mapValues { it.value.value })
    }
}
However, when I try to build my project, I get:
Copy code
D:\IdeaProject\build\tmp\kapt3\stubs\main\com\collection\ArchiveHashCache.java:13: error: incompatible types: Class<MapWithLazyValue> cannot be converted to Class<? extends KSerializer<?>>
    @kotlinx.serialization.Serializable(with = com.collection.MapWithLazyValue.class)
[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: arrow.core.extensions.ExtensionProcessor (NON_INCREMENTAL), arrow.fold.AutoFoldProcessor (NON_INCREMENTAL), arrow.generic.CoproductProcessor (NON_INCREMENTAL), arrow.generic.ProductProcessor (NON_INCREMENTAL), arrow.higherkinds.HigherKindsProcessor (NON_INCREMENTAL), arrow.optics.OpticsProcessor (NON_INCREMENTAL).
I realize this may be a bug in kapt/arrow, but have I taken the right approach to writing my custom serializer or have I done something wrong?
The [WARN] is a separate message to the error. I am seeing the error in the annotation processor / code gen probably related to this line of code where I specify my custom serializer:
Copy code
@Serializable(with=MapWithLazyValue::class) private var hashCache : MutableMap<FileKey, Lazy<String>> = ConcurrentHashMap<FileKey, Lazy<String>>()
FYI I was able to get this working by creating a new class
Copy code
class MapWithLazyValue : LinkedHashMap<FileKey, Lazy<String>>()
and then implementing the serializer without any templates. Probably need to qualify my value type somehow, but IDE is not giving me any feedback.