Skip to content

Instantly share code, notes, and snippets.

@vbsteven
Created April 9, 2019 09:17
Show Gist options
  • Save vbsteven/a0fa0769d5a2a9bdd2987285f532cc9d to your computer and use it in GitHub Desktop.
Save vbsteven/a0fa0769d5a2a9bdd2987285f532cc9d to your computer and use it in GitHub Desktop.
Gson constructor test in Kotlin
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
val json = """
{
"History":
[
{"Added": 2000}
]
}
"""
data class History(
@SerializedName("Added")
val addedTimeInSeconds : Long
) {
init {
println("@@@@ in constructor addedTimeInSeconds: $addedTimeInSeconds")
}
val addedTimeInMillis : Long = addedTimeInSeconds * 1000
}
data class HistoryResponse(
@SerializedName("History")
val historyList: List<History>
)
fun main() {
val response = Gson().fromJson(json, HistoryResponse::class.java)
println(response)
println("added time in millis ${response.historyList.first().addedTimeInMillis}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment