Kotlinx.serialization: Transient not working

Created on 27 May 2019  路  6Comments  路  Source: Kotlin/kotlinx.serialization

The kdoc of Transient says:

Marks the JVM backing field of the annotated property as transient, meaning that it is not part of the default serialized form of the object.

However this is not the case with the serialization plugin as it serializes transient properties.

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

@Serializable
data class Data(val a: Int) {

  @Transient
  val b = "Hello"
}

fun main() {
  val serialized = Json.stringify(Data.serializer(), Data(1))
  val expected = """{"a":1}"""
  check(serialized == expected)
}
  • Kotlin version: [e.g. 1.3.31]
  • Library version: [e.g. 0.11.1] ]
question

Most helpful comment

If you decide not to recognize it, I suggest to show a compile-time warning when using the wrong Transient on a Serializable class. This mistake is very easy to make...

All 6 comments

I think you've mentioned the kotlin.jvm.Transient annotation which is dedicated for the JVM platform and represents Java's transient keyword. This serialization framework is cross-platform and has different semantics, therefore it has its own kotlinx.serialization.Transient annotation, which should be used in @Serializable classes

Ah that's tricky and I assume it leads in a lot of confusion and programming mistakes.
Speaks anything against the serialization plug-in honoring that annotation too?

We could recognize it too, but it would be strange from the semantic point of view

If you decide not to recognize it, I suggest to show a compile-time warning when using the wrong Transient on a Serializable class. This mistake is very easy to make...

I agree with this idea - there should be a warning about using wrong Transient!

IDE warning arrives in the next release

Was this page helpful?
0 / 5 - 0 ratings