22 lines
587 B
Kotlin
22 lines
587 B
Kotlin
|
|
package com.xscm.moduleutil.utils
|
||
|
|
|
||
|
|
import com.google.gson.TypeAdapter
|
||
|
|
import com.google.gson.stream.JsonReader
|
||
|
|
import com.google.gson.stream.JsonToken
|
||
|
|
import com.google.gson.stream.JsonWriter
|
||
|
|
|
||
|
|
class NullToEmptyStringAdapter : TypeAdapter<String>() {
|
||
|
|
override fun write(out: JsonWriter, value: String?) {
|
||
|
|
out.value(value ?: "")
|
||
|
|
}
|
||
|
|
|
||
|
|
override fun read(`in`: JsonReader): String {
|
||
|
|
return if (`in`.peek() == JsonToken.NULL) {
|
||
|
|
`in`.nextNull()
|
||
|
|
"" // null → 空字符串
|
||
|
|
} else {
|
||
|
|
`in`.nextString() ?: ""
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|