Moshi Kotlin Extensions

Additional

Language
Kotlin
Version
1.0.3 (Nov 22, 2021)
Created
Dec 25, 2020
Updated
Nov 22, 2021 (Retired)
Owner
Mazen Rashed (mazenrashed)
Contributor
Mazen Rashed (mazenrashed)
1
Activity
Badge
Generate
Download
Source code

Promotion

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" class to explan the examples

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.