snowe
01/26/2024, 12:13 AM@Serializable
sealed class LambdaProxyRequest<T>(
val version: String? = null,
val routeKey: String? = null,
val rawPath: String? = null,
val rawQueryString: String? = null,
val cookies: List<String>? = null,
val headers: Headers? = null,
val queryStringParameters: QueryStringParameters? = null,
val requestContext: RequestContext? = null,
val body: T? = null,
val pathParameters: PathParameters? = null,
val isBase64Encoded: Boolean? = null,
val stageVariables: StageVariables? = null,
)
object Request1: LambdaProxyRequest<CalculateRequest>()
object Request2: LambdaProxyRequest<DoOtherStuffRequest>()
where the body
actually comes across the wire as an escaped JSON string, but I want to deserialize it as the generic object, rather than manually pull out the escaped string, then unescape it, then decode it again. I don’t exactly see how the Json primitive stuff y’all talked about above would solve this, but I do see how it could be done with a custom deserializer that simply searches each index until it finds the body
and then unescapes it, then finally deserializes the whole object as if nothing was changed. But I’m having trouble implementing that. Am I off base here? Is it not possible to do this?snowe
01/26/2024, 12:14 AMobject SomeProxyDeserializer: KSerializer<Some> {
fun unescapeJson(escapedJson: String): String {
return escapedJson.replace("""\"""", "\"")
}
override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor("body", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Some {
val unescapedString = unescapeJson(decoder.decodeString())
return Json.decodeFromString(Some.serializer(), unescapedString)
}
override fun serialize(encoder: Encoder, value: Some) {
TODO("No need to ever serialize this, probably. XD")
}
}
but it doesn’t use the original Json object that was used to start the deserialization process, thus immediately failing as some of the serialization modules aren’t available.
and this doesn’t solve the generics problem at all, i have to manually set the serializer to a single instance, but it does get me slightly closer to a solution.Adam S
01/26/2024, 12:17 AMbody
? The only example on the website has a string "body": "Hello from Lambda",
Adam S
01/26/2024, 12:17 AMsnowe
01/26/2024, 12:17 AMsnowe
01/26/2024, 12:19 AMval exampleLambdaProxyRequest = """
{
"body": "{\"idkanything\":\"json\"}"
}
snowe
01/26/2024, 12:20 AMval exampleLambdaProxyRequest = """
{
"body": "{\"idkanything\":\"json\"}"
}
""".trimIndent()
fun main() {
val out = Json.decodeFromString<ProxyRequest<Some>>(exampleLambdaProxyRequest)
println(out.some.idkanything == "json")
}
@Serializable
data class ProxyRequest<T>(val some: T) // but Some comes across as an Escaped JSON String
@Serializable
data class Some(val idkanything: String)
Adam S
01/26/2024, 12:20 AM@Serializable
class LambdaProxyRequest<T>(
@SerialName("body")
val bodyContent: String? = null,
) {
@Transient
val body: T? = Json.decodeFromString(bodyContent?.unescapeJson())
}
fun String.unescapeJson(): String {
TODO("")
}
snowe
01/26/2024, 12:21 AMsnowe
01/26/2024, 12:21 AMAdam S
01/26/2024, 12:23 AMbody
using JsonTransformingSerializer. You'd check that the incoming JsonElement was a JsonPrimitive.string, and then unescape and then Json.decodeFromString()
snowe
01/26/2024, 12:24 AMAdam S
01/26/2024, 12:24 AMsnowe
01/26/2024, 12:24 AMsnowe
01/26/2024, 12:24 AMAdam S
01/26/2024, 12:25 AMsnowe
01/26/2024, 12:26 AM