Skip to content

0.20.0

Compare
Choose a tag to compare
@ZacSweers ZacSweers released this 05 Dec 01:34
· 342 commits to main since this release

New: @FallbackJsonAdapter

moshi-sealed now supports a new @FallbackJsonAdapter. This is a proxy to Moshi's PolymorphicJsonAdapter.withFallbackJsonAdapter(). This allows you to specify a custom JsonAdapter to handle cases of unrecognized type labels in decoding. It's advanced usage and not recommended for regular cases.

The specified JsonAdapter must have a public constructor with no parameters or a single Moshi parameter.

@FallbackJsonAdapter(FrogFallbackJsonAdapter::class)
@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed class Frog {

  @JsonClass(generateAdapter = true)
  @TypeLabel("original", null)
  data class OriginalFrog(...)

  @JsonClass(generateAdapter = true)
  @TypeLabel("poisonous")
  data class PoisonousFrog(...)
  
  class FrogFallbackJsonAdapter(moshi: Moshi) : JsonAdapter<Frog>() {
    private val delegate = moshi.adapter<OriginalFrog>()
    override fun fromJson(reader: JsonReader): Frog? {
      // Default to original frog
      return delegate.fromJson(reader)
    }
    
    //...
  }
}

New: Experimental support for the new K2 compiler in moshi-ir.

Edit: this isn't usable in this release, don't try to use it!

Misc

  • Enhancement: Generate extra proguard rules for @NestedSealed types to prevent R8 from inlining the parent sealed type into the subtype in some cases.
  • Enhancement: Proguard generation in moshi-ir now uses Anvil's more optimized compiler util APIs.
  • Enhancement: Check and report more error cases in moshi-ir and moshi-sealed code gen.
  • Fix: moshi-ir now properly generates proguard rules for moshi-sealed types.
  • Fix: track @NestedSealed types as originating files in moshi-sealed KSP.
  • Enhancement: Substantially improved KSP and IR test coverage of error cases.
  • Update to Kotlin 1.7.22.
  • Update to KSP 1.7.22-1.0.8.

What's Changed

Full Changelog: 0.19.0...0.20.0