Skip to content

Kotlin extensions for Moshi, Make every thing with square / Moshi in one line.

Notifications You must be signed in to change notification settings

mazenrashed/MoshiKotlinExtensions

Repository files navigation

Moshi Kotlin Extensions

Kotlin extensions for Moshi, Make every thing you want with Moshi in just one line.

Android Arsenal

Add the JitPack repository to your build file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add dependency

dependencies {
    implementation 'com.github.mazenrashed:MoshiKotlinExtensions:${LAST_VERSION}'
}

We have "Student" as example

data class Student(var name: String)

Deserialize Json object

private val studentJson = "{\"name\":\"mazen\"}"
val student: Student? = studentJson.deserialize<Student>()

Deserialize Json array

private val studentsJson = "[{\"name\":\"Mazen\"},{\"name\":\"Mohammad\"}]"
val students: List<Student>? = studentsJson.deserializeList()

Serialize Json

private val student: Student = Student("mazen")
val serializedObject = student.serialize()

Check if you can convert json to a particular type

val canConvertStudentJsonToStudent: Boolean = studentJson.canConvertTo(Student::class.java) //true

If you have a custom Moshi instance you want to use it (For example: To apply Moshi adapters)

MoshiExtensions.init(YOUR INSTANCE)

Contributing

We welcome contributions!

  • ⇄ Pull requests and ★ Stars are always welcome.

About

Kotlin extensions for Moshi, Make every thing with square / Moshi in one line.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages