Once the mappings have been defined, Moshi has to be taught about these

valfactory=PristineModelsJsonAdapterFactory.Builder().add(User::class.java,UserMapper())// there is an API to allow lazy initialization of mappers too
.build()valmoshi=Moshi.Builder().add(factory)// anything else you wanna teach moshi about
.build()

Voila! Now Moshi knows how to map User to/from this JSON

{"user_name":"John Muir","user_age":76}

This raises some questions:

What if the models are written in Java?

What if the models have private properties?

Well, there is a way to handle those situations too (considering there are public getters & and a public constructor). Let us consider that the same User model we saw above is now written in Java. The mapping is a little bit more involved, but nevertheless possible

Download

// This is usually in the top-level build.gradle fileallprojects{repositories{jcenter()// Since the JAR lives in Bintray's jCenter}}dependencies{compile"com.jayrave:moshi-pristine-models:$version"}

Check this out

If you like keeping your models clean, you may be interested in checking out another library => Falkon (Disclaimer: I am the author), which helps to keep your models free of database/ORM specific annotations. Like this library, Falkon also enables to programmatically define the mapping between models & database records