Alex Lokkie
04/20/2018, 12:35 PMpackage org.example
import com.google.gson.annotations.SerializedName
import java.util.HashMap
import kotlin.reflect.full.declaredMemberProperties
class Example {
@SerializedName("some_var")
var someVar: String? = ""
@SerializedName("another_var")
var anotherVar: Int? = 1
fun toHashMap(): HashMap<String, Any?> {
return this::class.declaredMemberProperties.mapNotNull { field ->
if (field.annotations.any { it is SerializedName} ) {
field.annotations.find { it is SerializedName }?.let {
field.name to (it as SerializedName).value
}
} else null
}.toMap(HashMap())
}
}