dave08
03/27/2019, 4:22 PMclass IdAdapter {
private val logger = KotlinLogging.logger {}
@FromJson
@OdooIdentityId
fun fromJson(value: Any): Int? = when (value) {
is Boolean -> null
is Double -> value.toString().toInt()
is Int -> value
is ArrayList<*> -> checkArrayValue(value.first()!!)
else -> throw IllegalStateException("Non-object-non-boolean value for @OdooIdentityId field $value")
}
@ToJson
fun toJson(@OdooIdentityId value: Int?): String = TODO()
private fun checkArrayValue(value: Any): Int? = when(value) {
is Boolean -> null
is Double -> value.toInt()
is Int -> value
else -> error("Invalid Odoo identity $value")
}
}
bdawg.io
03/27/2019, 4:34 PMdave08
03/27/2019, 4:35 PMdave08
03/27/2019, 4:35 PMMoshi.Builder().add(IdAdapter()).build()
bdawg.io
03/27/2019, 4:36 PMdave08
03/27/2019, 4:36 PMbdawg.io
03/27/2019, 4:39 PM@OdooIdentityId value: Int?
apply the annotation to the parameter or to the type? IIRC moshi expects the type to be annotated. The example on the README is in java so it doesn’t really help lolbdawg.io
03/27/2019, 4:41 PMclass UppercaseJsonAdapter {
@ToJson fun toJson(@Uppercase s: String) : String {
return s.toUpperCase(<http://Locale.US|Locale.US>)
}
@FromJson @Uppercase fun fromJson(s: String) : String {
return s.toLowerCase(<http://Locale.US|Locale.US>)
}
}
https://github.com/square/moshi/blob/master/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.ktbdawg.io
03/27/2019, 5:34 PM