Skip to content

Commit

Permalink
Merge branch 'feature/plugin-info-deserialization' into patch/explici…
Browse files Browse the repository at this point in the history
…tNulls-false
  • Loading branch information
topi314 committed Oct 13, 2023
2 parents 4589d0f + 83128ee commit 3b9dc0a
Show file tree
Hide file tree
Showing 3 changed files with 36 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
1 change: 1 addition & 0 deletions protocol/src/commonTest/kotlin/LoadResultSerializerTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dev.arbjerg.lavalink.protocol.v4.Exception
import dev.arbjerg.lavalink.protocol.v4.LoadResult
import dev.arbjerg.lavalink.protocol.v4.ResultStatus
import kotlinx.serialization.json.JsonObject
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertIs
Expand Down

0 comments on commit 3b9dc0a

Please sign in to comment.