Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use UInt in custom serializer #1480

Closed
msrd0 opened this issue May 14, 2021 · 3 comments
Closed

Unable to use UInt in custom serializer #1480

msrd0 opened this issue May 14, 2021 · 3 comments
Assignees

Comments

@msrd0
Copy link

msrd0 commented May 14, 2021

Describe the bug

I tried to use UInt in a custom serializer. Unfortunately, it throws an exception at runtime.

To Reproduce

@Serializable(with = Foo.Serializer::class)
class Foo(val bar: UInt) {
	internal class Serializer : KSerializer<Foo> {
		override val descriptor: SerialDescriptor =
			buildClassSerialDescriptor("org.example.Foo") {
				element<UInt>("bar")
			}
		// ...
	}
}

This produces the following exception at runtime:

kotlinx.serialization.SerializationException: Serializer for class 'UInt' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
	at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)

The first line in the stacktrace pointing at my code is at the element<UInt>("bar") line.

Environment

  • Kotlin version: 1.5.0
  • Library version: both 1.1.0 and 1.2.1 produce this exception
  • Kotlin platforms: JVM
  • Gradle version: 7.0
  • Java version: 11.0.11 (OpenJDK)
@sandwwraith
Copy link
Member

As a workaround, you can use element("bar", UInt.serializer().descriptor)

@msrd0
Copy link
Author

msrd0 commented May 14, 2021

@sandwwraith Using Class.serializer() only seems to work for types defined in the same module with @Serializable annotation, using UInt.serializer() (or String.serializer()) produces the following compile error:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun <T : Any> KClass<TypeVariable(T)>.serializer(): KSerializer<TypeVariable(T)> defined in kotlinx.serialization
public inline fun <reified T> SerializersModule.serializer(): KSerializer<TypeVariable(T)> defined in kotlinx.serialization
public fun SerializersModule.serializer(type: Type): KSerializer<Any> defined in kotlinx.serialization
public fun SerializersModule.serializer(type: KType): KSerializer<Any?> defined in kotlinx.serialization

@sandwwraith
Copy link
Member

Try to import this function:

public fun UInt.Companion.serializer(): KSerializer<UInt> = UIntSerializer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants