In the new Release I was trying to make use of the EnumJsonAdapter, but it crashes with the message
java.lang.IllegalArgumentException: Expected at least one @ToJson or @FromJson method on com.squareup.moshi.adapters.EnumJsonAdapter
I have added it to my MoshiBuilder as described in the release notes, but maybe it is an error related to Kotlin?
Moshi.Builder()
// a lot of other adapters
.add(EnumJsonAdapter.create<ContentType>(ContentType::class.java).withUnknownFallback(null))
.build()
The enum looks like this:
enum class ContentType {
headline, text, video //, ...
}
.add(ContentType.class, EnumJsonAdapter.create<ContentType>(ContentType::class.java).withUnknownFallback(null)) is what you're looking for.
the other overload is for the method adapters that have @ToJson/@FromJson methods.
This makes me wonder if we should have a factory() method to get a JsonAdapter.Factory easily: EnumJsonAdapter.create<ContentType>(ContentType::class.java).withUnknownFallback(null).factory().
would prevent the duplication of the class parameter.
Or we could change it to implement both JsonAdapter and JsonAdapter.Factory!
I experimented with making registration as a Factory work, and it鈥檚 unsatisfying. Instead I鈥檓 going to document installation instructions for all of our adapters.
https://github.com/square/moshi/pull/705
Most helpful comment
.add(ContentType.class, EnumJsonAdapter.create<ContentType>(ContentType::class.java).withUnknownFallback(null))is what you're looking for.the other overload is for the method adapters that have
@ToJson/@FromJsonmethods.