diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt index 59ca4f8ee..f1b902d1f 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/loadResult.kt @@ -135,7 +135,19 @@ data class Playlist( val info: PlaylistInfo, val pluginInfo: JsonObject, val tracks: List -) : LoadResult.Data +) : LoadResult.Data { + + /** + * Deserialize the plugin info into a specific type. + * This method is a convenience method meant to be used in Java, + * since Kotlin extension methods are painful to use in Java. + * + * @param deserializer The deserializer to use. (e.g. `T.Companion.serializer()`) + * + * @return the deserialized plugin info as type T + */ + fun deserializePluginInfo(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) +} @Serializable data class Exception( diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt index 5cc31ac00..f76d14c84 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/player.kt @@ -1,10 +1,18 @@ package dev.arbjerg.lavalink.protocol.v4 +import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.serializer import kotlin.jvm.JvmInline -@Serializable() +inline fun JsonObject.deserialize(): T = + deserialize(json.serializersModule.serializer()) + +fun JsonObject.deserialize(deserializer: DeserializationStrategy): T = + json.decodeFromJsonElement(deserializer, this) + +@Serializable @JvmInline value class Players(val players: List) @@ -24,7 +32,19 @@ data class Track( val encoded: String, val info: TrackInfo, val pluginInfo: JsonObject -) : LoadResult.Data +) : LoadResult.Data { + + /** + * Deserialize the plugin info into a specific type. + * This method is a convenience method meant to be used in Java, + * since Kotlin extension methods are painful to use in Java. + * + * @param deserializer The deserializer to use. (e.g. `T.Companion.serializer()`) + * + * @return the deserialized plugin info as type T + */ + fun deserializePluginInfo(deserializer: DeserializationStrategy): T = pluginInfo.deserialize(deserializer) +} @Serializable @JvmInline