CLOVIS
05/09/2026, 11:30 AMdecodeFromString() etc functions always knows which instance should be used. How can it communicate that instance to the serializer deeper in the stack?
This is specifically for the MongoDB official KxS format, which has a custom BsonDecoder that I can't modify.Oliver.O
05/09/2026, 4:37 PMrnett
05/10/2026, 11:57 PMCLOVIS
05/11/2026, 8:17 AMFor now I think the only way is to make it part of the (custom) encoder and decoderI can't, this is needed with the official MongoDB format. My own custom format already passes that information. I think I'll go with thread-locals then
Oliver.O
05/11/2026, 8:24 AMCLOVIS
05/11/2026, 9:43 AMsuspend , so I won't have access to those, right?Oliver.O
05/11/2026, 9:58 AMThreadLocal become coroutine-local. And if you're using serialization from coroutines, you have to, as a coroutine can hop threads any time.
You insert
threadLocalContext.asContextElement(context)
at an appropriate (highest) point in your coroutine hierarchy, and then use threadLocalContext from non-suspending code (e.g. inside serialization) wherever you need it.CLOVIS
05/11/2026, 2:50 PMas a coroutine can hop threads any time.Not at any time, only on suspension points, which can only happen within
suspend functions. There are no suspend functions between the top-level deserialization call and the decoders.Oliver.O
05/11/2026, 2:53 PMCLOVIS
05/11/2026, 3:14 PMCLOVIS
05/14/2026, 10:24 AM@Serializable(with = ContextualSerializer::class) on the type, so it doesn't have a serializer at all, and register a runtime serializer in the serialization module that knows the required data?