3

When deserializing a class with Moshi it fails on by lazy property with error:

IllegalArgumentException: No JsonAdapter for interface kotlin.Lazy annotated []

So I want to tell Moshi to ignore the property. The way to ignore this is apparently to use @Transient however that can only be used on a field - not a property.

So how to ignore Kotlin lazy properties when deserializing with Moshi?

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74

1 Answers1

8

You can annotate the delegate itself:

class Foo {
    @delegate:Transient
    val bar by lazy { true }
}
tynn
  • 38,113
  • 8
  • 108
  • 143
  • 2
    Thanks... while this does work, it turns out there is another problem with lazy properties that are not initialized when deserialized by Moshi (NullPointerException) due to not have a no-arg constructor. But that is a different question. – Greg Ennis Mar 02 '18 at 14:33
  • @GregEnnis did you ever figure out how to initialise delegated properties in classes instantiated by Moshi during deserialisation? – Ernest Zamelczyk Dec 21 '18 at 07:42
  • I dont think so. I really wish Moshi used something other than Transient, which carries a lot of other meanings along with it. – Greg Ennis Dec 22 '18 at 16:10
  • If this is till an issue, add KotlinJsonAdapterFactory to moshi builder when building adapter: `Moshi.Builder().add(KotlinJsonAdapterFactory()).build().adapter(MyClass::class.java)` – jhavatar Mar 27 '19 at 09:51