Git Product home page Git Product logo

moshi-pristine-models's Introduction

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

moshi-pristine-models's People

Contributors

jayrave avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.