-
-
Save JakeWharton/6d08b7fb74c320b048db68e21912d878 to your computer and use it in GitHub Desktop.
Full code example from https://jakewharton.com/r8-optimization-method-outlining/
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 java.io.StringReader | |
import java.time.OffsetDateTime | |
import android.util.JsonReader | |
import android.util.JsonToken | |
fun main() { | |
println(TweetParser.fromJson(JsonReader(StringReader("")))) | |
println(UserParser.fromJson(JsonReader(StringReader("")))) | |
} | |
data class User( | |
val id: String, | |
val username: String, | |
val displayName: String, | |
val email: String, | |
val created: OffsetDateTime, | |
val isPublic: Boolean | |
) | |
data class Tweet( | |
val id: String, | |
val userId: String, | |
val content: String, | |
val created: OffsetDateTime | |
) | |
object UserParser { | |
fun fromJson(reader: JsonReader): User { | |
var id: String? = null | |
var username: String? = null | |
var displayName: String? = null | |
var email: String? = null | |
var created: OffsetDateTime? = null | |
var isPublic: Boolean? = null | |
reader.beginObject() | |
while (reader.peek() != JsonToken.END_OBJECT) { | |
when (reader.nextName()) { | |
"id" -> id = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("id") | |
.append("' was null at ").append(reader).toString()) | |
"username" -> username = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("username") | |
.append("' was null at ").append(reader).toString()) | |
"displayName" -> displayName = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("displayName") | |
.append("' was null at ").append(reader).toString()) | |
"email" -> email = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("email") | |
.append("' was null at ").append(reader).toString()) | |
"created" -> created = reader.nextString()?.let(OffsetDateTime::parse) ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("created") | |
.append("' was null at ").append(reader).toString()) | |
"isPublic" -> isPublic = reader.nextString()?.toBoolean() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("isPublic") | |
.append("' was null at ").append(reader).toString()) | |
else -> reader.skipValue() | |
} | |
} | |
reader.endObject() | |
return User( | |
id = id ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("id") | |
.append("' missing at ").append(reader).toString()), | |
username = username ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("username") | |
.append("' missing at ").append(reader).toString()), | |
displayName = displayName ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("displayName") | |
.append("' missing at ").append(reader).toString()), | |
email = email ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("email") | |
.append("' missing at ").append(reader).toString()), | |
created = created ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("created") | |
.append("' missing at ").append(reader).toString()), | |
isPublic = isPublic ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("isPublic") | |
.append("' missing at ").append(reader).toString()) | |
) | |
} | |
} | |
object TweetParser { | |
fun fromJson(reader: JsonReader): Tweet { | |
var id: String? = null | |
var userId: String? = null | |
var content: String? = null | |
var created: OffsetDateTime? = null | |
reader.beginObject() | |
while (reader.peek() != JsonToken.END_OBJECT) { | |
when (reader.nextName()) { | |
"id" -> id = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("id") | |
.append("' was null at ").append(reader).toString()) | |
"userId" -> userId = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("userId") | |
.append("' was null at ").append(reader).toString()) | |
"content" -> content = reader.nextString() ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("content") | |
.append("' was null at ").append(reader).toString()) | |
"created" -> created = reader.nextString()?.let(OffsetDateTime::parse) ?: | |
throw IllegalStateException( | |
StringBuilder("Non-null value '").append("created") | |
.append("' was null at ").append(reader).toString()) | |
else -> reader.skipValue() | |
} | |
} | |
reader.endObject() | |
return Tweet( | |
id = id ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("id") | |
.append("' missing at ").append(reader).toString()), | |
userId = userId ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("userId") | |
.append("' missing at ").append(reader).toString()), | |
content = content ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("content") | |
.append("' missing at ").append(reader).toString()), | |
created = created ?: throw IllegalStateException( | |
StringBuilder("Required property '").append("created") | |
.append("' missing at ").append(reader).toString()) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment