Heya,
I have the following code. The WelcomeType field works fine and the adapter to convert the int to enum gets called and works like expected. However strange enough the adapter for the customTheme field is not called at all and moshi attempts to build the CustomTheme object directly from the json.
Here is the code:
@SuppressLint("ParcelCreator")
@Parcelize
data class Course(
val id: Long,
@Json(name="welcome_type") val welcomeType: WelcomeType?,
// Other fields...
@Json(name="mobile_stylesheet")val theme: CustomTheme
): Parcelable {
enum class WelcomeType(val type: Int) {
None(1),
Image(2),
Video(3),
Both(4);
companion object {
private val map = WelcomeType.values().associateBy(WelcomeType::type)
fun fromInt(type: Int) = map[type]
}
}
// Gets called
class WelcomeTypeAdapter {
@ToJson fun toJson(type: WelcomeType): Int {
return type.type
}
@FromJson fun fromJson(value: Int): WelcomeType {
return WelcomeType.fromInt(value) ?: WelcomeType.None
}
}
@Parcelize
data class CustomTheme(
val primary: Int,
val primaryDark: Int,
val secondary: Int,
val contrast: Int,
val contrastText: Int
): Parcelable
@Parcelize
data class ThemeJson (
val primary: String?,
@Json(name="primary_dark") val primaryDark: String?,
val secondary: String?,
val contrast: String?,
@Json(name="contrast_text") val contrastText: String?
): Parcelable
// Does not get called
class ThemeJsonAdapter {
@ToJson fun toJson(theme: CustomTheme): ThemeJson {
Log.i("sdfsdfg", "sdfgsdghsfghsdfhg")
return ThemeJson(
java.lang.Integer.toHexString(theme.primary),
java.lang.Integer.toHexString(theme.primaryDark),
java.lang.Integer.toHexString(theme.secondary),
java.lang.Integer.toHexString(theme.contrast),
java.lang.Integer.toHexString(theme.contrastText)
)
}
@FromJson fun fromJson(value: ThemeJson): CustomTheme {
// Defaults to company theme colors if not set
Log.i("sdfsdfg", "sdfgsdghsfghsdfhg")
return CustomTheme(
Color.parseColor(value.primary ?: "#404A58"),
Color.parseColor(value.primaryDark ?: "#404A58"),
Color.parseColor(value.secondary ?: "#404A58"),
Color.parseColor(value.contrast ?: "#FD7060"),
Color.parseColor(value.contrastText ?: "#FD7060")
)
}
}
}
// building Moshi
return Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Course.ThemeJsonAdapter())
.add(Course.WelcomeTypeAdapter())
// Other adapters...
.build()
I as you see I'm doing this to convert the colors to ints. I also attempted the approach with the @JsonQualifier on the site but that adapter also did not get called.
So I must be doing something very stupid wrong here but I have no idea what it is. Can you help me out with this?
Cheers!
Try adding Course.WelcomeTypeAdapter() before the KotlinJsonAdapterFactory.
Factories are executed sequentially in order of addition, so the KotlinJsonAdapterFactory in this case has higher priority when moshi tries to get the right adapter.
Thank you sir! You just ended a gruwling 4 hours of frustration.. :)
Should this be documented better? I can see this being confusing for anyone unfamiliar with the chain. Maybe just something in the README reminding you to add the KotlinJsonAdapterFactory last?
Sure. I keep meaning to write a blog post on this delegating architecture
that's in all our libs.
On Fri, Sep 22, 2017, 2:39 PM Eric Cochran notifications@github.com wrote:
Should this be documented better? I can see this being confusing for
anyone unfamiliar with the chain. Maybe just something in the README
reminding you to add the KotlinJsonAdapterFactory last?—
You are receiving this because you modified the open/close state.Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/351#issuecomment-331528413, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEYFBh6NeEBa5Ifo-eZ_REUNuCYT3ks5sk_79gaJpZM4PgsSQ
.
Most helpful comment
Sure. I keep meaning to write a blog post on this delegating architecture
that's in all our libs.
On Fri, Sep 22, 2017, 2:39 PM Eric Cochran notifications@github.com wrote: