Skip to content

Commit

Permalink
allow for deserialization of plugin info into data classes (#973)
Browse files Browse the repository at this point in the history
* allow for deserialization of plugin info into data classes

* fix unit test

* fixup java access for plugin info deserialization

Co-authored-by: viztea <[email protected]>

* rename deserialize to deserializePluginInfo & docs

* remove unused import

---------

Co-authored-by: viztea <[email protected]>
  • Loading branch information
topi314 and viztea authored Dec 2, 2023
1 parent 7978e9d commit a34e0b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,19 @@ data class Playlist(
val info: PlaylistInfo,
val pluginInfo: JsonObject,
val tracks: List<Track>
) : 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 <T> deserializePluginInfo(deserializer: DeserializationStrategy<T>): T = pluginInfo.deserialize(deserializer)
}

@Serializable
data class Exception(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <reified T> JsonObject.deserialize(): T =
deserialize(json.serializersModule.serializer<T>())

fun <T> JsonObject.deserialize(deserializer: DeserializationStrategy<T>): T =
json.decodeFromJsonElement(deserializer, this)

@Serializable
@JvmInline
value class Players(val players: List<Player>)

Expand All @@ -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 <T> deserializePluginInfo(deserializer: DeserializationStrategy<T>): T = pluginInfo.deserialize(deserializer)
}

@Serializable
@JvmInline
Expand Down

0 comments on commit a34e0b9

Please sign in to comment.