Created
April 9, 2019 09:17
-
-
Save vbsteven/a0fa0769d5a2a9bdd2987285f532cc9d to your computer and use it in GitHub Desktop.
Gson constructor test in Kotlin
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 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