All Projects → jayrave → moshi-pristine-models

jayrave / moshi-pristine-models

Licence: Apache-2.0 license
Moshi add-on to programmatically define mapping between models & JSON

Programming Languages

kotlin
9241 projects

Labels

Projects that are alternatives of or similar to moshi-pristine-models

DemoApp
An Android template project for fast development and test.
Stars: ✭ 33 (+73.68%)
Mutual labels:  moshi
ResDelivery-Hilt-Coroutines-Mvvm-Single-Activity
This is a Sample Single Activity App (Multi Fragments) that uses Dagger-Hilt, Coroutines Flows, Paging 3 & Mvvm Clean Architecture
Stars: ✭ 28 (+47.37%)
Mutual labels:  moshi
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (+84.21%)
Mutual labels:  moshi
OLA Play Music App
Music Streaming App
Stars: ✭ 27 (+42.11%)
Mutual labels:  moshi
My Android Garage
A quick reference guide for Android development.
Stars: ✭ 66 (+247.37%)
Mutual labels:  moshi
Palette
Android application to get the #hexcode and rgb() values for any image
Stars: ✭ 31 (+63.16%)
Mutual labels:  moshi
moshi-gson-interop
An interop tool for safely mixing Moshi and Gson models in JSON serialization.
Stars: ✭ 39 (+105.26%)
Mutual labels:  moshi
Jsontokotlinclass
🚀 Plugin for Android Studio And IntelliJ Idea to generate Kotlin data class code from JSON text ( Json to Kotlin )
Stars: ✭ 2,438 (+12731.58%)
Mutual labels:  moshi
Foodium
It simply loads Posts data from API and stores it in persistence storage (i.e. SQLite Database). Posts will be always loaded from local database. Remote data (from API) and Local data is always synchronized.
Stars: ✭ 1,940 (+10110.53%)
Mutual labels:  moshi
Catchup
An app for catching up on things.
Stars: ✭ 1,690 (+8794.74%)
Mutual labels:  moshi
FootballNews2
Personal Android project for Football daily updates
Stars: ✭ 16 (-15.79%)
Mutual labels:  moshi
json-serialization-benchmarking
Miscellaneous benchmarks for JSON serialization on JVM/Android
Stars: ✭ 48 (+152.63%)
Mutual labels:  moshi
GitKtDroid
A sample Android application📱 built with Kotlin for #30DaysOfKotlin
Stars: ✭ 53 (+178.95%)
Mutual labels:  moshi
xing-android-sdk
The Official XING API client for Java/Android
Stars: ✭ 33 (+73.68%)
Mutual labels:  moshi

Moshi: Pristine Models Build Status Download

This is an add-on to Moshi which allows

  • to programmatically define mapping between models & JSON
  • to keep your models pristine => free of annotations & only concerned about the business logic

It is pretty easy to define the mappings!

data class User(val name: String, val age: Int)
class UserMapper : Mapper<User>() {
    val name = field(User::name, "user_name")
    val age = field(User::age, "user_age")
    
    override fun create(value: Value<User>) = User(value of name, value of age)
}

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

val factory = PristineModelsJsonAdapterFactory.Builder()
        .add(User::class.java, UserMapper()) // there is an API to allow lazy initialization of mappers too
        .build()
        
val moshi = 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

class User {
    private final String name;
    private final int age;
    
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
}
class UserMapper : Mapper<User>() {
    val name = field("user_field", false, object : PropertyExtractor<User, String> {
        override val type: Type = String::class.javaObjectType
        override fun extractFrom(t: User): String = t.getName()
    })

    val age = field("user_age", false, object : PropertyExtractor<User, Int> {
        override val type: Type = Int::class.javaPrimitiveType!!
        override fun extractFrom(t: User): Int = t.getAge()
    })

    override fun create(value: Value<User>): User {
        return User(value of name, value of age)
    }
}

Download

// This is usually in the top-level build.gradle file
allprojects {
    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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].