Is it possible to make the parser ignore some field when encoding them? I would like a class like th...
j
Is it possible to make the parser ignore some field when encoding them? I would like a class like this:
data class MyDataClass(@IgnoreThisKey val privateKey: String, val value: String)
And when serializing any data(not only
MyDataClass
) and there's a property with
IgnoreThisKey
annotation, it should not be included.
d
@Transient
is probably what you're looking for
a
And make sure it's
Copy code
@kotlinx.serialization.Transient
not
Copy code
@kotlin.jvm.Transient
just in case you're not using IntelliJ, which will give this warning:
@kotlin.jvm.Transient
does not affect
@Serializable
classes. Please use
@kotlinx.serialization.Transient
instead.
j
And I should be able to add this annotation onto my own annotation?
a
this should work
Copy code
@Serializable
data class MyDataClass(
  @Transient
  val privateKey: String? = null,
  val value: String,
)
note that it needs to have a default value, otherwise KxS doesn't know how to create a new instance during decoding
j
It's mostly the encoding part I'm interesting in. So it's important that when I call
json.encodeToString(MyDataClass("private", "Value"))
it results in
{"value": "Value"}
image.png
But it's not possible to do something like this? I would like to have my own custom annotation class with this
a
no that's not possible,
@Transient
goes on properties
there is
@SerialInfo
, which you can use on your custom annotations and they will be passed to the SerialDescriptor, but I don't think that's what you want https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serial-info/
I guess what you want to do is have some sort of
Copy code
value class Secret(val value: String)
so you can be sure that the value is never serialized?
j
I'm currently working with a library and I want to change the backend from Gson to Serialization and we already have a annotation class. So would be great if I could update without the users of my library needing to update their code. Gson has ExclusionStrategy, where we are just checking if the field contains our own annotation class. So was wondering if I could do something alike in Serialization. So would be nice if I could do it inside of the JsonBuilder somehow, and without specifing the specific class directly, but only do the filter by the annotation class on the properties.
a
hmmm... I don't think there's a direct equivalent. Trying to come up with something could be quite difficult. You could try creating your own KSerializer that always encodes to null, and specifying it as the serializer
Copy code
@Serializable(with = TransientSerializer::class)
data class MyDataClass(
  val privateKey: String? = null,
  val value: String,
)

object TransientSerializer : KSerializer<Any> {
  override fun encode(...) {}
  override fun decode(...) {}
}
j
Think I will just upgrade my major version, then I would go with the simple solution, of getting the users of this library to write in the @Transient themselves 😛
Thanks a bunch for the help anyway
a
haha I totally agree
2589 Views