Избавляемся от nullSafe врапера

This commit is contained in:
DBotThePony 2023-02-04 14:53:28 +07:00
parent 7fc4d225a2
commit c54e9b63d2
Signed by: DBot
GPG Key ID: DCC23B5715498507
7 changed files with 56 additions and 16 deletions

View File

@ -9,7 +9,7 @@ import java.lang.reflect.ParameterizedType
object ArrayListAdapterFactory : TypeAdapterFactory {
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (ArrayList::class.java.isAssignableFrom(type.rawType) && type.type is ParameterizedType) {
return ArrayListTypeAdapter(gson.getAdapter(TypeToken.get((type.type as ParameterizedType).actualTypeArguments[0]))).nullSafe() as TypeAdapter<T>
return ArrayListTypeAdapter(gson.getAdapter(TypeToken.get((type.type as ParameterizedType).actualTypeArguments[0]))) as TypeAdapter<T>
}
return null

View File

@ -6,8 +6,13 @@ import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.util.ArrayList
class ArrayListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<java.util.ArrayList<E>>() {
override fun write(out: JsonWriter, value: ArrayList<E>) {
class ArrayListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<ArrayList<E>>() {
override fun write(out: JsonWriter, value: ArrayList<E>?) {
if (value == null) {
out.nullValue()
return
}
if (value.size == 1) {
elementAdapter.write(out, value[0])
return
@ -22,7 +27,10 @@ class ArrayListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<
out.endArray()
}
override fun read(reader: JsonReader): ArrayList<E> {
override fun read(reader: JsonReader): ArrayList<E>? {
if (reader.peek() == JsonToken.NULL)
return null
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
// не массив, возможно упрощение структуры "a": [value] -> "a": value
val list = ArrayList<E>(1)

View File

@ -9,7 +9,12 @@ import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
class ImmutableArrayMapTypeAdapter<K, V>(val keyAdapter: TypeAdapter<K>, val elementAdapter: TypeAdapter<V>) : TypeAdapter<ImmutableMap<K, V>>() {
override fun write(out: JsonWriter, value: ImmutableMap<K, V>) {
override fun write(out: JsonWriter, value: ImmutableMap<K, V>?) {
if (value == null) {
out.nullValue()
return
}
out.beginArray()
for ((k, v) in value) {
@ -20,7 +25,10 @@ class ImmutableArrayMapTypeAdapter<K, V>(val keyAdapter: TypeAdapter<K>, val ele
out.endArray()
}
override fun read(reader: JsonReader): ImmutableMap<K, V> {
override fun read(reader: JsonReader): ImmutableMap<K, V>? {
if (reader.peek() == JsonToken.NULL)
return null
reader.beginArray()
val builder = ImmutableMap.Builder<K, V>()

View File

@ -15,25 +15,25 @@ object ImmutableCollectionAdapterFactory : TypeAdapterFactory {
when (type.rawType) {
ImmutableList::class.java -> {
val elementType = `$Gson$Types`.getCollectionElementType(type.type, type.rawType)
return ImmutableListTypeAdapter(gson.getAdapter(TypeToken.get(elementType))).nullSafe() as TypeAdapter<T>
return ImmutableListTypeAdapter(gson.getAdapter(TypeToken.get(elementType))) as TypeAdapter<T>
}
ImmutableSet::class.java -> {
val elementType = `$Gson$Types`.getCollectionElementType(type.type, type.rawType)
return ImmutableSetTypeAdapter(gson.getAdapter(TypeToken.get(elementType))).nullSafe() as TypeAdapter<T>
return ImmutableSetTypeAdapter(gson.getAdapter(TypeToken.get(elementType))) as TypeAdapter<T>
}
ImmutableMap::class.java -> {
val (elementType0, elementType1) = `$Gson$Types`.getMapKeyAndValueTypes(type.type, type.rawType)
if (`$Gson$Types`.getRawType(elementType0) == String::class.java) {
return ImmutableMapTypeAdapter(gson.getAdapter(TypeToken.get(elementType1))).nullSafe() as TypeAdapter<T>
return ImmutableMapTypeAdapter(gson.getAdapter(TypeToken.get(elementType1)))as TypeAdapter<T>
}
return ImmutableArrayMapTypeAdapter(
gson.getAdapter(TypeToken.get(elementType0)),
gson.getAdapter(TypeToken.get(elementType1))
).nullSafe() as TypeAdapter<T>
) as TypeAdapter<T>
}
else -> return null

View File

@ -8,7 +8,12 @@ import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
class ImmutableListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<ImmutableList<E>>() {
override fun write(out: JsonWriter, value: ImmutableList<E>) {
override fun write(out: JsonWriter, value: ImmutableList<E>?) {
if (value == null) {
out.nullValue()
return
}
if (value.size == 1) {
elementAdapter.write(out, value[0])
return
@ -23,7 +28,10 @@ class ImmutableListTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdap
out.endArray()
}
override fun read(reader: JsonReader): ImmutableList<E> {
override fun read(reader: JsonReader): ImmutableList<E>? {
if (reader.peek() == JsonToken.NULL)
return null
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
// не массив, возможно упрощение структуры "a": [value] -> "a": value
return ImmutableList.of(elementAdapter.read(reader) ?: throw JsonSyntaxException("List does not accept nulls"))

View File

@ -9,7 +9,12 @@ import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
class ImmutableMapTypeAdapter<V>(val elementAdapter: TypeAdapter<V>) : TypeAdapter<ImmutableMap<String, V>>() {
override fun write(out: JsonWriter, value: ImmutableMap<String, V>) {
override fun write(out: JsonWriter, value: ImmutableMap<String, V>?) {
if (value == null) {
out.nullValue()
return
}
out.beginObject()
for ((k, v) in value) {
@ -20,7 +25,10 @@ class ImmutableMapTypeAdapter<V>(val elementAdapter: TypeAdapter<V>) : TypeAdapt
out.endObject()
}
override fun read(reader: JsonReader): ImmutableMap<String, V> {
override fun read(reader: JsonReader): ImmutableMap<String, V>? {
if (reader.peek() == JsonToken.NULL)
return null
reader.beginObject()
val builder = ImmutableMap.Builder<String, V>()

View File

@ -9,7 +9,12 @@ import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
class ImmutableSetTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapter<ImmutableSet<E>>() {
override fun write(out: JsonWriter, value: ImmutableSet<E>) {
override fun write(out: JsonWriter, value: ImmutableSet<E>?) {
if (value == null) {
out.nullValue()
return
}
if (value.size == 1) {
elementAdapter.write(out, value.first())
return
@ -24,7 +29,10 @@ class ImmutableSetTypeAdapter<E>(val elementAdapter: TypeAdapter<E>) : TypeAdapt
out.endArray()
}
override fun read(reader: JsonReader): ImmutableSet<E> {
override fun read(reader: JsonReader): ImmutableSet<E>? {
if (reader.peek() == JsonToken.NULL)
return null
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
// не массив, возможно упрощение структуры "a": [value] -> "a": value
return ImmutableSet.of(elementAdapter.read(reader) ?: throw JsonSyntaxException("List does not accept nulls"))