dump moshi, use jackson

This commit is contained in:
Bixilon 2021-12-06 00:39:12 +01:00
parent c96720a076
commit ec965fbd0b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
22 changed files with 84 additions and 391 deletions

View File

@ -22,7 +22,7 @@ import de.bixilon.minosoft.util.KUtil.lockMapOf
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.collections.LockMap
import de.bixilon.minosoft.util.json.jackson.Jackson
import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.task.pool.DefaultThreadPool
import de.bixilon.minosoft.util.task.time.TimeWorker
import de.bixilon.minosoft.util.task.time.TimeWorkerTask

View File

@ -15,7 +15,7 @@ import de.bixilon.minosoft.util.KUtil.toInt
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.filewatcher.FileWatcher
import de.bixilon.minosoft.util.filewatcher.FileWatcherService
import de.bixilon.minosoft.util.json.jackson.Jackson
import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType

View File

@ -16,7 +16,7 @@ import com.squareup.moshi.JsonEncodingException
import de.bixilon.minosoft.data.language.Translator
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil.text
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.scene.Node
@ -91,7 +91,7 @@ interface ChatComponent {
}
if (!ignoreJson && string.startsWith('{')) {
try {
return BaseComponent(translator, parent, JSONSerializer.MAP_ADAPTER.fromJson(string)!!, restrictedMode)
return BaseComponent(translator, parent, Jackson.MAPPER.readValue(string, Jackson.JSON_MAP_TYPE), restrictedMode)
} catch (ignored: JsonEncodingException) {
}
}

View File

