Избавляемся от устаревших тип адаптеров
This commit is contained in:
parent
b212ef7518
commit
e154cec780
@ -1,15 +1,10 @@
|
||||
package ru.dbotthepony.kstarbound.defs.item
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import ru.dbotthepony.kstarbound.Starbound
|
||||
import ru.dbotthepony.kstarbound.defs.DirectAssetReference
|
||||
import ru.dbotthepony.kstarbound.defs.util.enrollMap
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.BuilderAdapter
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonBuilder
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonIgnoreProperty
|
||||
import ru.dbotthepony.kstarbound.io.json.util.asJsonObject
|
||||
import ru.dbotthepony.kstarbound.io.json.util.asList
|
||||
import ru.dbotthepony.kstarbound.io.json.neverNull
|
||||
import ru.dbotthepony.kstarbound.util.NotNullVar
|
||||
|
||||
@JsonBuilder
|
||||
|
@ -1,18 +0,0 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.util
|
||||
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import ru.dbotthepony.kstarbound.Starbound
|
||||
|
||||
class LazyTypeProvider<T : Any?>(private val bound: Class<T>, private val resolver: (Class<T>) -> TypeAdapter<T> = Starbound::getTypeAdapter) : TypeAdapter<T>() {
|
||||
private val resolved by lazy { resolver(bound) }
|
||||
|
||||
override fun write(out: JsonWriter, value: T) {
|
||||
resolved.write(out, value)
|
||||
}
|
||||
|
||||
override fun read(`in`: JsonReader): T {
|
||||
return resolved.read(`in`)
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.util
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.gson.JsonSyntaxException
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
|
||||
fun <T : Any> TypeAdapter<T>.asList(valueTransformer: (T) -> T = { it }): TypeAdapter<List<T>> {
|
||||
return ListAdapter(this, valueTransformer = valueTransformer)
|
||||
}
|
||||
|
||||
class ListAdapter<T : Any>(val elementAdapter: TypeAdapter<T>, val valueTransformer: (T) -> T = { it }) : TypeAdapter<List<T>>() {
|
||||
constructor(type: Class<T>, valueTransformer: (T) -> T = { it }) : this(LazyTypeProvider(type), valueTransformer = valueTransformer)
|
||||
|
||||
override fun write(out: JsonWriter, value: List<T>) {
|
||||
if (value.size == 1) {
|
||||
elementAdapter.write(out, value[0])
|
||||
return
|
||||
}
|
||||
|
||||
out.beginArray()
|
||||
|
||||
for (v in value) {
|
||||
elementAdapter.write(out, v)
|
||||
}
|
||||
|
||||
out.endArray()
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): List<T> {
|
||||
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
|
||||
// не массив, возможно упрощение структуры "a": [value] -> "a": value
|
||||
return ImmutableList.of(elementAdapter.read(reader) ?: throw JsonSyntaxException("List does not accept nulls"))
|
||||
}
|
||||
|
||||
reader.beginArray()
|
||||
|
||||
val builder = ImmutableList.builder<T>()
|
||||
|
||||
while (reader.peek() != JsonToken.END_ARRAY) {
|
||||
val readObject = elementAdapter.read(reader) ?: throw JsonSyntaxException("List does not accept nulls")
|
||||
builder.add(valueTransformer(readObject))
|
||||
}
|
||||
|
||||
reader.endArray()
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.util
|
||||
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
|
||||
class MapAdapter<K, V>(val keyAdapter: TypeAdapter<K>, val valueAdapter: TypeAdapter<V>) : TypeAdapter<Map<K, V>>() {
|
||||
constructor(keyType: Class<K>, valueType: Class<V>) : this(LazyTypeProvider(keyType), LazyTypeProvider(valueType))
|
||||
|
||||
override fun write(out: JsonWriter, value: Map<K, V>) {
|
||||
out.beginArray()
|
||||
|
||||
for ((k, v) in value) {
|
||||
out.beginArray()
|
||||
keyAdapter.write(out, k)
|
||||
valueAdapter.write(out, v)
|
||||
out.endArray()
|
||||
}
|
||||
|
||||
out.endArray()
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): Map<K, V> {
|
||||
reader.beginArray()
|
||||
|
||||
val builder = ImmutableMap.builder<K, V>()
|
||||
|
||||
while (reader.peek() != JsonToken.END_ARRAY) {
|
||||
reader.beginArray()
|
||||
builder.put(keyAdapter.read(reader), valueAdapter.read(reader))
|
||||
reader.endArray()
|
||||
}
|
||||
|
||||
reader.endArray()
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package ru.dbotthepony.kstarbound.io.json.util
|
||||
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonToken
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import ru.dbotthepony.kstarbound.Starbound
|
||||
|
||||
fun <T> TypeAdapter<T>.asJsonObject(valueTransformer: (T) -> T = { it }): TypeAdapter<Map<String, T>> {
|
||||
return String2ObjectAdapter(this, valueTransformer = valueTransformer)
|
||||
}
|
||||
|
||||
class String2ObjectAdapter<T>(val adapter: TypeAdapter<T>, val valueTransformer: (T) -> T = { it }) : TypeAdapter<Map<String, T>>() {
|
||||
constructor(type: Class<T>, valueTransformer: (T) -> T = { it }) : this(LazyTypeProvider(type), valueTransformer = valueTransformer)
|
||||
|
||||
override fun write(out: JsonWriter, value: Map<String, T>) {
|
||||
out.beginObject()
|
||||
|
||||
for ((k, v) in value) {
|
||||
out.name(k)
|
||||
adapter.write(out, v)
|
||||
}
|
||||
|
||||
out.endObject()
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): Map<String, T> {
|
||||
val builder = ImmutableMap.builder<String, T>()
|
||||
|
||||
reader.beginObject()
|
||||
|
||||
while (reader.peek() != JsonToken.END_OBJECT) {
|
||||
builder.put(Starbound.STRING_INTERNER.intern(reader.nextName()), valueTransformer(adapter.read(reader)))
|
||||
}
|
||||
|
||||
reader.endObject()
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user