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

KSerializer.serialize() of nullable type not invoked for null values #824

Closed
koral-- opened this issue Apr 30, 2020 · 4 comments
Closed
Assignees
Labels

Comments

@koral--
Copy link

koral-- commented Apr 30, 2020

In the project's code there is a nullable value.
Magic not null values need to be deserialized to null.
And nulls need to be serialized to non-null magic values.

While I'm able to deserialize to nulls the serialization does not work. serialize() method is not fired and null is passed to serialized form.

To Reproduce
Serializer code:

internal object LocalTimeSerializer : KSerializer<LocalTime?> {

    override val descriptor = PrimitiveDescriptor("LocalTime?", PrimitiveKind.INT)

    override fun deserialize(decoder: Decoder): LocalTime? = when (val seconds = decoder.decodeInt()) {
       -1 -> null
        else -> LocalTime.ofSecondOfDay(seconds.toLong())
    }

    override fun serialize(encoder: Encoder, value: LocalTime?) {
        when (value) {
            null -> encoder.encodeInt(-1) //this line is never reached despite that nulls exist in serialized lists
            else -> encoder.encodeInt(value.toSecondOfDay())
        }
    }
}

Usage inside serializable class:

val times: List<@Serializable(with = LocalTimeSerializer::class) LocalTime?>,

Expected behavior
serialize() method is invoked for nulls.

Environment

  • Kotlin version: 1.3.72
  • Library version: 0.20.0
  • Kotlin platforms: JVM
  • Gradle version: 6.3+
@Dominaezzz
Copy link
Contributor

Your serializer is probably being wrapped in another serializer that handles nulls.

@koral--
Copy link
Author

koral-- commented Apr 30, 2020

I'm not explicitly wrapping it.
Serialization is used with Retrofit like that:

val converterFactory = Json(JsonConfiguration(ignoreUnknownKeys = true))
        .asConverterFactory("application/json".toMediaType())

Apart from enabling ignoreUnknownKeys there are no more customizations.

@vladrotea
Copy link

+1, I have the same issue.

@qwwdfsad qwwdfsad self-assigned this May 7, 2020
@sandwwraith
Copy link
Member

I think the issue happens because serializer is automatically wrapped with a standard NullableSerializer from the runtime library

qwwdfsad added a commit that referenced this issue Jul 27, 2020
…alizer

Effectively enabling custom serialization and deserialization of nulls and non-nulls by properly checking whether descriptor is nullable

Fixes #824
qwwdfsad added a commit that referenced this issue Jul 28, 2020
…alizer

Effectively enabling custom serialization and deserialization of nulls and non-nulls by properly checking whether descriptor is nullable

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

No branches or pull requests

5 participants