diff --git a/kotlin/src/main/java/com/squareup/moshi/KotlinJsonAdapter.kt b/kotlin/src/main/java/com/squareup/moshi/KotlinJsonAdapter.kt index f14faee..6f040e2 100644 --- a/kotlin/src/main/java/com/squareup/moshi/KotlinJsonAdapter.kt +++ b/kotlin/src/main/java/com/squareup/moshi/KotlinJsonAdapter.kt @@ -156,10 +156,9 @@ object KotlinJsonAdapterFactory : JsonAdapter.Factory { if (!annotations.isEmpty()) return null val rawType = Types.getRawType(type) - val platformType = ClassJsonAdapter.isPlatformType(rawType) - if (platformType) return null - + if (rawType.isEnum) return null if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null + if (ClassJsonAdapter.isPlatformType(rawType)) return null val constructor = rawType.kotlin.primaryConstructor ?: return null val parametersByName = constructor.parameters.associateBy { it.name } diff --git a/kotlin/src/test/java/com/squareup/moshi/KotlinJsonAdapterTest.kt b/kotlin/src/test/java/com/squareup/moshi/KotlinJsonAdapterTest.kt index da9e565..0fdefd0 100644 --- a/kotlin/src/test/java/com/squareup/moshi/KotlinJsonAdapterTest.kt +++ b/kotlin/src/test/java/com/squareup/moshi/KotlinJsonAdapterTest.kt @@ -178,7 +178,7 @@ class KotlinJsonAdapterTest { class AbsentNull(var a: Int?, var b: Int?) @Test fun repeatedValue() { - val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build() + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build() val jsonAdapter = moshi.adapter(RepeatedValue::class.java) try { @@ -395,7 +395,7 @@ class KotlinJsonAdapterTest { } @Test fun privateConstructor() { - val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build() + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build() val jsonAdapter = moshi.adapter(PrivateConstructor::class.java) val encoded = PrivateConstructor.newInstance(3, 5) @@ -446,7 +446,7 @@ class KotlinJsonAdapterTest { } @Test fun unsettableProperty() { - val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build() + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build() try { moshi.adapter(UnsettableProperty::class.java) fail() @@ -462,7 +462,7 @@ class KotlinJsonAdapterTest { } @Test fun nonPropertyConstructorParameter() { - val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build() + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build() try { moshi.adapter(NonPropertyConstructorParameter::class.java) fail() @@ -475,6 +475,19 @@ class KotlinJsonAdapterTest { class NonPropertyConstructorParameter(a: Int, val b: Int) + @Test fun kotlinEnumsAreNotCovered() { + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build() + val adapter = moshi.adapter(UsingEnum::class.java) + + assertThat(adapter.fromJson("""{"e": "A"}""")).isEqualTo(UsingEnum(KotlinEnum.A)) + } + + data class UsingEnum(val e: KotlinEnum) + + enum class KotlinEnum { + A, B + } + // TODO(jwilson): resolve generic types? @Retention(RUNTIME)