Skip to content

Instantly share code, notes, and snippets.

@ZacSweers ZacSweers/Complex.kt
Last active Sep 30, 2019

Embed
What would you like to do?
@JsonClass(generateAdapter = true)
data class Foo<T>(
@Json(name = "barString") val bar: String,
val defaultValue: Int = 0,
val nullableString: String?,
val typeParam: T? = null,
val tList: List<T>
)
// Generated by Moshi Kotlin Code Gen
class FooJsonAdapter<T>(moshi: Moshi, types: Array<Type>) : JsonAdapter<Foo<T>>() {
private val options: JsonReader.Options =
JsonReader.Options.of("barString", "defaultValue", "nullableString", "typeParam", "tList")
private val stringAdapter: JsonAdapter<String> = moshi.adapter(String::class.java).nonNull()
private val intAdapter: JsonAdapter<Int> = moshi.adapter(Int::class.java).nonNull()
private val nullableStringAdapter: JsonAdapter<String?> =
moshi.adapter(String::class.java).nullSafe()
private val nullableTAdapter: JsonAdapter<T?> = moshi.adapter<T?>(types[0]).nullSafe()
private val listOfTAdapter: JsonAdapter<List<T>> =
moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, types[0])).nonNull()
override fun toString(): String = "GeneratedJsonAdapter(Foo)"
override fun fromJson(reader: JsonReader): Foo<T> {
var bar: String? = null
var defaultValue: Int? = null
var nullableString: String? = null
var typeParam: T? = null
var typeParamSet: Boolean = false
var tList: List<T>? = null
reader.beginObject()
while (reader.hasNext()) {
when (reader.selectName(options)) {
0 -> bar = stringAdapter.fromJson(reader) ?: throw JsonDataException("Non-null value 'bar' was null at ${reader.path}")
1 -> defaultValue = intAdapter.fromJson(reader) ?: throw JsonDataException("Non-null value 'defaultValue' was null at ${reader.path}")
2 -> nullableString = nullableStringAdapter.fromJson(reader)
3 -> {
typeParam = nullableTAdapter.fromJson(reader)
typeParamSet = true
}
4 -> tList = listOfTAdapter.fromJson(reader) ?: throw JsonDataException("Non-null value 'tList' was null at ${reader.path}")
-1 -> {
// Unknown name, skip it.
reader.skipName()
reader.skipValue()
}
}
}
reader.endObject()
var result = Foo<T>(
bar = bar ?: throw JsonDataException("Required property 'bar' missing at ${reader.path}"),
nullableString = nullableString,
tList = tList ?: throw JsonDataException("Required property 'tList' missing at ${reader.path}"))
result = result.copy(
defaultValue = defaultValue ?: result.defaultValue,
typeParam = if (typeParamSet) typeParam else result.typeParam)
return result
}
override fun toJson(writer: JsonWriter, value: Foo<T>?) {
if (value == null) {
throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
}
writer.beginObject()
writer.name("barString")
stringAdapter.toJson(writer, value.bar)
writer.name("defaultValue")
intAdapter.toJson(writer, value.defaultValue)
writer.name("nullableString")
nullableStringAdapter.toJson(writer, value.nullableString)
writer.name("typeParam")
nullableTAdapter.toJson(writer, value.typeParam)
writer.name("tList")
listOfTAdapter.toJson(writer, value.tList)
writer.endObject()
}
}
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.