The Android Arsenal – JSON


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.

Logo
Enable registration in settings - general
Compare items
  • Total (0)
Compare
0