Skip to content

Instantly share code, notes, and snippets.

@sagar-viradiya
Created October 10, 2020 09:09
Show Gist options
  • Save sagar-viradiya/ce8f765f072e38ad92d84a4b34add0f4 to your computer and use it in GitHub Desktop.
Save sagar-viradiya/ce8f765f072e38ad92d84a4b34add0f4 to your computer and use it in GitHub Desktop.
DevFest India 2020 Day 3 - Type converter
import androidx.room.TypeConverter
import com.devfest.india.bmsclone.data.local.database.entity.Movie
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
object MovieTypeConverter {
@TypeConverter
@JvmStatic
fun fromList(value: List<Movie>) = Gson().toJson(value)
@TypeConverter
@JvmStatic
fun toList(value: String): List<Movie> {
val listType: Type = object : TypeToken<List<Movie>>() {}.type
return Gson().fromJson(value, listType)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment