hey folks, it might be a strange question but will...
# serialization
i
hey folks, it might be a strange question but will shoot anyway. https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/examples.md#supported-properties
Initializers are called iff (if and only if) property is @Transient or optional and was not read (see below).
Is there any way to change this default behaviour
if was not read
, make initializer to be called without check if it was seen or not? (Basically have computed properties that only been serialized but never deserialized)
there is hack-ish to use the fact that:
So it is legal in bytecode to reassign a final field any number of times as long as this reassignment is conducted within a constructor call of the class that declares the field
This approach works, but wanted to know if there is another way to achieve the same:
Copy code
@Serializable
data class TaxLine(
    @Transient
    val price: String = "",
    @ContextualSerialization
    val rate: BigDecimal
) {
    @SerialName("price")
    private val _price: String

    init {
        _price = price
    }
}
n
i think
init
is the correct approach for running code that should always execute maybe think about why that initializer function always has to run? one option might be
Copy code
val price: String
    get() = _price
that will just access the backing field and not require copying data, this could also take multiple fields and calculate the correct value, but then maybe via add
inline
edit:
@Transient
is not even required with a getter
i
the issue with calculate fields is
By default, only properties which have backing fields will be serialized and restored back.
not sure if calculated fields with getter will be serialized at all