Make RuntimeJsonAdapterFactory internal and cleanup

This commit is contained in:
Benoit Marty 2022-04-12 15:18:15 +02:00 committed by Benoit Marty
parent 7514edb399
commit 4e48c076e8
1 changed files with 14 additions and 15 deletions

View File

@ -16,19 +16,13 @@
package org.matrix.android.sdk.internal.network.parsing package org.matrix.android.sdk.internal.network.parsing
import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonAdapter
import org.matrix.android.sdk.internal.network.parsing.RuntimeJsonAdapterFactory
import com.squareup.moshi.Moshi
import org.matrix.android.sdk.internal.network.parsing.RuntimeJsonAdapterFactory.RuntimeJsonAdapter
import kotlin.Throws
import com.squareup.moshi.JsonDataException import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonReader import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types import com.squareup.moshi.Types
import java.io.IOException import java.io.IOException
import java.lang.IllegalArgumentException
import java.lang.NullPointerException
import java.lang.reflect.Type import java.lang.reflect.Type
import java.util.LinkedHashMap
import javax.annotation.CheckReturnValue import javax.annotation.CheckReturnValue
/** /**
@ -36,8 +30,12 @@ import javax.annotation.CheckReturnValue
* decoding the JSON. This factory's adapters expect JSON in the format of a JSON object with a * decoding the JSON. This factory's adapters expect JSON in the format of a JSON object with a
* key whose value is a label that determines the type to which to map the JSON object. * key whose value is a label that determines the type to which to map the JSON object.
*/ */
class RuntimeJsonAdapterFactory<T> internal constructor(val baseType: Class<T>, val labelKey: String, val fallbackType: Class<*>?) : JsonAdapter.Factory { internal class RuntimeJsonAdapterFactory<T>(
val labelToType: MutableMap<String, Type> = LinkedHashMap() private val baseType: Class<T>,
private val labelKey: String,
private val fallbackType: Class<*>
) : JsonAdapter.Factory {
private val labelToType: MutableMap<String, Type> = LinkedHashMap()
/** /**
* Register the subtype that can be created based on the label. When an unknown type is found * Register the subtype that can be created based on the label. When an unknown type is found
@ -69,8 +67,11 @@ class RuntimeJsonAdapterFactory<T> internal constructor(val baseType: Class<T>,
objectJsonAdapter, fallbackAdapter).nullSafe() objectJsonAdapter, fallbackAdapter).nullSafe()
} }
internal class RuntimeJsonAdapter(val labelKey: String, val labelToAdapter: Map<String, JsonAdapter<Any>>, @Suppress("UNCHECKED_CAST")
val typeToLabel: Map<Type, String>, val objectJsonAdapter: JsonAdapter<Any>, internal class RuntimeJsonAdapter(val labelKey: String,
val labelToAdapter: Map<String, JsonAdapter<Any>>,
val typeToLabel: Map<Type, String>,
val objectJsonAdapter: JsonAdapter<Any>,
val fallbackAdapter: JsonAdapter<Any>) : JsonAdapter<Any?>() { val fallbackAdapter: JsonAdapter<Any>) : JsonAdapter<Any?>() {
@Throws(IOException::class) @Throws(IOException::class)
override fun fromJson(reader: JsonReader): Any? { override fun fromJson(reader: JsonReader): Any? {
@ -117,9 +118,7 @@ class RuntimeJsonAdapterFactory<T> internal constructor(val baseType: Class<T>,
* JSON object. * JSON object.
*/ */
@CheckReturnValue @CheckReturnValue
fun <T> of(baseType: Class<T>?, labelKey: String?, fallbackType: Class<out T>?): RuntimeJsonAdapterFactory<T> { fun <T> of(baseType: Class<T>, labelKey: String, fallbackType: Class<out T>): RuntimeJsonAdapterFactory<T> {
if (baseType == null) throw NullPointerException("baseType == null")
if (labelKey == null) throw NullPointerException("labelKey == null")
require(baseType != Any::class.java) { "The base type must not be Object. Consider using a marker interface." } require(baseType != Any::class.java) { "The base type must not be Object. Consider using a marker interface." }
return RuntimeJsonAdapterFactory(baseType, labelKey, fallbackType) return RuntimeJsonAdapterFactory(baseType, labelKey, fallbackType)
} }