Merge pull request #91 from TouchInstinct/fix/replace-typename-with-canonicalName

Replace .typeName with .canonicalName
This commit is contained in:
Vladimir Shefer 2020-08-18 13:48:31 +03:00 committed by GitHub
commit 1f60055793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 5 deletions

View File

@ -6,15 +6,20 @@ import java.lang.reflect.Type
class DateFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
return when (type.typeName) {
DateTime::class.java.typeName -> DateTimeJsonAdapter(getPatterns(annotations))
LocalDate::class.java.typeName -> LocalDateJsonAdapter(getPatterns(annotations))
override fun create(type: Type, annotations: Set<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
val typeName = when(type) {
is Class<*> -> type.canonicalName
else -> type.toString()
}
return when (typeName) {
DateTime::class.java.canonicalName -> DateTimeJsonAdapter(getPatterns(annotations))
LocalDate::class.java.canonicalName -> LocalDateJsonAdapter(getPatterns(annotations))
else -> null
}
}
private fun getPatterns(annotations: MutableSet<out Annotation>) = annotations
private fun getPatterns(annotations: Set<out Annotation>) = annotations
.map { it as? Format }
.firstOrNull()
?.patterns