I don't understand the algorithm for `serializer(f...
# serialization
c
I don't understand the algorithm for
serializer(foo: KType)
. I have this test code:
Copy code
test("Access a serializer that doesn't exist on all platforms") {
		check(serializerOrNull(typeOf<Foo>()) == null) { "The class Foo is not serializable, this code shouldn't find a serializer" }
	}

	test("Access a serializer that exists on all platforms") {
		check(serializerOrNull(typeOf<Profile>()) != null) { "The class Profile is serializable, this code should find a serializer" }
	}

	test("Access a serializer for ObjectId on all platforms") {
		check(serializerOrNull(typeOf<ObjectId>()) != null) { "The class ObjectId is serializable, this code should find a serializer" }
	}

	test("Access a serializer for Foo2 on all platforms") {
		check(serializerOrNull(typeOf<Foo2>()) != null) { "The class Foo2 is serializable, this code should find a serializer" }
	}
with the types:
Copy code
@Serializable
data class Profile(
	val name: String,
	val age: Int,
)

class Foo(val a: Int)
and in another module, that has the Serialization plugin:
Copy code
@Serializable(with = ObjectId.Serializer::class)
class ObjectId : Comparable<ObjectId> {

    class Serializer : KSerializer<ObjectId> { … }
}

@Serializable
class Foo2(val a: Int)
and I get the result: • Serializer of `Foo`: OK on all platforms (returns
null
) • Serializer of
Profile
: OK on all platforms (returns non-
null
) • Serializer of
ObjectId
: Fails on non-JVM platforms (returns
null
) • Serializer of `Foo2`: OK on all platforms (returns non-
null
) So I guess that means
serializer(KType)
cannot find classes with a custom serializer? That's very unfortunate.
I just checked and yeah, if
@ObjectId
uses a plain old
@Serializable
without a
with
, then
serializer(KType)
finds it 🤦‍♂️
Reproduced on the Kotlin Playground but… it seems to affect the JVM too, even though it doesn't in my tests? https://pl.kotl.in/x8DIrVEvA However the Kotlin Playground doesn't have KxS on non-JVM platforms so it's not easy to verify