Add a mapping from Interface to Concrete type with Moshi and Kotlin

If you’re using Moshi for your JSON needs, you’ll eventually stumble upon the need to specify the concrete implementation of an interface. For example, you might have a contrived example such as: So how do we make sure the list of mammals in MammalList uses the concrete Human implementation? With a custom adapter! The implementation is simple – we just …

Binding Annotations with Guice and Kotlin

If you’re using kotlin-guice to use Guice with your Kotlin project, you might have run into problems with binding annotations. Annotations let you use a specific implementation of an interface when there are multiple implementations registered with Guice: The problem is Kotlin works slightly differently when it comes to annotations and figuring out how to make it work can be …

Moshi Adapters for Platform Types like ArrayList and LinkedList (Part 2)

Earlier we created a generic adapter that supported concrete collection types in Moshi. However, sometimes the type of the item within the collection isn’t available. We’ll need to create another adapter but there is a problem – Java type erasure. Even though we might know the type ahead of time in the code, by the time we get to runtime …

Moshi Adapters for Platform Types like ArrayList and LinkedList (Part 1)

If you’ve used Moshi for any length of time, you’ve probably come across an error like: Platform class java.util.ArrayList (with no annotations) requires explicit JsonAdapter to be registered This means Moshi can’t serialise or deserialise the collection because it’s not just a basic ‘List’. The error message tells you to create a custom JsonAdapter but the documentation doesn’t really tell …