Last active
September 30, 2019 21:42
-
-
Save MoshDev/0bb52ff2c55fa28f70be5c1797804749 to your computer and use it in GitHub Desktop.
Gson List Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException | |
import java.util.ArrayList | |
import com.google.gson.Gson | |
import com.google.gson.TypeAdapter | |
import com.google.gson.reflect.TypeToken | |
import com.google.gson.stream.JsonReader | |
import com.google.gson.stream.JsonToken | |
import com.google.gson.stream.JsonWriter | |
class ListAdapter<T>(private val clazz: Class<T>, private val gson: Gson) : TypeAdapter<List<T>>() { | |
@Throws(IOException::class) | |
override fun read(reader: JsonReader): List<T>? = when (reader.peek()) { | |
JsonToken.BEGIN_OBJECT -> { | |
val item = gson.fromJson<T>(reader, clazz) | |
listOf(item) | |
} | |
JsonToken.BEGIN_ARRAY -> { | |
val list = ArrayList<T>() | |
reader.beginArray() | |
while (reader.hasNext()) { | |
val item = gson.fromJson<T>(reader, clazz) | |
list.add(item) | |
} | |
reader.endArray() | |
list | |
} | |
else -> null | |
} | |
@Throws(IOException::class) | |
override fun write(out: JsonWriter, collection: List<T>?) { | |
if (collection == null) { | |
out.nullValue() | |
return | |
} | |
val elementTypeAdapter = gson.getAdapter(object : TypeToken<T>() { | |
}) | |
out.beginArray() | |
for (element in collection) { | |
elementTypeAdapter.write(out, element) | |
} | |
out.endArray() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.ParameterizedType | |
import com.google.gson.Gson | |
import com.google.gson.TypeAdapter | |
import com.google.gson.TypeAdapterFactory | |
import com.google.gson.reflect.TypeToken | |
class ListAdapterFactory : TypeAdapterFactory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? { | |
return if (type.rawType == List::class.java) { | |
try { | |
val clazz: Class<T> = (type.type as ParameterizedType).actualTypeArguments[0] as Class<T> | |
ListAdapter(clazz, gson) as TypeAdapter<T> | |
} catch (e: Exception) { | |
null | |
} | |
} else { | |
null | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//to register the adapter | |
val gson = GsonBuilder().registerTypeAdapterFactory(ListAdapterFactory()).create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment