Skip to content

Commit

Permalink
fix: use correct json to any map function
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Mar 7, 2024
1 parent 7428d23 commit 05c704a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions core/src/main/kotlin/util/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ internal fun Map<*, *>.toJsonObject(): JsonObject = JsonObject(
internal fun JsonElement.toAny(): Any? {
return when (this) {
is JsonPrimitive -> toAny()
is JsonArray -> toList()
is JsonObject -> toMap()
is JsonArray -> toAnyList()
is JsonObject -> toAnyMap()
}
}

Expand All @@ -57,9 +57,9 @@ internal fun JsonPrimitive.toAny(): Any? {
}
}

internal fun JsonArray.toList(): List<Any?> = map { it.toAny() }
internal fun JsonArray.toAnyList(): List<Any?> = map { it.toAny() }

fun JsonObject.toMap(): Map<String, Any?> = mapValues { it.value.toAny() }
fun JsonObject.toAnyMap(): Map<String, Any?> = mapValues { it.value.toAny() }

internal object AnySerializer : KSerializer<Any?> {
private val delegate = JsonElement.serializer()
Expand Down
5 changes: 3 additions & 2 deletions service/src/main/kotlin/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.amplitude.plugins.configureMetrics
import com.amplitude.util.json
import com.amplitude.util.logger
import com.amplitude.util.stringEnv
import com.amplitude.util.toAnyMap
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
Expand Down Expand Up @@ -275,15 +276,15 @@ private fun ApplicationRequest.getFlagKeys(): Set<String> {
private fun ApplicationRequest.getUserFromHeader(): Map<String, Any?> {
val b64User = this.headers["X-Amp-Exp-User"]
val userJson = Base64.getDecoder().decode(b64User).toString(Charsets.UTF_8)
return json.decodeFromString<JsonObject>(userJson).toMap()
return json.decodeFromString<JsonObject>(userJson).toAnyMap()
}

/**
* Get the user from the body. Used for SDK/REST POST requests.
*/
private suspend fun ApplicationRequest.getUserFromBody(): Map<String, Any?> {
val userJson = this.receiveChannel().toByteArray().toString(Charsets.UTF_8)
return json.decodeFromString<JsonObject>(userJson).toMap()
return json.decodeFromString<JsonObject>(userJson).toAnyMap()
}

/**
Expand Down

0 comments on commit 05c704a

Please sign in to comment.