v79
01/21/2023, 8:08 AMobjectMapper.readValue<Map<String, Any>>(it)
And how I might write it using Kotlinx.serialization instead? In the context, it's extracting a JWT claim from a Json Web Token. I don't know what the type of the 'Any' is, but I suspect it's just a String, or possibly a List of Strings.Adam S
01/21/2023, 8:41 AMit
a string? My guess is that the KxS equivalent is decoding to JsonObject
https://github.com/Kotlin/kotlinx.serialization/blob/v1.4.1/docs/json.md#json-elements
you can try
val jwtObj: JsonObject = Json.decodeFromString(it)
And then you can treat JsonObject
like a key-value Mapv79
01/21/2023, 8:45 AMit
is a String. I'll try that, thanks.response.body?.let { // type of it is T & Any, whatever that means!
objectMapper.writeValueAsString(it)
}
I guess that Jackson is using reflection so can find out what the concrete class of T
is, but kotlinx is not and I need to pass a SerializationStrategy - and since it's T
, I don't know what type that is.Adam S
01/21/2023, 8:50 AMT & Any
means definitely not nullv79
01/21/2023, 8:52 AMopen fun <T> createResponse(contentType: MediaType, response: ResponseEntity<T>): APIGatewayProxyResponseEvent =
when (response.body != null && serializationHandlerChain.supports(contentType, response.body)) {
true -> contentType
false -> MediaType.parse(router.defaultContentType)
}.let { finalContentType ->
APIGatewayProxyResponseEvent()
.withStatusCode(response.statusCode)
.withHeaders(response.headers.toMutableMap().apply { put("Content-Type", finalContentType.toString()) })
.withBody(
response.body?.let {
// Json.encodeToString(it)
serializationHandlerChain.serialize(finalContentType, it as Any)
}
)
}
The call to serializationHandlerChain.serialize(finalContentType, it as Any)
ultimately just ends with objectMapper.writeValueAsString(body)
I can't just write Json.encodeToString(it as Any)
Adam S
01/21/2023, 8:53 AMv79
01/21/2023, 8:54 AMAdam S
01/21/2023, 8:56 AMinline fun <reified T> createResponse()
but I don’t think that will work because it’s open
, and it’s probably too large/complicated to inlinedata class ResponseEntity
data class ResponseEntity<T>(
val statusCode: Int,
val body: T? = null,
val headers: Map<String, String> = emptyMap(),
val serializer: KSerializer<T>,
)
inline
and reified T
then I think you could get the KSerializer<T>
from the `SerializerModule`… maybev79
01/21/2023, 9:04 AMLocalDate
values to deal with 😞.v79
01/21/2023, 9:14 AMAdam S
01/21/2023, 9:14 AM