apply plugin: 'com.android.application'
apply plugin: 'kotlin-platform-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.cren90.sandbox"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.9.2'
implementation 'com.squareup.moshi:moshi-adapters:1.11.0'
implementation 'dev.zacsweers.moshix:moshi-sealed-runtime:0.6.1'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.2'
kapt 'dev.zacsweers.moshix:moshi-sealed-codegen:0.6.1'
}
package com.cren90.sandbox.moshisealed
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import dev.zacsweers.moshix.sealed.annotations.TypeLabel
import dev.zacsweers.moshix.sealed.annotations.DefaultObject
import java.util.*
@JsonClass(generateAdapter = true,
generator = "sealed:object_type")
//@DefaultNull //Not working
sealed class BaseType {
@TypeLabel("type1")
@JsonClass(generateAdapter = true)
data class Type1(
@Json(name = "credential") val credential: Type1Credential,
@Json(name = "id") override val id: Int
// more fields here
) : BaseType()
@TypeLabel("type2")
@JsonClass(generateAdapter = true)
data class Type2(
@Json(name = "credential") val credential: Type2Credential,
@Json(name = "id") override val id: Int
// more fields here
) : BaseType()
@DefaultObject // Not working
object UnknownType: BaseType() {
override val id: Int = -1
// more fields here
}
abstract val id: Int
// more fields here
}
@JsonClass(generateAdapter = true)
data class Type1Credential(
@Json(name = "device_public_key") val devicePublicKey: String,
@Json(name = "device_serial") val deviceSerial: String,
@Json(name = "device_uuid") val deviceUUID: UUID,
@Json(name = "user_private_key") val userPrivateKey: String,
@Json(name = "user_uuid") val userUUID: UUID
)
data class Type2Credential(
@Json(name = "key") val key: String
)
// Code generated by moshi-sealed. Do not edit.
package com.cren90.sandbox.moshisealed
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.emptySet
public class BaseTypeJsonAdapter(
moshi: Moshi
) : JsonAdapter<BaseType>() {
@Suppress("UNCHECKED_CAST")
private val runtimeAdapter: JsonAdapter<BaseType> =
PolymorphicJsonAdapterFactory.of(BaseType::class.java, "object_type")
.withSubtype(BaseType.Type1::class.java, "type1")
.withSubtype(BaseType.Type2::class.java, "type2")
.withDefaultValue(BaseType.UnknownType) // When using @DefaultNull, BaseType.UnknownType is replaced with null here
.create(BaseType::class.java, emptySet(), moshi) as JsonAdapter<BaseType>
public override fun fromJson(reader: JsonReader): BaseType? = runtimeAdapter.fromJson(reader)
public override fun toJson(writer: JsonWriter, value: BaseType?): Unit {
runtimeAdapter.toJson(writer, value)
}
}
e: /Users/chris/Git/sandbox/appSandbox/build/generated/source/kapt/debug/com/cren90/sandbox/moshisealed/BaseTypeJsonAdapter.kt: (21, 10): Unresolved reference: withDefaultValue
e: /Users/chris/Git/sandbox/appSandbox/build/generated/source/kapt/debug/com/cren90/sandbox/moshisealed/BaseTypeJsonAdapter.kt: (22, 39): Type inference failed: Not enough information to infer parameter T in fun <T> emptySet(): Set<T>
Please specify it explicitly.