Hello all! I'm trying to serialize `Any` in a nest...
# serialization
m
Hello all! I'm trying to serialize 
Any
 in a nested Map and I'm using the @contextual method defined in this post:
Copy code
@Serializable
data class Media(
    val id: String? = null,
    val title: String? = null,
    val tracking: Map<String, Map<String, @Contextual Any>> = mapOf(),
)

val json = Json {
    ignoreUnknownKeys = true
    serializersModule = SerializersModule {
    contextual(String.serializer()) // could be an Int
    contextual(Int.serializer()) // could be a String
    contextual(Boolean.serializer()) // could be a Boolean
   }
}

val media = json.decodeFromString(Media.serializer(), MediaResponse.json) //MediaResponse.json is a string
I'm still getting this error
Copy code
kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
Am I missing something?
t
@Contextual
require context for
Any
type (missed in
json
constant)
m
@turansky Thanks for the response! I'm still a little unclear on your suggestion of what needs to be added to the json constant. Mostly because
Any
doesn't have its own serializer so I can't do something like
contextual(Any.serializer())
.
t
AFAIK - you need register custom serializer in this case
contextual(Any::class, MyAnySerializer())
Problem already was discussed in this channel. You can find exact solution in channel
179 Views