ansman/kotshi

Transient fields are not ignored

bishiboosh opened this issue · 6 comments

Fields marked @Transient are not ignored. I'll try to add a PR for this, as the fix seems quite easy (just detect the transient modifier in the parameter, and ignore this parameter if it is the case)

If you do this, don't forget to allow setting a default value into the field when deserializing.

It’s not that easy I’m afraid since you need all properties to be declared in the constructor. One solution is to enforce default values for transient properties but I’m not sure I like it. It feels a bit like this could be solved in a nicer way (in the client code that is)

Moshi supports transient, is there an argument that Kotshi shouldn't?

Consider:

@JsonSerializable
data class User(
    val name: String,
    val email: String,
    @Transient val sessionKeys: List<String> = listOf())

Yes you could put sessionKeys down in a separate property but then you lose out on all the benefits of data class for that field including copy constructors, hashing, and equals. And yes one could always model the transient data outside of the data class but that seems restrictive.

@bishiboosh hope you don't mind that I gave this a try!

@gladed I was on holidays, so thank you ! I'll go check your PR :D

Tried it in 1.0.3 and it's working great, thanks for merging @ansman