Moshi Kotlin Extensions

Additional

Language
Kotlin
Version
1.1.0 (Jan 23, 2023)
Created
Dec 24, 2020
Updated
Jan 23, 2023
Owner
Mazen Rashed (mazenrashed)
Contributor
Mazen Rashed (mazenrashed)
1
Activity
Badge
Generate
Download
Source code

Moshi Kotlin Extensions

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

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.