Skip to content

Commit 1a0491a

Browse files
committed
missing apis for getPayload and getHeader with serializer instance
1 parent 7356a11 commit 1a0491a

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

lib/src/commonMain/kotlin/co/touchlab/kjwt/model/JwtInstance.kt

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package co.touchlab.kjwt.model
22

33
import co.touchlab.kjwt.internal.JwtJson
4+
import kotlinx.serialization.KSerializer
45
import kotlinx.serialization.json.Json
56

67
public sealed class JwtInstance {
@@ -14,26 +15,48 @@ public sealed class JwtInstance {
1415
/**
1516
* Deserializes the token payload as type [T].
1617
*
18+
* @param T the target type to deserialize the payload into
1719
* @param jsonInstance the [Json] instance to use for deserialization; defaults to the library's
1820
* internal [JwtJson] configuration (`ignoreUnknownKeys = true`, `explicitNulls = false`)
1921
* @return the payload deserialized into an instance of [T]
2022
*/
21-
public inline fun <reified T> getPayload(jsonInstance: Json = JwtJson): T = jsonInstance.decodeFromJsonElement(
22-
kotlinx.serialization.serializer<T>(),
23-
payload.jsonData
24-
)
23+
public inline fun <reified T> getPayload(jsonInstance: Json = JwtJson): T =
24+
getPayload(kotlinx.serialization.serializer<T>(), jsonInstance)
25+
26+
/**
27+
* Deserializes the token payload using the given [serializer].
28+
*
29+
* @param T the target type to deserialize the payload into
30+
* @param serializer the [KSerializer] used to deserialize the payload
31+
* @param jsonInstance the [Json] instance to use for deserialization; defaults to the library's
32+
* internal [JwtJson] configuration (`ignoreUnknownKeys = true`, `explicitNulls = false`)
33+
* @return the payload deserialized into an instance of [T]
34+
*/
35+
public fun <T> getPayload(serializer: KSerializer<T>, jsonInstance: Json = JwtJson): T =
36+
jsonInstance.decodeFromJsonElement(serializer, payload.jsonData)
2537

2638
/**
2739
* Deserializes the token header as type [T].
2840
*
41+
* @param T the target type to deserialize the header into
42+
* @param jsonInstance the [Json] instance to use for deserialization; defaults to the library's
43+
* internal [JwtJson] configuration (`ignoreUnknownKeys = true`, `explicitNulls = false`)
44+
* @return the header deserialized into an instance of [T]
45+
*/
46+
public inline fun <reified T> getHeader(jsonInstance: Json = JwtJson): T =
47+
getHeader(kotlinx.serialization.serializer<T>(), jsonInstance)
48+
49+
/**
50+
* Deserializes the token header using the given [serializer].
51+
*
52+
* @param T the target type to deserialize the header into
53+
* @param serializer the [KSerializer] used to deserialize the header
2954
* @param jsonInstance the [Json] instance to use for deserialization; defaults to the library's
3055
* internal [JwtJson] configuration (`ignoreUnknownKeys = true`, `explicitNulls = false`)
3156
* @return the header deserialized into an instance of [T]
3257
*/
33-
public inline fun <reified T> getHeader(jsonInstance: Json = JwtJson): T = jsonInstance.decodeFromJsonElement(
34-
kotlinx.serialization.serializer<T>(),
35-
header.jsonData
36-
)
58+
public fun <T> getHeader(serializer: KSerializer<T>, jsonInstance: Json = JwtJson): T =
59+
jsonInstance.decodeFromJsonElement(serializer, header.jsonData)
3760

3861
/** Represents a JWE (encrypted) token with five compact-serialization parts. */
3962
public class Jwe internal constructor(

0 commit comments

Comments
 (0)