Skip to content

Commit

Permalink
String转换List方法修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Reginer committed Dec 9, 2019
1 parent af5edaa commit 0fa4ac2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/MvvmExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object Version {
const val materialVersion = "1.2.0-alpha01"
const val objectboxVersion = "2.4.1"
const val loggerVersion = "2.2.0"
const val libVersionCode = 8
const val libVersionName = "1.8"
const val libVersionCode = 9
const val libVersionName = "1.9"
}

object MvvmExt {
Expand Down
13 changes: 9 additions & 4 deletions common/src/main/java/win/regin/common/CommonExt.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package win.regin.common

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.PrettyFormatStrategy
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type


/**
Expand Down Expand Up @@ -33,13 +34,17 @@ fun Any?.toJsonString(): String {
return Gson().toJson(this)
}

class ParameterizedTypeImpl(private val clazz: Class<*>) : ParameterizedType {
override fun getRawType(): Type = List::class.java
override fun getOwnerType(): Type? = null
override fun getActualTypeArguments(): Array<Type> = arrayOf(clazz)
}

/**
* String转换List
*/
fun <T> String?.toJsonArray(): List<T>? {
val type = object : TypeToken<List<T>>() {}.type
return Gson().fromJson(this, type)
inline fun <reified T> String?.toJsonArray(): List<T>? {
return Gson().fromJson<List<T>>(this, ParameterizedTypeImpl(T::class.java))
}

/**
Expand Down

0 comments on commit 0fa4ac2

Please sign in to comment.