Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing classes with lazy properties using Gson and Kotlin 1.0 beta 4

Tags:

gson

kotlin

Using Gson, I want to deserialize a Kotlin class that contains a lazy property.

With Kotlin 1.0 beta 4 I get the following error during object deserialization:

Caused by: java.lang.InstantiationException: can't instantiate class kotlin.Lazy 

With Kotlin 1.0 beta 2, I used to mark the property with the @Transient annotaiton to tell Gson to skip it. With beta 4 this is not possible anymore, as the annotation causes a compile error.

This annotation is not applicable to target 'member property without backing field' 

I can’t figure out how to fix this. Any ideas?

Edit: the lazy property is serialized to JSON ("my_lazy_prop$delegate":{}), but this is not what I want as it is computed from other properties. I suppose if I find a way to prevent the property from being serialized the deserialization crash would be fixed.

like image 370
clemp6r Avatar asked Dec 23 '15 13:12

clemp6r


1 Answers

Since Kotlin 1.0 simply mark the field like this to ignore it during de/serialization:

@delegate:Transient  val field by lazy { ... } 
like image 80
Fabian Zeindl Avatar answered Sep 20 '22 03:09

Fabian Zeindl