Extensions for Moshi including IR plugins, moshi-sealed, and more.
APACHE-2.0 License
Bot releases are hidden (Show)
Fix: Fix IR lookups of setOf()
overloads. There are two setOf()
functions with one arg - one
is the vararg and the other is a shorthand for Collections.singleton(element)
. It's important we
pick the right one, otherwise we can accidentally send a vararg array into the singleton()
function.
Dependency updates:
Kotlin 1.6.21
kotlinpoet 1.11.0
kotlinx-metadata 0.4.2
In some cases, it's useful to have more than one level of sealed types that share the same label key.
sealed interface Response {
data class Success(val value: String) : Response
sealed interface Failure : Response {
data class ErrorMap(val errors: List<String>) : Failure
data class ErrorString(val error: String) : Failure
}
}
moshi-sealed now supports this out of the box via @NestedSealed
annotation. Simply indicate the nested type with this
annotation.
@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed interface Response {
@TypeLabel("success")
@JsonClass(generateAdapter = true)
data class Success(val value: String) : Response
@NestedSealed
sealed interface Failure : Response {
@TypeLabel("error_map")
@JsonClass(generateAdapter = true)
data class ErrorMap(val errors: List<String>) : Failure
@TypeLabel("error_string")
@JsonClass(generateAdapter = true)
data class ErrorString(val error: String) : Failure
}
}
In this case, now Failure
's subtypes will also participate in Response
decoding based on the type
label key.
Caveats:
@DefaultObject
is only supported on direct subtypes.moshi.adapter<Response.Failure>()
),NestedSealed.Factory
JsonAdapter.Factory
to your Moshi
instance for runtime lookup.
val moshi = Moshi.Builder()
.add(NestedSealed.Factory())
.build()
moshi-sealed
has many implementations - kotlin-reflect
, kotlinx-metadata
, KSP, Java sealed classes, and
recently IR. These are a lot to maintain! To cut down on maintenance, Kapt is no longer supported and has been removed
in this release. Please consider migrating to KSP or Moshi-IR.
With Kotlin 1.5.0, sealed types could now exist across multiple files. moshi-sealed
's KSP support previously assumed single files when reporting originating elements, and now properly reports all files if sealed types are spread across multiple files.
Type
construction in moshi-ir
adapter lookups. Prior to this, we generated IRtypeOf()
, but this appears to be too late to leverage compiler intrinsics support for it andkotlin-reflect
is on the classpath. This should improve runtime performance as aimplementation
rather than api
when applying themoshi-ir
plugin. #200
moshi-ir
's proguardThanks to @gpeal for contributing to this release!
Fix: Build new type parameters when generating classes in moshi-ir
rather than incorrectly reuse the original class's parameters. Resolves this issue (that was originally believed to be a Compose issue).
moshi-ir
now supports dynamic generation of proguard rules, bringing it to feature parity with Moshi's existinggenerateProguardRules
moshi {
generateProguardRules.set(false)
}
moshi-ir
, a new debug
property is available in the Gradle extension. It is offmoshi {
debug.set(true)
}
An experimental Kotlin IR implementation of Moshi code gen and moshi-sealed code gen.
The goal of this is to have functional parity with their native Kapt/KSP code gen analogues but run as a fully
embedded IR plugin.
Benefits
Cons
# Keep names for JsonClass-annotated classes
-keepnames @com.squareup.moshi.JsonClass class **
# Keep generated adapter classes' constructors
-keepclassmembers class *JsonAdapter {
public <init>(...);
}
Simply apply the Gradle plugin in your project to use it. You can enable moshi-sealed code gen via the moshi
extension.
The Gradle plugin is published to Maven Central, so ensure you have mavenCentral()
visible to your buildscript
classpath.
plugins {
kotlin("jvm")
id("dev.zacsweers.moshix") version "x.y.z"
}
moshi {
// Opt-in to enable moshi-sealed, disabled by default.
enableSealed.set(true)
}
1.6.10
1.6.10-1.0.2
1.13.0
moshi-ksp
artifact has been upstreamed to Moshi itself as is no longer published.moshi-records-reflect
artifact has been upstreamed to Moshi itself as is no longer published.1.6.0
1.10.2
moshi-sealed-java-sealed-reflect
's support of sealed
classes in Java is now out of preview and requires Java 17 to use.moshi-records-reflect
still targets Java 16 for maximum compatibility.1.5.31
1.10.1
1.5.30-1.0.0
stable!moshi-sealed-ksp
has now been merged into moshi-sealed-codegen
. This artifact can be used for both kapt
andksp
.moshi-ksp
is now soft-deprecated and will be fully deprecated once Moshi's next release is out with formal support.1.5.30
.1.5.30-1.0.0-beta08
.RecordsJsonAdapterFactory
is now aligned with the upstreamed implementation on Moshi itself.
MethodHandle
and better unpacking of different runtime exceptions. Full details can be found in the PR.CodeBlock
s to avoid noisy logging.moshi-ksp
(see #125 and #151).Special thanks to @yigit for contributing to this release!
RecordsJsonAdapterFactory
now properly respects @JsonQualifier
annotations on components.RecordsJsonAdapterFactory
now supports non-public constructors (i.e. package or file-private).moshi-ksp
when dealing with generic typealias properties.1.5.21-1.0.0-beta07
.@JsonClass
-annotated Java file with a custom generator, moshi-ksp
would error@OptIn(ExperimentalStdLibApi::class)
annotations in moshi-sealed
when object
addAdapter
extension.Thanks to @gabrielittner for contributing to this release!
1.5.21-1.0.0-beta05
.1.5.21
.1.5.0
.1.9.0
.moshi-ksp
and moshi-sealed's codegen both support a new moshi.generateProguardRules
option. This can befalse
to disable proguard rule generation.-module-name
attribute set to their artifact ID to help avoid module nameThanks to @SeongUgJung and @slmlt for contributing to this release!