@ -18,7 +18,7 @@ import de.bixilon.minosoft.util.KUtil.asUUID
import de.bixilon.minosoft.util.KUtil.nullCast
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.asCompound
import java.util.*
@ -31,13 +31,13 @@ class EntityHoverData(
companion object {
fun deserialize(data: Any): EntityHoverData {
var json: Map<String, Any> = if (data is String) {
JSONSerializer.MAP_ADAPTER.fromJson(data)
Jackson.MAPPER.readValue(data, Jackson.JSON_MAP_TYPE)
} else {
data
}.asCompound()
json["text"]?.let {
// 1.14.3.... lol
json = JSONSerializer.MAP_ADAPTER.fromJson(it.unsafeCast<String>())!!
json = Jackson.MAPPER.readValue(it.unsafeCast<String>(), Jackson.JSON_MAP_TYPE)
}
var type: ResourceLocation? = null
json["type"]?.nullCast<String>()?.let {

View File

@ -24,7 +24,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.SpriteTextu
import de.bixilon.minosoft.gui.rendering.textures.TextureAnimation
import de.bixilon.minosoft.gui.rendering.textures.properties.ImageProperties
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
@ -54,7 +54,7 @@ class OpenGLTextureArray(
// load .mcmeta
val properties = try {
JSONSerializer.IMAGE_PROPERTIES_ADAPTER.fromJson(renderWindow.connection.assetsManager.readStringAsset(ResourceLocation("$resourceLocation.mcmeta")))!!
Jackson.MAPPER.readValue(renderWindow.connection.assetsManager.readStringAsset(ResourceLocation("$resourceLocation.mcmeta")), ImageProperties::class.java)
} catch (exception: FileNotFoundException) {
ImageProperties()
}

View File

@ -13,7 +13,8 @@
package de.bixilon.minosoft.gui.rendering.textures.properties
import com.squareup.moshi.Json
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.util.KUtil.toInt
@ -21,15 +22,17 @@ data class AnimationProperties(
val interpolate: Boolean = false,
var width: Int = -1,
var height: Int = -1,
@Json(name = "frametime") private val frameTime: Int = 1,
@Json(name = "frames") private val _frames: List<Any> = listOf(),
@JsonProperty("frametime") private val frameTime: Int = 1,
@JsonProperty("frames") private val _frames: List<Any> = listOf(),
) {
@JsonIgnore
private var initialized = false
@Transient
@JsonIgnore
lateinit var frames: Array<AnimationFrame>
private set
@JsonIgnore
var frameCount = -1
private set

View File

@ -23,7 +23,7 @@ import de.bixilon.minosoft.data.tags.Tag
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.Connection
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.nbt.tag.NBTTagTypes
import glm_.vec2.Vec2i
import glm_.vec3.Vec3
@ -271,7 +271,7 @@ open class InByteBuffer {
fun readJson(): Map<String, Any> {
return JSONSerializer.MUTABLE_MAP_ADAPTER.fromJson(readString())!!
return Jackson.MAPPER.readValue(readString(), Jackson.JSON_MAP_TYPE)
}
fun readJsonArray(length: Int = readVarInt()): Array<Map<String, Any>> {

View File

@ -13,8 +13,6 @@
package de.bixilon.minosoft.util
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonWriter
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.inventory.ItemStack
import de.bixilon.minosoft.data.registries.ResourceLocation
@ -28,11 +26,10 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.collections.LockMap
import de.bixilon.minosoft.util.collections.SynchronizedMap
import de.bixilon.minosoft.util.enum.AliasableEnum
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import glm_.vec2.Vec2t
import glm_.vec3.Vec3t
import glm_.vec4.Vec4t
import okio.Buffer
import sun.misc.Unsafe
import java.io.*
import java.lang.reflect.Field
@ -362,20 +359,16 @@ object KUtil {
return this.unsafeCast()
}
fun Any.toJson(beautiful: Boolean = false, adapter: JsonAdapter<Any> = JSONSerializer.ANY_ADAPTER): String {
val buffer = Buffer()
val jsonWriter: JsonWriter = JsonWriter.of(buffer)
if (beautiful) {
jsonWriter.indent = " "
fun Any.toJson(beautiful: Boolean = false): String {
return if (beautiful) {
Jackson.MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(this)
} else {
Jackson.MAPPER.writeValueAsString(this)
}
synchronized(this) {
adapter.toJson(jsonWriter, this)
}
return buffer.readUtf8()
}
fun String.fromJson(): Any {
return JSONSerializer.ANY_ADAPTER.fromJson(this)!!
return Jackson.MAPPER.readValue(this, Jackson.JSON_MAP_TYPE)
}
fun Any?.toInt(): Int {

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.util.http
import de.bixilon.minosoft.util.KUtil.decide
import de.bixilon.minosoft.util.KUtil.extend
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.json.JSONSerializer
import de.bixilon.minosoft.util.json.Jackson
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
@ -52,8 +52,8 @@ object HTTP2 {
return post(
url = url,
data = this,
bodyPublisher = { JSONSerializer.MAP_ADAPTER.toJson(it) },
bodyBuilder = { it.isBlank().decide(null) { JSONSerializer.MAP_ADAPTER.fromJson(it) } },
bodyPublisher = { Jackson.MAPPER.writeValueAsString(it) },
bodyBuilder = { it.isBlank().decide(null) { Jackson.MAPPER.readValue(it, Jackson.JSON_MAP_TYPE) } },
headers = headers.extend(
"Content-Type" to "application/json",
"Accept" to "application/json",
@ -66,7 +66,7 @@ object HTTP2 {
url = url,
data = this,
bodyPublisher = { Util.mapToUrlQuery(this) },
bodyBuilder = { it.isBlank().decide(null) { JSONSerializer.MAP_ADAPTER.fromJson(it) } },
bodyBuilder = { it.isBlank().decide(null) { Jackson.MAPPER.readValue(it, Jackson.JSON_MAP_TYPE) } },
headers = headers.extend(
"Content-Type" to "application/x-www-form-urlencoded",
)
@ -87,7 +87,7 @@ object HTTP2 {
fun String.getJson(headers: Map<String, Any> = mapOf()): HTTPResponse<Map<String, Any>?> {
return this.get(
bodyBuilder = { it.isBlank().decide(null) { JSONSerializer.MAP_ADAPTER.fromJson(it) } },
bodyBuilder = { it.isBlank().decide(null) { Jackson.MAPPER.readValue(it, Jackson.JSON_MAP_TYPE) } },
headers = headers.extend(
"Content-Type" to "application/json",
"Accept" to "application/json",

View File

@ -1,4 +1,4 @@
package de.bixilon.minosoft.util.json.jackson
package de.bixilon.minosoft.util.json
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser

View File

@ -1,36 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import de.bixilon.minosoft.data.text.ChatComponent
object ChatComponentSerializer : JsonAdapter<ChatComponent>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): ChatComponent? {
if (jsonReader.peek() == JsonReader.Token.NULL) {
return null
}
return ChatComponent.of(jsonReader.nextString())
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, chatComponent: ChatComponent?) {
if (chatComponent == null) {
jsonWriter.nullValue()
return
}
jsonWriter.value(chatComponent.legacyText)
}
}

View File

@ -1,40 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import de.bixilon.minosoft.gui.rendering.textures.properties.ImageProperties
@Deprecated("Moshi is deprecated, use klaxon or mbf instead")
object JSONSerializer {
val MOSHI = Moshi.Builder()
.add(RGBColorSerializer)
.add(Vec2Serializer)
.add(ChatComponentSerializer)
.add(ServerAddressSerializer)
.add(ResourceLocationSerializer)
.add(UUIDSerializer)
.add(VersionSerializer)
.add(KotlinJsonAdapterFactory())
.build()!!
val ANY_ADAPTER = MOSHI.adapter(Any::class.java)!!
val MUTABLE_MAP_ADAPTER: JsonAdapter<MutableMap<String, Any>> = MOSHI.adapter(Types.newParameterizedType(MutableMap::class.java, String::class.java, Any::class.java))
val MAP_ADAPTER: JsonAdapter<Map<String, Any>> = MOSHI.adapter(Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java))
val IMAGE_PROPERTIES_ADAPTER = MOSHI.adapter(ImageProperties::class.java)!!
}

View File

@ -1,4 +1,4 @@
package de.bixilon.minosoft.util.json.jackson
package de.bixilon.minosoft.util.json
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper

View File

@ -1,4 +1,4 @@
package de.bixilon.minosoft.util.json.jackson
package de.bixilon.minosoft.util.json
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser

View File

@ -1,48 +1,38 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.bixilon.minosoft.data.text.ChatCode.Companion.toColor
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
object RGBColorSerializer : JsonAdapter<RGBColor>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): RGBColor? {
return when (jsonReader.peek()) {
JsonReader.Token.NUMBER -> {
val rgb = jsonReader.nextInt()
if (rgb == 0) {
return null
}
return rgb.asRGBColor()
object RGBColorSerializer : SimpleModule() {
init {
addDeserializer(RGBColor::class.java, Deserializer)
addSerializer(RGBColor::class.java, Serializer)
}
object Deserializer : StdDeserializer<RGBColor>(RGBColor::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?): RGBColor {
return when (parser.currentToken) {
JsonToken.VALUE_NUMBER_INT -> RGBColor(parser.valueAsInt)
JsonToken.VALUE_STRING -> parser.valueAsString.toColor()!!
else -> TODO("Can not parse color!")
}
JsonReader.Token.STRING -> jsonReader.nextString().asColor()
else -> null
}
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, color: RGBColor?) {
if (color == null) {
jsonWriter.nullValue()
return
object Serializer : StdSerializer<RGBColor>(RGBColor::class.java) {
override fun serialize(value: RGBColor?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeString(value?.toString())
}
if (color.rgba == 0) {
jsonWriter.nullValue()
}
jsonWriter.value(color.toString())
}
}

View File

@ -1,36 +1,33 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.util.KUtil.toResourceLocation
object ResourceLocationSerializer : JsonAdapter<ResourceLocation>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): ResourceLocation? {
if (jsonReader.peek() == JsonReader.Token.NULL) {
return null
}
return ResourceLocation.getResourceLocation(jsonReader.nextString())
object ResourceLocationSerializer : SimpleModule() {
init {
addDeserializer(ResourceLocation::class.java, Deserializer)
addSerializer(ResourceLocation::class.java, Serializer)
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, resourceLocation: ResourceLocation?) {
if (resourceLocation == null) {
jsonWriter.nullValue()
return
object Deserializer : StdDeserializer<ResourceLocation>(ResourceLocation::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?): ResourceLocation {
return parser.valueAsString.toResourceLocation()
}
}
object Serializer : StdSerializer<ResourceLocation>(ResourceLocation::class.java) {
override fun serialize(value: ResourceLocation?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeString(value?.full)
}
jsonWriter.value(resourceLocation.full)
}
}

View File

@ -1,37 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import de.bixilon.minosoft.util.DNSUtil
import de.bixilon.minosoft.util.ServerAddress
object ServerAddressSerializer : JsonAdapter<ServerAddress>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): ServerAddress? {
if (jsonReader.peek() == JsonReader.Token.NULL) {
return null
}
return DNSUtil.getServerAddress(jsonReader.nextString())
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, serverAddress: ServerAddress?) {
if (serverAddress == null) {
jsonWriter.nullValue()
return
}
jsonWriter.value(serverAddress.toString())
}
}

View File

@ -1,37 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import de.bixilon.minosoft.util.Util
import java.util.*
object UUIDSerializer : JsonAdapter<UUID>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): UUID? {
if (jsonReader.peek() == JsonReader.Token.NULL) {
return null
}
return Util.getUUIDFromString(jsonReader.nextString())
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, uuid: UUID?) {
if (uuid == null) {
jsonWriter.nullValue()
return
}
jsonWriter.value(uuid.toString())
}
}

View File

@ -1,30 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import glm_.vec2.Vec2
object Vec2Serializer {
@FromJson
fun fromJson(json: List<Float>): Vec2 {
return Vec2(json[0], json[1])
}
@ToJson
fun toJson(vec2: Vec2): List<Float> {
return listOf(vec2.x, vec2.y)
}
}

View File

@ -1,39 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import de.bixilon.minosoft.data.registries.versions.Version
import de.bixilon.minosoft.data.registries.versions.Versions
object VersionSerializer : JsonAdapter<Version>() {
@FromJson
override fun fromJson(jsonReader: JsonReader): Version? {
return when (jsonReader.peek()) {
JsonReader.Token.NULL -> null
JsonReader.Token.NUMBER -> Versions.getVersionById(jsonReader.nextInt())
JsonReader.Token.STRING -> Versions.getVersionByName(jsonReader.nextString())
else -> TODO()
}
}
@ToJson
override fun toJson(jsonWriter: JsonWriter, version: Version?) {
if (version == null) {
jsonWriter.nullValue()
return
}
jsonWriter.value(version.versionId)
}
}

View File

@ -1,38 +0,0 @@
package de.bixilon.minosoft.util.json.jackson
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.bixilon.minosoft.data.text.ChatCode.Companion.toColor
import de.bixilon.minosoft.data.text.RGBColor
object RGBColorSerializer : SimpleModule() {
init {
addDeserializer(RGBColor::class.java, Deserializer)
addSerializer(RGBColor::class.java, Serializer)
}
object Deserializer : StdDeserializer<RGBColor>(RGBColor::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?): RGBColor {
return when (parser.currentToken) {
JsonToken.VALUE_NUMBER_INT -> RGBColor(parser.valueAsInt)
JsonToken.VALUE_STRING -> parser.valueAsString.toColor()!!
else -> TODO("Can not parse color!")
}
}
}
object Serializer : StdSerializer<RGBColor>(RGBColor::class.java) {
override fun serialize(value: RGBColor?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeString(value?.toString())
}
}
}

View File

@ -1,33 +0,0 @@
package de.bixilon.minosoft.util.json.jackson
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.util.KUtil.toResourceLocation
object ResourceLocationSerializer : SimpleModule() {
init {
addDeserializer(ResourceLocation::class.java, Deserializer)
addSerializer(ResourceLocation::class.java, Serializer)
}
object Deserializer : StdDeserializer<ResourceLocation>(ResourceLocation::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?): ResourceLocation {
return parser.valueAsString.toResourceLocation()
}
}
object Serializer : StdSerializer<ResourceLocation>(ResourceLocation::class.java) {
override fun serialize(value: ResourceLocation?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeString(value?.full)
}
}
}