添加依赖:
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.3'
implementation 'com.squareup.moshi:moshi:1.9.3'
使用kotlin的内联函数,很大程度上可以缩减代码,减少一些不必要的传参,比如可以不用传class,type等
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import java.lang.reflect.Type
object MoshiUtils {
val moshiBuild = Moshi.Builder().build()
fun fromJson(json: String, type: Type): T? {
val adapter = moshiBuild.adapter(type)
return adapter.fromJson(json)
}
inline fun fromJson(json: String): T? {
val adapter = moshiBuild.adapter(T::class.java)
return adapter.fromJson(json)
}
inline fun toJson(t: T) = moshiBuild.adapter(T::class.java).toJson(t) ?: ""
inline fun listFromJson(json: String): MutableList = fromJson>(
json, Types.newParameterizedType(
MutableList::class.java, T::class.java
)
) ?: mutableListOf()
inline fun mapFromJson(json: String): MutableMap = fromJson(
json,
Types.newParameterizedType(MutableMap::class.java, K::class.java, V::class.java)
) ?: mutableMapOf()
}
使用方法:
实体类:
@JsonClass(generateAdapter = true)
data class Shop(
val boss: Person? = Person(),
val employees: MutableList = mutableListOf()
)
@JsonClass(generateAdapter = true)
data class Person(
val name: String = "", // Carlson
val age: Int? = 0 // 56
)
json:
{"boss":{"name":"Carlson","age":56},"employees":[{"name":"Aaron","age":18},{"name":"Bland","age":19},{"name":"Carl","age":15}]}
实体类转json
val employees = mutableListOf(Person("Aaron",18), Person("Bland",19), Person("Carl",15))
val boss = Person("Carlson",56)
val shopBean = Shop(boss,employees)
val json = MoshiUtils.toJson(shopBean)
LogUtils.e("json-->$json")
// log -> {"boss":{"name":"Carlson","age":56},"employees":[{"name":"Aaron","age":18},{"name":"Bland","age":19},{"name":"Carl","age":15}]}
--------------------------------------------------------------------------------------------------------------------------------------------------
val employeeList = mutableListOf(Person("Aaron",18), Person("Bland",19), Person("Carl",15))
val listJson = MoshiUtils.toJson(employeeList)
LogUtils.e("listJson ->$listJson")
//log: [{"name":"Aaron","age":18},{"name":"Bland","age":19},{"name":"Carl","age":15}]
json转实体类
val person = MoshiUtils.fromJson("{\"name\":\"Carl\",\"age\":15}")
LogUtils.e(person?.toString())
//log : Person(name=Carl, age=15)
val shop =
MoshiUtils.fromJson("{\"boss\":{\"name\":\"Carlson\",\"age\":56},\"employees\":[{\"name\":\"Aaron\",\"age\":18},{\"name\":\"Bland\",\"age\":19},{\"name\":\"Carl\",\"age\":15}]}")
shop?.let {
LogUtils.e(it.boss)
LogUtils.e(it.employees)
}
------------------------------------------------------------------------------------------------------------
//传入的泛型为List ,可以直接解析List
val persons = MoshiUtils
.fromJson>("[{\"name\":\"Aaron\",\"age\":18},{\"name\":\"Bland\",\"age\":19},{\"name\":\"Carl\",\"age\":15}]")
LogUtils.e(persons?:"")
//log: [{name=Aaron, age=18.0}, {name=Bland, age=19.0}, {name=Carl, age=15.0}]
-------------------------------------------------------------------------------------------------------------
//但很多时候,我们希望获取可以避免空指针,可以使用listFromJson方法
val persons = MoshiUtils
.listFromJson("[{\"name\":\"Aaron\",\"age\":18},{\"name\":\"Bland\",\"age\":19},{\"name\":\"Carl\",\"age\":15}]")
LogUtils.e(persons)
//map也是类似