Skip to content

Commit

Permalink
Fix Nullability warning in JsonAdapters
Browse files Browse the repository at this point in the history
Updating generated methods `toJson` to accept a non null `JsonWriter`.
Previously the IDE was raising an issue due to different method signature.

Fixes #74
  • Loading branch information
cortinico committed Jan 1, 2020
1 parent 01fbfbf commit c2f416d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions plugin/src/main/resources/kotlin/tools/TypesAdapters.kt.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {
override fun fromNonNullString(nextString: String) : LocalDate = LocalDate.parse(nextString, formatter)
override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)
override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import com.squareup.moshi.Json

/**
* Represents a specific Pet in the store
* @property id Unique ID of this Pet
* @property category Optional category of the pet
* @property id Unique ID of this Pet
* @property name Name of this specific pet
* @property photoUrls Photo URls for this Pet on the bucket
* @property tags Pet status in the store
*/
data class Pet(
@Json(name = "name") @field:Json(name = "name") var name: String,
@Json(name = "photoUrls") @field:Json(name = "photoUrls") var photoUrls: List<String>,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "category") @field:Json(name = "category") var category: Category? = null,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "tags") @field:Json(name = "tags") var tags: List<Tag>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.math.BigDecimal
* Moshi Factory to handle all the custom types we want to support,
* such as [LocalDate], [ZonedDateTime], [BigDecimal].
*/
internal class TypesAdapterFactory : JsonAdapter.Factory {
class TypesAdapterFactory : JsonAdapter.Factory {
private val types = mapOf<Type, JsonAdapter<*>>(
LocalDate::class.java to LocalDateAdapter(),
ZonedDateTime::class.java to ZonedDateTimeAdapter(),
Expand Down Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {

override fun fromNonNullString(nextString: String): LocalDate = LocalDate.parse(nextString, formatter)

override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)

override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import java.lang.reflect.Type

internal class XNullableAdapterFactory : JsonAdapter.Factory {
class XNullableAdapterFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
if (annotations.any { it is XNullable }) {
return object : JsonAdapter<Any>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {

override fun fromNonNullString(nextString: String): LocalDate = LocalDate.parse(nextString, formatter)

override fun toJson(writer: JsonWriter?, value: LocalDate?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
}
}

Expand All @@ -76,15 +76,15 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
}
}

override fun toJson(writer: JsonWriter?, value: ZonedDateTime?) {
value?.let { writer?.value(it.format(formatter)) }
override fun toJson(writer: JsonWriter, value: ZonedDateTime?) {
value?.let { writer.value(it.format(formatter)) }
}
}

internal class BigDecimalJsonAdapter : XNullableJsonAdapter<BigDecimal>() {
override fun fromNonNullString(nextString: String) = BigDecimal(nextString)

override fun toJson(writer: JsonWriter?, value: BigDecimal?) {
value?.let { writer?.value(it) }
override fun toJson(writer: JsonWriter, value: BigDecimal?) {
value?.let { writer.value(it) }
}
}

0 comments on commit c2f416d

Please sign in to comment.