Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2016 01:07
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Moshi custom type adapter on Kotlin
class RedditLink(
val author: String,
val likes: RedditLikes,
val title: String,
val score: Int
enum class RedditLikes {
class LikesAdapter {
@FromJson fun fromJson(likes: String): RedditLikes {
Log.d("LikesAdapter", "I was called!!")
when (likes) {
"true" -> return RedditLikes.UP
"false" -> return RedditLikes.DOWN
"null" -> return RedditLikes.NONE
else -> {
throw JsonDataException("likes could not be parsed: " + likes)
@ToJson fun toJson(likes: RedditLikes) = when(likes) {
RedditLikes.UP -> "true"
RedditLikes.DOWN -> "false"
RedditLikes.NONE -> "null"
val moshi = Moshi.Builder()
val retrofit = Retrofit.Builder()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment