Skip to content

Instantly share code, notes, and snippets.

@JakeWharton JakeWharton/Json.kt Secret
Created Apr 11, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.