mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #286 from square/jwilson.0421.enums
KotlinJsonAdapter shouldn't convert enums.
This commit is contained in:
@@ -156,10 +156,9 @@ object KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
if (!annotations.isEmpty()) return null
|
if (!annotations.isEmpty()) return null
|
||||||
|
|
||||||
val rawType = Types.getRawType(type)
|
val rawType = Types.getRawType(type)
|
||||||
val platformType = ClassJsonAdapter.isPlatformType(rawType)
|
if (rawType.isEnum) return null
|
||||||
if (platformType) return null
|
|
||||||
|
|
||||||
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
|
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
|
||||||
|
if (ClassJsonAdapter.isPlatformType(rawType)) return null
|
||||||
|
|
||||||
val constructor = rawType.kotlin.primaryConstructor ?: return null
|
val constructor = rawType.kotlin.primaryConstructor ?: return null
|
||||||
val parametersByName = constructor.parameters.associateBy { it.name }
|
val parametersByName = constructor.parameters.associateBy { it.name }
|
||||||
|
@@ -178,7 +178,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class AbsentNull(var a: Int?, var b: Int?)
|
class AbsentNull(var a: Int?, var b: Int?)
|
||||||
|
|
||||||
@Test fun repeatedValue() {
|
@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)
|
val jsonAdapter = moshi.adapter(RepeatedValue::class.java)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -395,7 +395,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun privateConstructor() {
|
@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 jsonAdapter = moshi.adapter(PrivateConstructor::class.java)
|
||||||
|
|
||||||
val encoded = PrivateConstructor.newInstance(3, 5)
|
val encoded = PrivateConstructor.newInstance(3, 5)
|
||||||
@@ -446,7 +446,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun unsettableProperty() {
|
@Test fun unsettableProperty() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
try {
|
try {
|
||||||
moshi.adapter(UnsettableProperty::class.java)
|
moshi.adapter(UnsettableProperty::class.java)
|
||||||
fail()
|
fail()
|
||||||
@@ -462,7 +462,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun nonPropertyConstructorParameter() {
|
@Test fun nonPropertyConstructorParameter() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
try {
|
try {
|
||||||
moshi.adapter(NonPropertyConstructorParameter::class.java)
|
moshi.adapter(NonPropertyConstructorParameter::class.java)
|
||||||
fail()
|
fail()
|
||||||
@@ -475,6 +475,19 @@ class KotlinJsonAdapterTest {
|
|||||||
|
|
||||||
class NonPropertyConstructorParameter(a: Int, val b: Int)
|
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?
|
// TODO(jwilson): resolve generic types?
|
||||||
|
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
|
Reference in New Issue
Block a user